-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy.py
More file actions
104 lines (82 loc) · 3.09 KB
/
deploy.py
File metadata and controls
104 lines (82 loc) · 3.09 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
"""
Script that reads a json file of source: destination paths and symlinks them.
If the json file is like:
"""
import argparse
import contextlib
import json
import sys
from pathlib import Path
def yellow(msg: str):
return f"\033[93m{msg}\033[0m"
def create_symlinks(json_file):
with open(json_file, "r") as f:
path_mapping = json.load(f)
skip_all = False
overwrite_all = False
for source, destination in path_mapping.items():
source = Path(source).expanduser().resolve()
destination = Path(destination).expanduser()
if not source.exists():
print(yellow(f"Source {source} does not exist. Skipping."))
continue
# If destination is symlink and already points at source, skip it
if destination.is_symlink() and destination.resolve() == source:
continue
if destination.exists() or destination.is_symlink():
if skip_all:
print(f"Skipping: {destination}")
continue
elif overwrite_all:
destination.unlink()
else:
choice = input(
yellow(
f"{destination} already exists. Skip [s], overwrite [o], skip all [S], or overwrite all [O]?: "
)
)
if choice == "S":
skip_all = True
elif choice == "O":
overwrite_all = True
if choice.lower() == "s":
print(f"Skipping: {destination}")
continue
elif choice.lower() == "o":
with contextlib.suppress(FileNotFoundError):
backup = destination.with_suffix(".bak")
backup.write_text(destination.read_text())
destination.unlink()
else:
print("Invalid choice")
exit(1)
try:
destination.symlink_to(source)
print(f"Created symlink: {source} -> {destination}")
except Exception as e:
print(f"Error creating symlink {source} -> {destination}: {str(e)}")
def setup_zshrc():
zshrc_path = Path.home() / ".zshrc"
if not zshrc_path.exists():
zshrc_path.touch()
dotfiles_root = Path(__file__).parent
# Create a .hushlogin file to disable the "Last login" message
Path.home().joinpath(".hushlogin").touch()
source_line = f"source {dotfiles_root}/zsh/zshrc"
if source_line not in zshrc_path.read_text():
with zshrc_path.open("a") as f:
f.write(f"{source_line}\n")
def main():
parser = argparse.ArgumentParser(description="Create symlinks based on a JSON file")
parser.add_argument(
"json_file", help="Path to the JSON file containing source:destination mappings"
)
args = parser.parse_args()
json_file = Path(args.json_file)
if not json_file.is_file():
print(f"Error: File {json_file} not found")
sys.exit(1)
setup_zshrc()
create_symlinks(json_file)
if __name__ == "__main__":
main()