-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall
executable file
·248 lines (217 loc) · 6.23 KB
/
install
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
#!/usr/bin/env python3
import os
import shutil
import subprocess
import sys
from pathlib import Path
CWD = os.path.join(Path.home(), ".dotfiles")
BACKUP_FOLDER = os.path.join(CWD, ".backup")
STOW_FOLDERS = [
"bin", # 0
"bash", # 1
"i3", # 2
"awesome", # 3
"polybar", # 4
"nvim", # 5
"chtsh", # 6
"tmux", # 7
"vim", # 8
"zsh", # 9
"kitty", # 10
"alacritty", # 11
"rofi", # 12
"picom", # 13
"lf", # 14
"stylua", # 15
"xorg", # 16
"wezterm", # 17
"apps", # 18
"dunst", # 19
"qtile", # 20
"hyprland", # 21
"waybar", # 22
]
STOW_CONFIGS = [
"", # 0
".bashrc", # 1
".config/i3", # 2
".config/awesome", # 3
".config/polybar", # 4
".config/nvim", # 5
"", # 6
".tmux.conf", # 7
".vimrc", # 8
".zshrc", # 9
".config/kitty", # 10
".config/alacritty", # 11
".config/rofi", # 12
".config/picom", # 13
".config/lf", # 14
".config/.stylua.toml", # 15
".xprofile", # 16
".config/wezterm", # 17
[
".config/mimeapps.list", # 18
".config/user-dirs.dirs",
".config/user-dirs.locale",
],
".config/dunst", # 19
".config/qtile", # 20
".config/hypr", # 21
".config/waybar", # 22
]
# Get the mode, default to normal
if len(sys.argv) == 1:
MODE = "--normal"
elif len(sys.argv) == 2:
MODE = sys.argv[1]
else:
print("Too many arguments! Try again.")
sys.exit(1)
def is_tool(name):
"""Check whether `name` is in PATH and marked as executable."""
return shutil.which(name) is not None
def exists(path: str) -> bool:
"""Check if a given path exists
Args:
path (str): path to a file or a folder
Returns:
True if the path exists
"""
return os.path.isfile(path) or os.path.isdir(path)
def fullpath(location: str) -> str:
"""Add $HOME to the beginning of given path
Args:
location (str): path to file/folder
Returns:
Full path to file/folder
"""
return os.path.join(Path.home(), location)
def backup(location: str) -> None:
"""Create a backup of an existing config
Args:
location (str): path to the config
Returns:
None
"""
# filter empty strings
if not location:
return
full_path = fullpath(location)
if exists(full_path) and not os.path.islink(full_path):
if not exists(BACKUP_FOLDER):
os.mkdir(BACKUP_FOLDER)
# if a backup already exist, add backup2
basename = os.path.basename(location)
# TODO: refactor
if os.path.isfile(full_path):
name, ext = basename.split(".")
else:
name = basename
count = len([el for el in os.listdir(BACKUP_FOLDER) if name in el])
if count > 0:
# turn settings.json into settings1.json and not settings.json1
if os.path.isfile(full_path):
new_name = name + str(count + 1) + "." + ext
else:
new_name = basename + str(count + 1)
backup_path = os.path.join(BACKUP_FOLDER, new_name)
else:
backup_path = os.path.join(BACKUP_FOLDER, basename)
print("Found existing", location, "-> Moving it to", backup_path)
shutil.move(full_path, backup_path)
def get_config_names(): # -> list[str]: -- not supported in 3.7
"""Get the names of configs to install
Returns:
List of names
"""
names = []
to_stow = []
# Full installation (combine small and normal)
if MODE == "-f" or MODE == "--full":
# indexex of folders to stow
to_stow = [0, 1, 5, 6, 7, 8, 9]
# Small installation
elif MODE == "-s" or MODE == "--small":
to_stow = [0, 1, 6, 7, 8]
# Linux installation
elif MODE == "-l" or MODE == "--linux":
# autopep8: off
to_stow = [0, 5, 6, 7, 9, 10, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22]
# autopep8: on
# Remote server installation
elif MODE == "--server":
to_stow = [0, 1, 7, 8]
# Normal mode (good for wsl)
elif MODE == "--normal":
to_stow = [0, 5, 6, 7, 9]
else:
print("Error:", MODE, "argument doesn't exist")
sys.exit(1)
if to_stow:
# check for existing files to backup
for ind in to_stow:
conf = STOW_CONFIGS[ind]
if isinstance(conf, list):
for loc in conf:
backup(loc)
else:
backup(conf)
names.append(STOW_FOLDERS[ind])
return names
def install():
# Check if stow is installed
if not is_tool("stow"):
print("Error: stow not found")
sys.exit(1)
# Check if in ~/.dotfiles
if os.getcwd() != CWD:
print("Error: not in", CWD)
sys.exit(1)
# Stow
names = get_config_names()
for name in names: # pyright: ignore [reportOptionalIterable]
subprocess.run(["stow", "-vR", name])
# print("Done")
def unixpool_install():
# Install tmux-sessionizer, bash, vim, tmux, i3
subprocess.run(
[
"mkdir",
"-p",
fullpath(".local/bin"),
fullpath(".config"),
fullpath(".config/i3"),
fullpath(".config/i3status"),
]
)
pairs = [
(".dotfiles/bin/.local/bin/tmux-sessionizer", ".local/bin/tmux-sessionizer"),
(
".dotfiles/bin/.local/bin/set-random-wallpaper",
".local/bin/set-random-wallpaper",
),
(".dotfiles/bash/.bashrc", ".bashrc"),
(".dotfiles/bash/.bash_profile", ".bash_profile"),
(".dotfiles/vim/.vimrc", ".vimrc"),
(".dotfiles/tmux/.tmux.conf", ".tmux.conf"),
(".dotfiles/i3/.config/i3/config.alt", ".config/i3/config"),
(".dotfiles/i3/.config/i3status/config.alt", ".config/i3status/config"),
]
for pair in pairs:
backup(pair[1])
subprocess.run(
[
"ln",
"-s",
fullpath(pair[0]),
fullpath(pair[1]),
]
)
print("Adding symlink to", pair[1])
# print("Done")
if __name__ == "__main__":
if MODE == "--unixpool":
unixpool_install()
else:
install()