-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmount.py
More file actions
executable file
·47 lines (37 loc) · 1.22 KB
/
Copy pathmount.py
File metadata and controls
executable file
·47 lines (37 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/env python3
from pathlib import Path
import shutil
import os
os.chdir(Path(__file__).parent)
CWD = Path(os.getcwd())
HOME = CWD / "HOME"
ROOT = CWD / "ROOT"
BACKUP = CWD / "BACKUP"
HOST_HOME = Path.home()
HOST_ROOT = Path("/")
def iter_files(root: Path):
for path in root.rglob("*"):
if path.is_file():
yield path
def iter_files_relative_to(root: Path):
for file in iter_files(root):
yield file.relative_to(root)
if __name__ == '__main__':
BACKUP.mkdir(parents=True, exist_ok=True)
for file_path in iter_files_relative_to(HOME):
source = HOME / file_path
target = HOST_HOME / file_path
backup = BACKUP / file_path
# target.exists() doesn't report symlinks
if target.exists() or target.is_symlink():
if target.is_symlink():
print(f"REMOVE {target}")
os.remove(target)
else:
print(f"BACKUP {target} -> {backup}")
backup.parent.mkdir(parents=True, exist_ok=True)
shutil.move(target, backup)
else:
target.parent.mkdir(parents=True, exist_ok=True)
print(f"LINK {source} -> {target}")
os.symlink(source, target)