-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.py
More file actions
executable file
·95 lines (72 loc) · 2.55 KB
/
Copy pathsetup.py
File metadata and controls
executable file
·95 lines (72 loc) · 2.55 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/env python3
import os
import re
import shutil
import sys
DEBUG = "--debug" in sys.argv
DOTFILES_DIR = os.path.dirname(os.path.abspath(__file__))
CONFIG_FILE = os.path.join(DOTFILES_DIR, "dotfiles.conf")
def log(msg):
print(msg)
def debug(msg):
if DEBUG:
print(f"🐞 {msg}")
def expand_path(path: str) -> str:
return os.path.expanduser(path)
def process_package(name: str, target: str, ignore: list[str]):
src = os.path.join(DOTFILES_DIR, name)
if not os.path.isdir(src):
log(f"⚠️ Skipping {name} (no folder {src})")
return
target = expand_path(target)
log(f"📦 Linking {name} → {target}")
os.makedirs(target, exist_ok=True)
for item in os.listdir(src):
if item in ignore:
log(f" ⏭️ Ignoring {item}")
continue
src_item = os.path.join(src, item)
dest_item = os.path.join(target, item)
if os.path.exists(dest_item) and not os.path.islink(dest_item):
backup = dest_item + ".bak"
shutil.move(dest_item, backup)
log(f" 🔒 Backed up {dest_item} → {backup}")
if os.path.islink(dest_item) or os.path.exists(dest_item):
os.remove(dest_item)
os.symlink(src_item, dest_item)
log(f" 🔗 {dest_item} → {src_item}")
def main():
if not os.path.isfile(CONFIG_FILE):
log(f"❌ Config file not found: {CONFIG_FILE}")
sys.exit(1)
log(f"🔗 Installing dotfiles from {DOTFILES_DIR}")
current_package = None
target = None
ignore = []
with open(CONFIG_FILE, "r", encoding="utf-8") as f:
for raw_line in f:
line = raw_line.strip()
debug(f"LINE: '{line}'")
if not line or line.startswith("#"):
continue
section = re.match(r"^\[(.+)\]$", line)
if section:
if current_package and target:
process_package(current_package, target, ignore)
current_package = section.group(1)
target = None
ignore = []
continue
match = re.match(r"^target=(.+)$", line)
if match:
target = match.group(1)
continue
match = re.match(r"^ignore=(.+)$", line)
if match:
ignore = match.group(1).split()
continue
if current_package and target:
process_package(current_package, target, ignore)
log("------------Completed------------")
if __name__ == "__main__":
main()