-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlink.py
More file actions
executable file
·368 lines (311 loc) · 14.1 KB
/
Copy pathlink.py
File metadata and controls
executable file
·368 lines (311 loc) · 14.1 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
#!/usr/bin/env python3
"""Check and create symlinks from dotfiles repo to filesystem locations."""
from __future__ import annotations
import argparse
import base64
import filecmp
import io
import os
import platform
import shutil
import subprocess
import sys
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding="utf-8", errors="replace")
sys.stderr = io.TextIOWrapper(sys.stderr.buffer, encoding="utf-8", errors="replace")
from datetime import datetime
from enum import Enum, auto
from pathlib import Path
from typing import Callable
class Status(Enum):
OK = auto()
MISSING = auto()
WRONG_TARGET = auto()
STALE_COPY = auto()
STALE_DIFF = auto()
ELEVATION_NEEDED = auto()
COVERED = auto()
ERROR = auto()
REPO = Path(__file__).resolve().parent
def _wt_settings() -> Path | None:
"""Resolve Windows Terminal settings.json (Store version)."""
packages = Path.home() / "AppData/Local/Packages"
if not packages.is_dir():
return None
for entry in sorted(packages.iterdir()):
if entry.name.startswith("Microsoft.WindowsTerminal"):
target = entry / "LocalState/settings.json"
if target.parent.is_dir():
return target
return None
LinkTarget = str | Callable[[], Path | None]
LINKS: list[tuple[str, LinkTarget, list[str]]] = [
# ── Cross-platform (checked everywhere) ──────────────────────
("bash/bashrc", "~/.bashrc", ["all"]),
("bash/inputrc", "~/.inputrc", ["all"]),
("zsh/zshrc", "~/.zshrc", ["all"]),
("zsh/zprofile", "~/.zprofile", ["all"]),
("wezterm/wezterm.lua", "~/.wezterm.lua", ["all"]),
("tmux/tmux.conf", "~/.tmux.conf", ["all"]),
("npmrc", "~/.npmrc", ["all"]),
("python_startup.py", "~/.python_startup.py", ["all"]),
("emacs/init.el", "~/.emacs.d/init.el", ["all"]),
("emacs/spacemacs", "~/.spacemacs", ["all"]),
("emacs/_emacs", "~/.emacs", ["all"]),
("nvim", "~/.config/nvim", ["all"]),
# ── Windows only -----------------------------------------------
("helix", "~/AppData/Roaming/helix", ["windows"]),
("nvim", "~/AppData/Local/nvim", ["windows"]),
("Zellij", "~/AppData/Roaming/Zellij", ["windows"]),
("terminal/settings.json", _wt_settings, ["windows"]),
("powershell/Microsoft.PowerShell_profile.ps1",
"~/OneDrive/Documents/PowerShell/Microsoft.PowerShell_profile.ps1",
["windows"]),
# ── Linux-only ──────────────────────────────────────────────
("Zellij", "~/.config/Zellij", ["windows"]),
("nvim", "~/.config/nvim", ["linux"]),
# ── macOS-only ──────────────────────────────────────────────
("nvim", "~/.config/nvim", ["darwin"]),
]
def expand(path: str) -> Path:
return Path(os.path.expanduser(path))
def resolve_target(entry: LinkTarget) -> Path | None:
if callable(entry):
return entry()
return expand(entry)
def _parent_is_repo_link(target: Path) -> str | None:
"""If any parent dir of target is a symlink into the repo, return the source_rel."""
real_repo = os.path.realpath(str(REPO)).lstrip("\\").lstrip("?").lstrip("\\")
for parent in target.parents:
if os.path.islink(str(parent)):
link_dest = os.readlink(str(parent))
if not os.path.isabs(link_dest):
link_path = os.path.join(str(parent.parent), link_dest)
else:
link_path = link_dest
real_link = os.path.realpath(link_path).lstrip("\\").lstrip("?").lstrip("\\")
if real_link.startswith(real_repo):
# Return the relative path inside the repo that this parent covers
return os.path.relpath(real_link, real_repo).replace("\\", "/")
return None
def get_status(source: Path, target: Path) -> Status:
# Check if a parent directory is already a symlink into the repo
covered_by = _parent_is_repo_link(target)
if covered_by:
return Status.COVERED
# Check symlink first (must check before exists(), which follows links)
is_link = os.path.islink(str(target))
if is_link:
link_dest = os.readlink(str(target))
if not os.path.isabs(link_dest):
link_path = os.path.join(str(target.parent), link_dest)
else:
link_path = link_dest
# Normalize to remove \\?\ prefix on Windows
real_link = os.path.realpath(link_path).lstrip("\\").lstrip("?").lstrip("\\")
real_src = os.path.realpath(str(source)).lstrip("\\").lstrip("?").lstrip("\\")
if Path(real_link) == Path(real_src):
return Status.OK
return Status.WRONG_TARGET
if not target.exists():
return Status.MISSING
# Plain file — check if content matches
try:
if filecmp.cmp(source, target, shallow=False):
return Status.STALE_COPY
return Status.STALE_DIFF
except OSError:
return Status.ERROR
STYLE = {
Status.OK: ("\033[92m", "OK"),
Status.MISSING: ("\033[91m", "MISSING"),
Status.WRONG_TARGET: ("\033[93m", "WRONG"),
Status.STALE_COPY: ("\033[93m", "STALE copy"),
Status.STALE_DIFF: ("\033[93m", "STALE diff"),
Status.ELEVATION_NEEDED: ("\033[94m", "ELEVATE"),
Status.COVERED: ("\033[94m", "COVERED"),
Status.ERROR: ("\033[91m", "ERROR"),
}
def check_links(entries: list[tuple[str, LinkTarget, list[str]]],
platform_ok: str) -> list[tuple[str, Path, Path | None, Status]]:
results: list[tuple[str, Path, Path | None, Status]] = []
for source_rel, target_def, _ in entries:
source = (REPO / source_rel).resolve()
target = resolve_target(target_def)
if target is None:
results.append((source_rel, source, None, Status.ERROR))
continue
results.append((source_rel, source, target, get_status(source, target)))
return results
def print_results(results: list[tuple[str, Path, Path | None, Status]], verbose: bool = False):
lines = [f"Dotfiles link status - {REPO}", ""]
header = f" {'Status':>12} Repo file -> Symlink"
lines.append(header)
lines.append("-" * len(header))
for source_rel, source, target, status in results:
style_code, label = STYLE[status]
target_str = str(target) if target else "?"
if status in (Status.STALE_COPY, Status.STALE_DIFF):
extra = " (copy)" if status == Status.STALE_COPY else " (diff)"
label += extra
elif status == Status.COVERED and target:
covered_by = _parent_is_repo_link(target)
if covered_by:
label += f" ({covered_by})"
reset = "\033[0m" if sys.stdout.isatty() else ""
lines.append(f" {style_code}{label:>12}{reset} {source_rel} -> {target_str} ")
# Summary
counts = {s: 0 for s in Status}
for _, _, _, s in results:
counts[s] += 1
lines.append("")
parts = [f" {counts[s]}x {STYLE[s][1]}" for s in Status if counts[s] > 0]
lines.append(" ".join(parts))
print("\n".join(lines))
def backup_stale(target: Path) -> Path | None:
"""Move an existing file to .backups/ before replacing it."""
backup_dir = REPO / ".backups" / datetime.now().strftime("%Y%m%d_%H%M%S")
backup_dir.mkdir(parents=True, exist_ok=True)
dest = backup_dir / target.name
shutil.move(str(target), str(dest))
return dest
def _is_admin_windows() -> bool:
try:
import ctypes
return ctypes.windll.shell32.IsUserAnAdmin() != 0
except Exception:
return False
def _elevated_symlink_batch(ops: list[tuple[Path, Path]]) -> bool:
"""Run all pending symlink creations in an elevated PowerShell process."""
tmp = Path(os.environ.get("TEMP", "/tmp")) / f"_dotfiles_link_{os.getpid()}.ps1"
lines = [
'$ErrorActionPreference = "Stop"',
]
for source, target in ops:
parent = str(target.parent)
src = str(source)
tgt = str(target)
lines.append(f'if (!(Test-Path "{parent}")) {{ New-Item -ItemType Directory -Path "{parent}" -Force | Out-Null }}')
lines.append(f'if (Test-Path "{tgt}") {{ Remove-Item "{tgt}" -Force }}')
lines.append(f'New-Item -ItemType SymbolicLink -Path "{tgt}" -Target "{src}" -Force')
lines.append(f'Remove-Item "{tmp}" -Force')
tmp.write_text("\n".join(lines), encoding="utf-8")
try:
result = subprocess.run([
"powershell", "-NoProfile", "-ExecutionPolicy", "Bypass",
"-Command",
f'Start-Process -Verb RunAs -Wait powershell -ArgumentList '
f'\'-NoProfile -ExecutionPolicy Bypass -File "{tmp}"\'',
], capture_output=True, text=True, timeout=120)
if result.returncode != 0:
print(f" Elevation failed: {result.stderr.strip()}")
return False
return True
except subprocess.TimeoutExpired:
print(" Elevation timed out (UAC may have been dismissed or ignored)")
return False
except Exception as e:
print(f" Elevation error: {e}")
return False
finally:
if tmp.exists():
try:
tmp.unlink()
except OSError:
pass
def fix_links(results: list[tuple[str, Path, Path | None, Status]],
force: bool = False) -> int:
"""Create missing symlinks and fix stale ones."""
needs_fix = [r for r in results if r[3] in (Status.MISSING, Status.STALE_COPY, Status.STALE_DIFF, Status.WRONG_TARGET)]
if not needs_fix:
print("Nothing to fix.")
return 0
fixed = 0
elevated_ops: list[tuple[Path, Path]] = []
needs_elevation = False
for source_rel, source, target, status in needs_fix:
if target is None:
continue
print(f" {target} -> {source_rel}", end="")
if status == Status.WRONG_TARGET:
target.unlink()
print(" (removed wrong symlink)", end="")
elif status in (Status.STALE_COPY, Status.STALE_DIFF):
if force or status == Status.STALE_DIFF:
backup = backup_stale(target)
print(f" (backed up -> {backup.name})", end="")
else:
choice = input(" - file content matches repo, skip? [Y/s] ").strip().lower()
if choice and choice not in ("n", "no", "overwrite"):
print(" -> SKIPPED")
continue
backup = backup_stale(target)
print(f" (backed up -> {backup.name})", end="")
# Attempt symlink creation
target.parent.mkdir(parents=True, exist_ok=True)
try:
os.symlink(source, target)
print(" -> OK")
fixed += 1
except OSError:
elevated_ops.append((source, target))
needs_elevation = True
print(" -> needs elevation")
except Exception as e:
print(f" -> ERROR: {e}")
if needs_elevation:
if platform.system() == "Windows":
if _is_admin_windows():
print("\nAlready running as administrator. Retrying symlinks directly...")
for src, tgt in elevated_ops:
try:
tgt.parent.mkdir(parents=True, exist_ok=True)
if tgt.exists() or tgt.is_symlink():
tgt.unlink()
os.symlink(src, tgt)
fixed += 1
print(f" {src.name} -> OK")
except OSError as e:
print(f" {src.name} -> ERROR: {e}")
else:
print(f"\n* Launching elevated PowerShell to create {len(elevated_ops)} symlink(s)...")
if _elevated_symlink_batch(elevated_ops):
fixed += len(elevated_ops)
print("Done.")
else:
print(f"Failed to create {len(elevated_ops)} symlink(s). Run as Administrator and try again.")
else:
print(f"\nRun with sudo: sudo python3 {__file__} --fix")
return fixed
def main():
ap = argparse.ArgumentParser(description="Manage dotfiles symlinks")
ap.add_argument("--fix", action="store_true", help="Create/fix missing or stale symlinks")
ap.add_argument("--force", action="store_true", help="In --fix mode, overwrite even if content matches")
args = ap.parse_args()
current_platform = platform.system().lower()
if current_platform == "windows":
platform_ok = "windows"
elif current_platform == "darwin":
platform_ok = "darwin"
else:
platform_ok = "linux"
# Filter links for current platform
active = []
for source_rel, target_def, platforms in LINKS:
if "all" in platforms or platform_ok in platforms:
active.append((source_rel, target_def, platforms))
results = check_links(active, platform_ok)
print_results(results)
if args.fix:
count = fix_links(results, force=args.force)
if count > 0:
print(f"\nCreated/fixed {count} symlink(s). Run again to verify.")
elif count == 0:
pass
# Return exit code: 0 if all OK, 1 if any issues
exit_code = 0
for _, _, _, s in results:
if s != Status.OK:
exit_code = 1
return exit_code
if __name__ == "__main__":
sys.exit(main())