-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrun.py
534 lines (444 loc) · 17.5 KB
/
run.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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
#!/usr/bin/env python
from __future__ import annotations
import logging
import pathlib
import platform
import shutil
import shlex
import subprocess
import warnings
def get_script_path() -> pathlib.Path:
return pathlib.Path.cwd() / __file__
def eval_os_cmd(cmd: str) -> tuple[int, str]:
logging.debug(shlex.split(cmd))
proc = subprocess.Popen(shlex.split(cmd),
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
stdout, stderr = proc.communicate()
if proc.returncode:
return proc.returncode, f"Evaluation of {cmd} raised the error {stderr}"
else:
return proc.returncode, stdout.decode("utf8")
def install_brew():
if platform.system() == "Darwin":
if shutil.which("brew") is None:
eval_os_cmd(
"/bin/bash -c '$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)'"
)
else:
raise ValueError(f"{platform.system()} is not supported")
def install_git():
if platform.system() == "Darwin":
if shutil.which("git") is None:
eval_os_cmd("brew install git")
elif platform.system() == "Linux":
if shutil.which("git") is None:
eval_os_cmd("sudo apt install -y git")
else:
raise ValueError(f"{platform.system()} is not supported")
def install_curl():
if platform.system() == "Darwin":
if shutil.which("curl") is None:
eval_os_cmd("brew install curl")
elif platform.system() == "Linux":
if shutil.which("curl") is None:
eval_os_cmd("sudo apt install -y curl")
else:
raise ValueError(f"{platform.system()} is not supported")
def copy_tmux_config(dot_folder: pathlib.Path):
logging.debug("Installing tmux config ...")
tmux_conf_src = dot_folder / "tmux.conf"
tmux_conf_dst = pathlib.Path.home() / ".tmux.conf"
if tmux_conf_dst.exists():
if tmux_conf_dst.is_symlink() and tmux_conf_dst.resolve(
) == tmux_conf_src:
logging.info(
f"{tmux_conf_dst} is alread symlink to the {tmux_conf_src}")
else:
raise ValueError(f"{tmux_conf_dst} exists.")
else:
tmux_conf_dst.symlink_to(tmux_conf_src)
def copy_neovim_config(dot_folder: pathlib.Path):
logging.debug("Installing neovim config ...")
neovim_conf_src = dot_folder / "nvim"
neovim_conf_dst = pathlib.Path.home() / ".config/nvim"
if neovim_conf_dst.exists():
if neovim_conf_dst.is_symlink() and neovim_conf_dst.resolve(
) == neovim_conf_src:
logging.info(
f"{neovim_conf_dst} is alread symlink to the {neovim_conf_src}"
)
else:
raise ValueError(f"{neovim_conf_dst} exists.")
else:
neovim_conf_dst.symlink_to(neovim_conf_src)
def copy_git_message(dot_folder: pathlib.Path):
logging.debug("Installing git message...")
gitmessage_src = dot_folder / "gitmessage"
gitmessage_dst = pathlib.Path.home() / ".gitmessage"
if gitmessage_dst.exists():
if gitmessage_dst.is_symlink() and gitmessage_dst.resolve(
) == gitmessage_src:
logging.info(
f"{gitmessage_dst} is alread symlink to the {gitmessage_src}")
else:
raise ValueError(f"{gitmessage_dst} exists.")
else:
gitmessage_dst.symlink_to(gitmessage_src)
eval_os_cmd("git config --global commit.template ~/.gitmessage")
def copy_kitty_config(dot_folder: pathlib.Path):
logging.debug("Installing kitty config ...")
if platform.system() == "Linux":
kitty_conf_src = dot_folder / "kitty"
kitty_conf_dst = pathlib.Path.home() / ".config/kitty"
if kitty_conf_dst.exists():
if kitty_conf_dst.is_symlink() and kitty_conf_dst.resolve(
) == kitty_conf_src:
logging.info(
f"{kitty_conf_dst} is alread symlink to the {kitty_conf_src}"
)
else:
raise ValueError(f"{kitty_conf_dst} exists.")
else:
kitty_conf_dst.symlink_to(kitty_conf_src)
else:
logging.info(f"Platform {platform.platform()} is not supported")
def copy_zsh_config(dot_folder: pathlib.Path):
logging.debug("Installing zsh config ...")
zsh_conf_src = dot_folder / "zshrc"
zsh_conf_dst = pathlib.Path.home() / ".zshrc"
if zsh_conf_dst.exists():
if zsh_conf_dst.is_symlink() and zsh_conf_dst.resolve(
) == zsh_conf_src:
logging.info(
f"{zsh_conf_dst} is alread symlink to the {zsh_conf_src}")
else:
raise ValueError(f"{zsh_conf_dst} exists.")
else:
zsh_conf_dst.symlink_to(zsh_conf_src)
def install_fira_code():
logging.debug("Installing fira code...")
if platform.system() == "Linux":
rcode, msg = eval_os_cmd("apt list --installed fonts-firacode")
if not rcode and len(msg.rstrip().split("\n")) == 1:
rcode, msg = eval_os_cmd("sudo apt install -y fonts-firacode")
if rcode:
logging.critical(msg)
elif platform.system() == "Darwin":
install_brew()
rcode, _ = eval_os_cmd("brew list --cask font-fira-code")
if rcode:
warnings.warn(f"list firacode returns {rcode}")
eval_os_cmd(
"brew tap homebrew/cask-fonts && brew cask install font-fira-code"
)
def install_dircolors():
logging.debug("Installing dircolors...")
if platform.system() == "Darwin":
rcode, _ = eval_os_cmd("brew list coreutils")
if rcode:
rcode, msg = eval_os_cmd("brew install coreutils")
if rcode:
logging.critical(msg)
elif platform.system() == "Linux":
logging.info("Linux has dircolors by default")
else:
raise ValueError(f"{platform.system()} platform is not supported yet")
def install_zsh():
logging.debug("Installing zsh...")
if platform.system() == "Linux":
rcode, msg = eval_os_cmd("apt list --installed zsh")
if not rcode and len(msg.rstrip().split("\n")) == 1:
rcode, msg = eval_os_cmd("sudo apt install -y zsh")
if rcode:
logging.critical(msg)
rcode, msg = eval_os_cmd("chsh -s $(which zsh) dbihbka")
if rcode:
logging.critical(msg)
elif platform.system() == "Darwin":
logging.info("MacOS X uses zsh by default")
else:
raise ValueError(f"{platform.system()} platform is not supported yet")
def install_kitty():
logging.debug("Installing kitty...")
if platform.system() == "Linux":
rcode, msg = eval_os_cmd("apt list --installed kitty")
if not rcode and len(msg.rstrip().split("\n")) == 1:
rcode, msg = eval_os_cmd("sudo apt install -y kitty")
if rcode:
logging.critical(msg)
elif platform.system() == "Darwin":
logging.info("with use iTerm2 in MacOS X usually")
else:
raise ValueError(f"{platform.system()} platform is not supported yet")
def install_vim_plug():
logging.info("Install vim plug...")
config_folder = pathlib.Path.home() / ".local/share"
logging.critical(
f"curl -fLo {config_folder}/nvim/site/autoload/plug.vim --create-dirs https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim"
)
def install_node():
# list of useful nvm commands: https://github.com/nvm-sh/nvm#usage
logging.info("Installing node via NVM...")
if shutil.which("node") is None:
rcode, msg = eval_os_cmd(
"curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash && nvm use stable"
)
if rcode:
logging.critical(msg)
def install_neovim():
if platform.system() == "Darwin":
if shutil.which("nvim") is None:
eval_os_cmd("brew install --fetch-HEAD tree-sitter luajit neovim")
elif platform.system() == "Linux":
if shutil.which("nvim") is None:
eval_os_cmd("sudo apt install -y neovim")
else:
raise ValueError(f"{platform.system()} is not supported")
def install_tmux():
if platform.system() == "Darwin":
if shutil.which("tmux") is None:
eval_os_cmd("brew install tmux")
elif platform.system() == "Linux":
if shutil.which("tmux") is None:
eval_os_cmd("sudo apt install -y tmux")
else:
raise ValueError(f"{platform.system()} is not supported")
def install_diff():
if platform.system() == "Darwin":
if shutil.which("diff-so-fancy") is None:
eval_os_cmd("brew install diff-so-fancy")
elif platform.system() == "Linux":
if shutil.which("diff-so-fancy") is None:
eval_os_cmd("sudo apt install -y diff-so-fancy")
else:
raise ValueError(f"{platform.system()} is not supported")
# postinstall usage
# https://github.com/so-fancy/diff-so-fancy#usage
commands = [
'git config --global core.pager "diff-so-fancy | less --tabs=4 -RFX"',
'git config --global interactive.diffFilter "diff-so-fancy --patch"',
'git config --global color.ui true',
'git config --global color.diff-highlight.oldNormal "red bold"',
'git config --global color.diff-highlight.oldHighlight "red bold 52"',
'git config --global color.diff-highlight.newNormal "green bold"',
'git config --global color.diff-highlight.newHighlight "green bold 22"',
'git config --global color.diff.meta "11"',
'git config --global color.diff.frag "magenta bold"',
'git config --global color.diff.func "146 bold"',
'git config --global color.diff.commit "yellow bold"',
'git config --global color.diff.old "red bold"',
'git config --global color.diff.new "green bold"',
'git config --global color.diff.whitespace "red reverse"',
]
for cmd in commands:
eval_os_cmd(cmd)
def install_df():
if platform.system() == "Darwin":
if shutil.which("duf") is None:
eval_os_cmd("brew install duf")
elif platform.system() == "Linux":
if shutil.which("duf") is None:
eval_os_cmd("sudo apt install -y duf")
else:
raise ValueError(f"{platform.system()} is not supported")
def install_fx():
if platform.system() == "Darwin":
if shutil.which("fx") is None:
eval_os_cmd("npm install -g fx")
elif platform.system() == "Linux":
if shutil.which("fx") is None:
eval_os_cmd("sudo npm install -g fx")
else:
raise ValueError(f"{platform.system()} is not supported")
def install_tldr():
if platform.system() == "Darwin":
if shutil.which("tldr") is None:
eval_os_cmd("npm install -g tldr")
elif platform.system() == "Linux":
if shutil.which("tldr") is None:
eval_os_cmd("sudo npm install -g tldr")
else:
raise ValueError(f"{platform.system()} is not supported")
def install_zsh_plugins():
install_git()
zsh_folder = pathlib.Path.home() / ".zsh"
if not zsh_folder.exists():
zsh_folder.mkdir()
rcode, msg = eval_os_cmd(
f"git clone https://github.com/zsh-users/zsh-autosuggestions {zsh_folder}/zsh-autosuggestions"
)
if rcode:
warnings.warn(msg)
rcode, msg = eval_os_cmd(
f"git clone --recursive https://github.com/joel-porquet/zsh-dircolors-solarized {zsh_folder}/zsh-dircolors-solarized"
)
if rcode:
logging.critical(msg)
def install_starship():
logging.debug("Installing starship...")
if platform.system() == "Darwin":
rcode, _ = eval_os_cmd("brew list starship")
if rcode:
eval_os_cmd("brew install starship")
elif platform.system() == "Linux":
if shutil.which("starship") is None:
rcode, msg = eval_os_cmd(
"curl -fsSL https://starship.rs/install.sh | bash")
if rcode:
raise RuntimeError(msg)
else:
raise ValueError(f"{platform.system()} platform isnot supported yet")
def install_exa():
logging.debug("Installing exa ...")
if platform.system() == "Darwin":
install_brew()
rcode, _ = eval_os_cmd("brew list exa")
if rcode:
rcode, msg = eval_os_cmd("brew install exa")
if rcode:
logging.warn(msg)
elif platform.system() == "Linux":
rcode, msg = eval_os_cmd("apt list --installed exa")
if not rcode and len(msg.rstrip().split("\n")) == 1:
rcode, msg = eval_os_cmd("sudo apt install -y exa")
if rcode:
logging.critical(msg)
def install_fd():
logging.debug("Installing fd ...")
if platform.system() == "Darwin":
install_brew()
rcode, _ = eval_os_cmd("brew list fd")
if rcode:
rcode, msg = eval_os_cmd("brew install fd")
if rcode:
logging.warn(msg)
elif platform.system() == "Linux":
rcode, msg = eval_os_cmd("apt list --installed fd-find")
if not rcode and len(msg.rstrip().split("\n")) == 1:
rcode, msg = eval_os_cmd("sudo apt install -y fd-find")
if rcode:
logging.critical(msg)
def install_rg():
logging.debug("Installing rg (ripgrep) ...")
if platform.system() == "Darwin":
install_brew()
rcode, _ = eval_os_cmd("brew list ripgrep")
if rcode:
rcode, msg = eval_os_cmd("brew install ripgrep")
if rcode:
logging.warn(msg)
elif platform.system() == "Linux":
rcode, msg = eval_os_cmd("apt list --installed ripgrep")
if not rcode and len(msg.rstrip().split("\n")) == 1:
rcode, msg = eval_os_cmd("sudo apt install -y ripgrep")
if rcode:
logging.critical(msg)
def install_bat():
logging.debug("Installing bat ...")
if platform.system() == "Darwin":
install_brew()
rcode, _ = eval_os_cmd("brew list bat")
if rcode:
rcode, msg = eval_os_cmd("brew install bat")
if rcode:
logging.warn(msg)
elif platform.system() == "Linux":
rcode, msg = eval_os_cmd("apt list --installed bat")
if not rcode and len(msg.rstrip().split("\n")) == 1:
rcode, msg = eval_os_cmd("sudo apt install -y bat")
if rcode:
logging.critical(msg)
def generate_alaises(dot_folder, aliases):
logging.debug("Generating aliases ...")
alias_conf_src = dot_folder / "aliases"
alias_conf_dst = pathlib.Path.home() / ".aliases"
with open(alias_conf_src, "w") as src:
grep_cmd = "grep=rg"
dircolors_cmd = "dircolors=dircolors"
cat_cmd = "cat=cat"
find_cmd = "find=find"
df_cmd = "df=duf"
if platform.system() == "Darwin":
dircolors_cmd = "dircolors=gdircolors"
cat_cmd = "cat=bat"
find_cmd = "find=fd"
elif platform.system() == "Linux":
dircolors_cmd = "dircolors=dircolors"
cat_cmd = "cat=batcat"
find_cmd = "find=fdfind"
ls_cmd = "ls=exa"
src.write("\n".join([
f"alias {cmd}" for cmd in aliases +
[ls_cmd, dircolors_cmd, find_cmd, cat_cmd, grep_cmd, df_cmd]
]))
if alias_conf_dst.exists():
if alias_conf_dst.is_symlink() and alias_conf_dst.resolve(
) == alias_conf_src:
logging.info(
f"{alias_conf_dst} is alread symlink to the {alias_conf_src}")
else:
raise ValueError(f"{alias_conf_dst} exists.")
else:
alias_conf_dst.symlink_to(alias_conf_src)
def install_bazel_compilation_database(dot_folder):
"""Install the tool to generate compile_command.json
for bazel projects.
https://github.com/grailbio/bazel-compilation-database
"""
bin_folder = dot_folder / "bin"
if not bin_folder.exists():
bin_folder.mkdir()
install_dir = str(bin_folder)
version = "0.4.5"
cmd = f"/bin/bash -c '$(curl -L https://github.com/grailbio/bazel-compilation-database/archive/{version}.tar.gz | tar --directory {install_dir} -xz)'"
logging.debug(cmd)
rcode, msg = eval_os_cmd(cmd)
if rcode:
logging.critical(msg)
return None
else:
msg = f"bazel compilation database version {version} installed to {install_dir}"
logging.debug(msg)
return f"bazel-compdb={install_dir}/bazel-compilation-database-{version}/generate.sh"
def main():
dot_folder = get_script_path().parent
install_git()
install_curl()
install_fira_code()
install_zsh()
install_zsh_plugins()
install_starship()
install_exa()
install_dircolors()
install_fd()
install_rg()
install_bat()
install_kitty()
install_node()
install_vim_plug()
install_neovim()
install_tmux()
install_diff()
install_df()
install_fx()
install_tldr()
bcdalias = install_bazel_compilation_database(dot_folder)
generate_alaises(dot_folder, [bcdalias])
copy_zsh_config(dot_folder)
copy_tmux_config(dot_folder)
copy_neovim_config(dot_folder)
copy_git_message(dot_folder)
if platform.system() == "Linux":
copy_kitty_config(dot_folder)
if __name__ == "__main__":
try:
from rich.logging import RichHandler
except ImportError:
RichHandler = logging.StreamHandler
FORMAT = "%(message)s"
logging.basicConfig(level="NOTSET",
format=FORMAT,
datefmt="[%X]",
handlers=[RichHandler()])
main()