-
Notifications
You must be signed in to change notification settings - Fork 0
/
sync.py
executable file
·470 lines (386 loc) · 16.3 KB
/
sync.py
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
#!/usr/bin/env python
from __future__ import annotations
import argparse
import hashlib
import os
import shutil
import subprocess
import sys
from collections.abc import Callable, Iterable, Iterator, Sequence
from enum import Enum, auto
from pathlib import Path
from typing import NamedTuple
try:
import rich
except ImportError:
print("rich not found (`pip install rich`), falling back to no colors", file=sys.stderr)
rich = None
DOTROOTDIR = Path("./root")
ROOTDIR = Path("/")
DOTHOMEDIR = Path("./home")
HOMEDIR = Path(f"~{os.environ.get('SUDO_USER', os.getlogin())}") # Make sure we use correct home even in sudo
def yes_no(prompt: str, default: bool | None = None) -> bool:
"""Get a yes/no answer to given prompt by the user."""
if default is None:
prompt += " [y/n]: "
elif default is True:
prompt += "[Y/n]: "
elif default is False:
prompt += "[y/N]: "
else:
raise ValueError("Invalid default value")
while True:
inp = input(prompt).lower()
if inp in {"y", "yes"}:
return True
if inp in {"n", "no"}:
return False
if inp == "" and default is not None:
return default
def int_input(msg: str) -> int:
"""Get a valid integer input from the user."""
while True:
x = input(msg)
try:
return int(x)
except ValueError:
print("Invalid input, expected a number.")
def choice_input(msg: str, choices: Sequence[str]) -> str:
"""Get one of given choices based on a valid user input."""
print(f"{msg}")
for index, choice in enumerate(choices, start=1):
print(f"{index}: {choice}")
x = int_input("Enter your choice: ")
if x < 1 or x > len(choices):
print("Invalid choice! Try again")
return choice_input(msg, choices)
return choices[x - 1]
class DiffStatus(Enum):
NOT_FOUND = auto()
PERMISSION_ERROR = auto()
MATCH = auto()
EXPECTED_SYMLINK = auto()
UNEXPECTED_SYMLINK = auto()
CONTENT_DIFFERS = auto()
SYMLINK_DIFFERS = auto()
UNEXPECTED_DIRECTORY = auto()
class FileDiff(NamedTuple):
dot_file: Path
sys_file: Path
status: DiffStatus
def __repr__(self) -> str:
dot_file = str(self.rel_dot_file)
sys_file = str(self.sys_file)
status = self.status.name
return f"FileDiff({dot_file=}, {sys_file=}, {status=})"
@property
def rel_dot_file(self) -> Path:
"""Returns path to a dot_file relative to workdir."""
return self.dot_file.relative_to(Path.cwd())
def iter_dir(directory: Path) -> Iterator[Path]:
"""Recursively iterate over given directory and yield all files in it."""
for subpath in directory.iterdir():
if subpath.is_file() or subpath.is_symlink():
yield subpath
else:
yield from iter_dir(subpath)
def file_sum(file: Path) -> str:
"""Compute SHA-256 hash sum of given file."""
sha256_hash = hashlib.sha256()
with file.open("rb") as f:
for byte_block in iter(lambda: f.read(4096), b""):
sha256_hash.update(byte_block)
return sha256_hash.hexdigest()
def compare_files(dot_file: Path, sys_file: Path) -> DiffStatus:
"""Compare two files returning their diff status."""
try:
if not sys_file.exists():
return DiffStatus.NOT_FOUND
except PermissionError:
return DiffStatus.PERMISSION_ERROR
if dot_file.is_symlink():
if sys_file.is_symlink():
if dot_file.readlink() == sys_file.readlink():
return DiffStatus.MATCH
# In case the sys_file link uses an absolute path, make sure
# it points to the same location, even if that location is a
# symlink.
dot_target = sys_file.parent.joinpath(dot_file.readlink()).absolute()
sys_target = sys_file.parent.joinpath(sys_file.readlink()).absolute()
if dot_target == sys_target:
return DiffStatus.MATCH
return DiffStatus.SYMLINK_DIFFERS
return DiffStatus.EXPECTED_SYMLINK
if sys_file.is_symlink():
return DiffStatus.UNEXPECTED_SYMLINK
if sys_file.is_dir():
return DiffStatus.UNEXPECTED_DIRECTORY
try:
if file_sum(dot_file) == file_sum(sys_file):
return DiffStatus.MATCH
except PermissionError:
return DiffStatus.PERMISSION_ERROR
return DiffStatus.CONTENT_DIFFERS
def iter_pairs() -> Iterator[tuple[Path, Path]]:
"""Go through all files in the dotfiles directories and match them to system paths.
Yields tuples of (dotfile file path, matching system path)
"""
for dot_file in iter_dir(DOTHOMEDIR):
real_file = Path(HOMEDIR, *dot_file.parts[1:])
yield dot_file.expanduser().absolute(), real_file.expanduser().absolute()
for dot_file in iter_dir(DOTROOTDIR):
real_file = Path(ROOTDIR, *dot_file.parts[1:])
yield dot_file.expanduser().absolute(), real_file.expanduser().absolute()
def iter_diffs() -> Iterator[FileDiff]:
"""Go through all files and compute their diffs."""
for dot_file, sys_file in iter_pairs():
diff_status = compare_files(dot_file, sys_file)
yield FileDiff(dot_file, sys_file, diff_status)
def print_report(diffs: Iterable[FileDiff]) -> None:
"""Pretty print the individual diff statuses."""
# Exhause the iterable, and ensure we work on a copy
diffs = list(diffs)
# Sort by DiffStatus, with MATCH entries being first
diffs.sort(key=lambda v: (v.status is not DiffStatus.MATCH, v.status.name))
if rich is None:
for diff in diffs:
print(f"{diff.status.name} -> {diff.sys_file}")
return
from rich.table import Table
table = Table()
table.add_column("Status")
table.add_column("System file", style="magenta")
table.add_column("Dotfile file", style="magenta")
for diff in diffs:
_str_status = diff.status.name.replace("_", " ")
match diff.status:
case DiffStatus.MATCH:
status_str = f"[green]{_str_status}[/green]"
case DiffStatus.PERMISSION_ERROR:
status_str = f"[bold yellow]{_str_status}[/bold yellow]"
case DiffStatus.NOT_FOUND:
status_str = f"[bold orange_red1]{_str_status}[/bold orange_red1]"
case _:
status_str = f"[bold red]{_str_status}[/bold red]"
try:
# Unexpand home (/home/xyz/foo -> ~/foo)
sys_file = Path("~") / diff.sys_file.relative_to(Path.home())
except ValueError: # File not in home
sys_file = diff.sys_file
sys_file = str(sys_file)
dot_file = "./" + str(diff.rel_dot_file)
table.add_row(status_str, sys_file, dot_file)
rich.print(table)
class FixChoice(Enum):
OVERWRITE_SYSTEM = auto()
OVERWRITE_DOTFILE = auto()
PARTIAL_OVERWRITE = auto()
SKIP = auto()
@classmethod
def pick(cls, file_path: Path, system_type: str | None, dotfile_type: str) -> FixChoice:
if system_type is None:
overwrite_system_prompt = f"Create non-existing {dotfile_type}"
overwrite_dotfile_prompt = f"Delete dotfile {dotfile_type}"
else:
overwrite_system_prompt = f"Overwrite the system {system_type} with the dotfile {dotfile_type}"
overwrite_dotfile_prompt = f"Overwrite the dotfile {dotfile_type} with the system {system_type}"
options = [overwrite_system_prompt, overwrite_dotfile_prompt, "Skip this fix"]
# With 2 files, partial overwrites are also a possibility
partial_prompt = "Selectively apply partial overwrites to either file"
if system_type == dotfile_type == "file":
# Add the partial option before the skip option
options.insert(-1, partial_prompt)
answer = choice_input(f"How to fix {file_path}?", options)
if answer == overwrite_system_prompt:
return cls.OVERWRITE_SYSTEM
if answer == overwrite_dotfile_prompt:
return cls.OVERWRITE_DOTFILE
if answer == partial_prompt:
return cls.PARTIAL_OVERWRITE
if answer == "Skip this fix":
return cls.SKIP
raise Exception("Invalid answer, this can't happen")
def _overwrite_file(source: Path, target: Path, partial: bool = False):
"""Overwrite content of `target` file/directory/symlink with `source` file/directory/symlink.
If `partial` is True, `vimdiff` or `nvim -d` is opened for an interactive editor,
where the user can selectively apply any changes to either file.
"""
if not source.exists():
raise ValueError(f"Can't overwrite target with non-existent source ({target=}, {source=})")
if partial:
if not target.exists():
raise ValueError(f"Can't apply a partial patch with non-existent target ({target=})")
if shutil.which("nvim") is not None:
prog = ["nvim", "-d"]
elif shutil.which("vim") is not None:
prog = ["vimdiff"]
else:
raise ValueError("No diff tool installed, please install neovim or vim")
subprocess.run([*prog, str(source), str(target)], check=True) # noqa: S603
return
# Remove the target, if it already exists
if target.exists():
if target.is_dir():
shutil.rmtree(target)
else:
target.unlink()
# Ensure parent dir exists for the target
target.parent.mkdir(parents=True, exist_ok=True)
# Copy the source to given target, preserving symlinks
if source.is_dir():
shutil.copytree(source, target, symlinks=True)
else:
shutil.copy(source, target, follow_symlinks=False)
def apply_fix(diff: FileDiff) -> None:
"""Ask the user how to fix the difference, and apply requested fix."""
match diff.status:
case DiffStatus.PERMISSION_ERROR:
print("Skipping fix: insufficient permissions")
return
case DiffStatus.UNEXPECTED_DIRECTORY:
_choice = FixChoice.pick(diff.sys_file, "directory", "file")
case DiffStatus.UNEXPECTED_SYMLINK:
_choice = FixChoice.pick(diff.sys_file, "symlink", "file")
case DiffStatus.EXPECTED_SYMLINK:
_choice = FixChoice.pick(diff.sys_file, "file/directory", "symlink")
case DiffStatus.SYMLINK_DIFFERS:
_choice = FixChoice.pick(diff.sys_file, "symlink", "symlink")
case DiffStatus.NOT_FOUND:
_choice = FixChoice.pick(diff.sys_file, None, "file")
case DiffStatus.CONTENT_DIFFERS:
_choice = FixChoice.pick(diff.sys_file, "file", "file")
case status:
raise Exception(f"Diff status {status!r} didn't match on any cases. This should never happen")
partial = False
match _choice:
case FixChoice.SKIP:
return
case FixChoice.OVERWRITE_SYSTEM:
source = diff.dot_file
target = diff.sys_file
case FixChoice.OVERWRITE_DOTFILE:
source = diff.sys_file
target = diff.dot_file
case FixChoice.PARTIAL_OVERWRITE:
# It doesn't really matter which is the target and which is source here
# since the user will be given an interactive environment where they can
# make changes to either file.
source = diff.dot_file
target = diff.sys_file
partial = True
case choice:
raise Exception(f"Choice {choice!r} didn't match on any cases. This should never happen")
try:
_overwrite_file(source=source, target=target, partial=partial)
except PermissionError:
print("Fix failed: insufficient permissions")
return
def show_diffs(diffs: Iterable[FileDiff], ask_show_diff: bool, apply_fix_prompt: bool) -> None:
for diff in diffs:
match diff.status:
case DiffStatus.MATCH:
continue
case DiffStatus.CONTENT_DIFFERS:
if ask_show_diff is False or yes_no(f"Show diff for {diff.sys_file}?"):
subprocess.run( # noqa: PLW1510
["git", "diff", str(diff.dot_file), str(diff.sys_file)], # noqa: S607,S603
)
case _:
_str_status = diff.status.name.replace("_", " ")
print(f"Skipping {diff.sys_file} diff for status: {_str_status}")
if apply_fix_prompt:
apply_fix(diff)
print("---")
def exclude_fun(diff: FileDiff) -> bool:
exclude_rules: list[Callable[[FileDiff], bool]] = [
lambda d: d.status is DiffStatus.MATCH,
lambda d: d.dot_file.name == ".keep" and d.sys_file.parent.is_dir(),
lambda d: Path("root/etc/opensnitchd/rules") in d.rel_dot_file.parents,
lambda d: Path("root/usr/local/src/eww") in d.rel_dot_file.parents,
lambda d: Path("root/usr/local/src/z.lua") in d.rel_dot_file.parents,
lambda d: Path("root/usr/local/src/Hyprland") in d.rel_dot_file.parents,
lambda d: Path("root/usr/share/zsh/site-functions/zsh-you-should-use") in d.rel_dot_file.parents,
lambda d: Path("root/usr/share/zsh/site-functions/zsh-syntax-highlighting") in d.rel_dot_file.parents,
lambda d: Path("root/usr/share/zsh/site-functions/zsh-autosuggestions") in d.rel_dot_file.parents,
lambda d: Path("home/.config/zsh/.zgenom") in d.rel_dot_file.parents,
lambda d: Path("home/.config/tmux/plugins/tpm") in d.rel_dot_file.parents,
lambda d: Path("home/.cache/zsh/history") == d.rel_dot_file and d.status is DiffStatus.CONTENT_DIFFERS,
lambda d: Path("home/.config/nomacs/Image Lounge.conf") == d.rel_dot_file
and d.status is DiffStatus.CONTENT_DIFFERS,
lambda d: Path("home/.config/pcmanfm/default/pcmanfm.conf") == d.rel_dot_file
and d.status is DiffStatus.CONTENT_DIFFERS,
]
return all(not exc_rule(diff) for exc_rule in exclude_rules)
def get_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(
prog="sync.py",
description="Compare the differences between system file and dotfile file versions",
)
parser.add_argument(
"-E",
"--no-apply-excludes",
help="Don't apply exclude rules (some files are expected to be modified, use if you want to see these too)",
action="store_true",
)
parser.add_argument(
"-R",
"--no-show-report",
help="Don't show the full report of all files, and their diff status in the beginning",
action="store_true",
)
exc_grp = parser.add_mutually_exclusive_group()
exc_grp.add_argument(
"-d",
"--show-diffs",
help="Show file diffs comparing the versions (using git diff)",
action="store_true",
default=None,
)
exc_grp.add_argument(
"-D",
"--no-show-diffs",
help="Don't show file diffs comparing the versions",
dest="show_diffs",
action="store_false",
)
parser.add_argument(
"-A",
"--no-ask-each-diff",
help="Don't ask whether to show a diff or not for each modified file (can be annoying for many files)",
action="store_true",
)
parser.add_argument(
"-f",
"--apply-fixes",
help=(
"Asks whether to overwrite the system file with the dotfile version (or vice-versa)."
" This option can only be used with --show-diffs"
),
action="store_true",
default=None,
)
ns = parser.parse_args()
if ns.no_ask_each_diff and not ns.show_diffs:
parser.error("-A/--no-ask-each-diff only makes sense with -d/--show-diffs")
if ns.apply_fixes and not ns.show_diffs:
parser.error("-f/--apply-fixes only makes sense with -d/--show-diffs")
return ns
def main() -> None:
ns = get_args()
diffs = iter_diffs()
if not ns.no_apply_excludes:
diffs = filter(exclude_fun, diffs)
diffs = list(diffs)
if not ns.no_show_report:
print_report(diffs)
if ns.show_diffs is True or ns.show_diffs is None and yes_no("Show diffs for modified files?"):
if ns.apply_fixes is None:
apply_fixes = yes_no("Apply fixes?")
else:
apply_fixes = ns.apply_fixes
show_diffs(diffs, ask_show_diff=not ns.no_ask_each_diff, apply_fix_prompt=apply_fixes)
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
print("\n\nStopped...", file=sys.stderr)