-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.py
executable file
·143 lines (127 loc) · 4.4 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/usr/bin/env python3
import argparse
import os
import re
import subprocess
import sys
def die(code, *args):
if args:
print(*args, file=sys.stderr)
exit(code)
GIT_DEPENDENCIES=[
('nvm', 'https://github.com/nvm-sh/nvm.git'),
('oh-my-zsh', 'https://github.com/robbyrussell/oh-my-zsh.git'),
('powerlevel9k', 'https://github.com/bhilburn/powerlevel9k.git'),
('vim-plug', 'https://github.com/junegunn/vim-plug.git'),
('zsh-histdb', 'https://github.com/larkery/zsh-histdb.git'),
('zsh-autosuggestions', 'https://github.com/zsh-users/zsh-autosuggestions.git'),
]
def setup_deps(should_update):
try:
os.mkdir('./deps')
except FileExistsError:
pass
submodule_revisions = []
err = subprocess.call(['git', '--version'])
if err != 0:
die(err, "Failed to print git version (command not in PATH?)")
for name, repo in GIT_DEPENDENCIES:
clonepath = os.path.join('./deps', name)
if not os.path.isdir(clonepath):
err = subprocess.call(['git', 'clone', '--depth=1', repo, clonepath])
if err != 0:
die(err, "Unable to clone", name)
err = subprocess.call(['git', '-C', clonepath, 'pull' if should_update else 'fetch'])
if err != 0:
die(err, "Unable to pull or fetch", name)
err = subprocess.call(
['git', '-C', clonepath, 'submodule', 'update', '--init', '--recursive']
)
if err != 0:
die(err, "Unable to update submodules for", name)
sp = subprocess.Popen(
['git', '-C', clonepath, 'rev-parse', 'HEAD'],
stdout=subprocess.PIPE,
)
if sp.wait() != 0:
die(err, "Unable to retrieve current revision for", name)
current_revision = re.match(
r'^([0-9a-f]+)\n$',
sp.communicate()[0].decode('ascii'),
).groups()[0]
submodule_revisions.append((name, current_revision))
if should_update:
with open('deps_lock.txt', 'w+') as deps_lock:
deps_lock.truncate(0)
for name, rev in submodule_revisions:
deps_lock.write(name)
deps_lock.write(' ')
deps_lock.write(rev)
deps_lock.write('\n')
with open('deps_lock.txt', 'r') as deps_lock:
for line in deps_lock:
dep_name, revision = re.match(
r'^([a-zA-Z0-9_-]+) ([0-9a-f]+)$',
line
).groups()
clonepath = os.path.join('./deps', dep_name)
sys.stdout.write(dep_name)
sys.stdout.write(': ')
sys.stdout.flush()
err = subprocess.call(
[
'git', '-C', clonepath,
'switch', '--detach', revision
]
)
if err != 0:
die(err, "Unable to swith to detached revision", dep_name, revision)
print("Setup depdendencies successfully.")
def setup_ytdlp():
err = subprocess.call(['git', '--version'])
if err != 0:
die(err, "Failed to print git version (command not in PATH?)")
clonepath = os.path.join('./deps', 'yt-dlp')
if not os.path.exists(clonepath):
err = subprocess.call(['git', 'clone', 'https://github.com/yt-dlp/yt-dlp.git', clonepath])
if err != 0:
die(err, "Unable to clone yt-dlp")
err = subprocess.call(['git', '-C', clonepath, 'fetch'])
if err != 0:
die(err, "Unable to fetch yt-dlp")
err = subprocess.call(
['git', '-C', clonepath, 'switch', '--detach', '2023.07.06']
)
if err != 0:
die(err, "Unable to switch branches")
def main():
parser = argparse.ArgumentParser(
prog="Dotfiles Setup",
description="Setup Eric's dotfiles",
)
parser.add_argument(
'--deps',
action=argparse.BooleanOptionalAction,
)
parser.add_argument(
'--update',
action=argparse.BooleanOptionalAction,
)
parser.add_argument(
'--yt-dlp',
action=argparse.BooleanOptionalAction,
)
args = parser.parse_args()
print("== Dotfiles Setup ==")
anything_done = False
if args.deps:
anything_done = True
setup_deps(should_update=args.update)
if args.yt_dlp:
anything_done = True
setup_ytdlp()
if not anything_done:
print("Nothing was done.")
exit(2)
if __name__ == '__main__':
main()