62 lines
1.4 KiB
Python
Executable File
62 lines
1.4 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
|
|
import os
|
|
import shutil
|
|
import sys
|
|
|
|
def parse_line(line):
|
|
line = line.strip()
|
|
if not line or line.startswith("#"):
|
|
return None
|
|
|
|
for sep in ["→", "->"]:
|
|
if sep in line:
|
|
src, dst = map(str.strip, line.split(sep, 1))
|
|
return src, dst
|
|
|
|
return None
|
|
|
|
|
|
def move_with_overwrite(src, dst):
|
|
if not os.path.exists(src) and not os.path.islink(src):
|
|
print(f"[SKIP missing source] {src}")
|
|
return
|
|
|
|
# If destination exists, remove it (overwrite behavior)
|
|
if os.path.lexists(dst):
|
|
try:
|
|
if os.path.isdir(dst) and not os.path.islink(dst):
|
|
shutil.rmtree(dst)
|
|
else:
|
|
os.remove(dst)
|
|
except Exception as e:
|
|
print(f"[FAIL delete dst] {dst}: {e}")
|
|
return
|
|
|
|
# Ensure parent directory exists
|
|
os.makedirs(os.path.dirname(dst), exist_ok=True)
|
|
|
|
try:
|
|
shutil.move(src, dst)
|
|
print(f"[OK] {src} -> {dst}")
|
|
except Exception as e:
|
|
print(f"[FAIL move] {src} -> {dst}: {e}")
|
|
|
|
|
|
def process(file_path):
|
|
with open(file_path, "r") as f:
|
|
for line in f:
|
|
parsed = parse_line(line)
|
|
if not parsed:
|
|
continue
|
|
|
|
dst, src = parsed
|
|
move_with_overwrite(src, dst)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
if len(sys.argv) != 2:
|
|
print("Usage: python rename_apply.py <file>")
|
|
sys.exit(1)
|
|
|
|
process(sys.argv[1]) |