-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.py
executable file
·127 lines (104 loc) · 4.09 KB
/
setup.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
#!/usr/bin/python3
# setup script for my dot files
#
# from http://github.com/njdart/dot-files
import os.path
import sys
from typing import Callable, Optional
from pathlib import Path
ERRORCOLOUR = "\033[91m"
WARNINGCOLOUR = '\033[93m'
SUCCESS_COLOUR = '\033[32m'
NOCOLOUR = "\033[0m"
class ConfigItem():
src: Path
dest: Path
def __init__(self, src: str, dest: str):
self.src = Path(os.path.expandvars(src)).absolute()
self.dest = Path(os.path.expandvars(dest)).absolute()
def describe(self) -> str:
return f"""{self.__class__.__name__}("{self.src}" -> "{self.dest}")"""
def check_src(self) -> bool:
return self.src.exists()
def ensure_destination(self):
""" ensure_destination ensures the destination directory exists,
creating it if it doesn't. """
dest_dir = self.dest.parent
if not dest_dir.exists():
dest_dir.mkdir(parents=True)
if not dest_dir.is_dir():
raise Exception(f"{dest_dir} exists but is not a directory!")
def symlink_src_to_dest(self) -> Optional[str]:
""" symlink_src_to_dest will attempt to symlink the destination
file to the """
existing_src = None
if self.dest.is_symlink():
existing_src = self.dest.readlink()
if existing_src == self.src:
return None
self.dest.unlink()
self.dest.symlink_to(self.src, target_is_directory=self.src.is_dir())
return existing_src
def setup(self):
if not self.check_src():
raise Exception(f"Source {self.src} does not exist")
try:
self.ensure_destination()
except OSError as e:
raise Exception(f"Error creating destination: {str(e)}")
try:
existed = self.symlink_src_to_dest()
if existed is not None:
raise Exception(f"Link {self.dest} already exists, linking to {existed}. Re-linking to {self.src}")
except Exception as e:
raise Exception(f"Error creating symlink: {str(e)}")
class EmptyDir(ConfigItem):
def __init__(self, dest: str):
super().__init__("", dest)
def describe(self) -> str:
return f"""EmptyDir("{self.dest}")"""
def symlink_src_to_dest(self) -> Optional[str]:
""" This doesn't need to do anything, since calling
ConfigItem.ensure_Destination() will do all the work
"""
pass
def warn(s: str) -> str:
return f"{WARNINGCOLOUR}{s}{NOCOLOUR}"
def error(s: str) -> str:
return f"{ERRORCOLOUR}{s}{NOCOLOUR}"
def setup(configs: list[ConfigItem]):
errors = False
for config in configs:
print(config.describe())
try:
config.setup()
except Exception as e:
print(warn(str(e)))
errors = True
return errors
def globFiles(srcs: str, dest: str) -> list[ConfigItem]:
srcs_path = Path(srcs)
dest_path = Path(dest)
return [ConfigItem(str(s), str(dest_path/s.name)) for s in srcs_path.parent.glob(srcs_path.name)]
if __name__ == "__main__":
errors = setup([
*globFiles("bin/*", "$HOME/bin"),
ConfigItem(".zshrc", "$HOME/.zshrc"),
ConfigItem(".tmux.conf", "$HOME/.tmux.conf"),
ConfigItem("nvim", "$HOME/.config/nvim"),
ConfigItem("terminator", "$HOME/.config/terminator"),
ConfigItem("gitignore_global", "$HOME/.config/gitignore_global"),
ConfigItem("rofi", "$HOME/.config/rofi"),
ConfigItem("dunst", "$HOME/.config/dunst"),
ConfigItem("polybar", "$HOME/.config/polybar"),
ConfigItem("picom.conf", "$HOME/.config/picom.conf"),
ConfigItem("systemd", "$HOME/.config/systemd"),
ConfigItem("./bspwm/sxhkdrc", "$HOME/.config/sxhkd/sxhkdrc"),
ConfigItem("./bspwm/bspwmrc", "$HOME/.config/bspwm/bspwmrc"),
ConfigItem("./vscode/User", "$HOME/.config/Code/User"),
ConfigItem("./vscode/User", "$HOME/.config/Code - OSS/User"),
# Create the screenshot directory for the scrot alias in .zshrc
EmptyDir("$HOME/screenshots"),
])
if errors:
sys.exit(1)