-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsetup
executable file
·321 lines (272 loc) · 9.14 KB
/
setup
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
#!/usr/bin/env python3
import os
import pathlib
import tempfile
import sys
import waflibs
script_dir = waflibs.filedir.get_parent_directory(pathlib.Path(__file__))
dotfiles_dir = pathlib.Path(script_dir, "dotfiles")
wf_parser = waflibs.arg_parse.parser()
wf_parser.enable_verbose_logging()
wf_parser.enable_dry_run()
parser = wf_parser.get_parser()
parser.add_argument(
"-f",
"--force",
action="store_true",
help="do not force commands",
)
parser.add_argument(
"-t",
"-e",
"--test",
"--dev",
action="store_true",
help="dev/test mode",
)
parser.add_argument(
"-c",
"--config",
"--config-file",
type=str,
help="config file",
default=f"{script_dir}/config.yaml",
)
parser.add_argument(
"-i",
"--home-dir",
"--home-directory",
"--directory",
help="destination directory to use",
default=pathlib.Path(os.environ["HOME"]).absolute(),
)
parser.add_argument(
"-o",
"--dotfiles-dir",
help="dotfiles dir",
default=dotfiles_dir,
)
parser.add_argument(
"-u",
"--no-custom",
action="store_true",
help="do not run custom setup",
)
parser.add_argument(
"-s",
"--only-custom",
action="store_true",
help="only run custom setup",
)
parser.add_argument(
"-b",
"--stdout",
action="store_true",
help="print stderr to stdout for easier paging",
)
args = parser.parse_args()
force = args.force
home_dir = args.home_dir
test = args.test
dry_run = args.dry_run
dotfiles_dir = args.dotfiles_dir
no_custom = args.no_custom
only_custom = args.only_custom
config_file = args.config
if test:
dotfiles_dir = pathlib.Path(script_dir, "test")
real_config_file = pathlib.Path(script_dir, "config.yaml")
temp_dir = tempfile.mkdtemp()
home_dir = pathlib.Path(temp_dir).absolute()
args.verbose = True
else:
real_config_file = config_file
logger = waflibs.log.logger(args).get_logger()
verbose = args.verbose
logger.debug(f"config file from arg: {config_file}")
logger.debug(f"real config file: {real_config_file}")
config = waflibs.config.parse_yaml_file(real_config_file)
logger.debug(f"config: {config}")
if test:
for d, sub_dir in config["append"].items():
real_dir = f"{home_dir}/.{d}"
logger.debug(f"creating test dir {real_dir}")
os.mkdir(real_dir)
if sub_dir:
for sd in sub_dir:
real_sub_dir = f"{real_dir}/{sd}"
logger.debug(f"creating test sub dir {real_sub_dir}")
os.mkdir(real_sub_dir)
logger.debug(f"using home dir: {home_dir}")
logger.debug(f"dotfiles dir: {dotfiles_dir}")
logger.debug("args: {}".format(args))
def eprint(*print_args, **kwargs):
if args.stdout:
logger.debug("stdout arg set to true")
kwargs["stdout"] = True
waflibs.text.eprint(*print_args, **kwargs)
if only_custom:
logger.debug(f"only custom is {only_custom}... ONLY running custom script")
else:
os.chdir(dotfiles_dir)
eprint(
"updating repo... ",
newline=False,
)
cmd = "git pull"
logger.debug("command to run: {}".format(cmd))
pull_stdout, pull_stderr, _ = waflibs.shell.command(
cmd.split(" "),
cwd=dotfiles_dir,
)
eprint("done.")
logger.debug("pull stdout: {}".format(pull_stdout))
logger.debug("pull stderr: {}".format(pull_stderr))
eprint(
"running submodule init... ",
newline=False,
)
cmd = "git submodule init"
logger.debug("command to run: {}".format(cmd))
init_stdout, init_stderr, _ = waflibs.shell.command(
cmd.split(" "),
cwd=dotfiles_dir,
)
logger.debug("init stdout: {}".format(init_stdout))
logger.debug("init stderr: {}".format(init_stderr))
eprint("done.")
eprint(
"running submodule sync... ",
newline=False,
)
cmd = "git submodule sync"
logger.debug("command to run: {}".format(cmd))
sync_stdout, sync_stderr, _ = waflibs.shell.command(
cmd.split(" "),
cwd=dotfiles_dir,
)
logger.debug("sync stdout: {}".format(sync_stdout))
logger.debug("sync stderr: {}".format(sync_stderr))
eprint("done.")
eprint(
"running submodule update... ",
newline=False,
)
cmd = "git submodule update"
logger.debug("command to run: {}".format(cmd))
update_stdout, update_stderr, _ = waflibs.shell.command(
cmd.split(" "),
cwd=dotfiles_dir,
)
eprint("done.")
logger.debug("update stdout: {}".format(update_stdout))
logger.debug("update stderr: {}".format(update_stderr))
eprint(
"setting up dotfiles... ",
newline=False,
)
root_entries = [re for re in dotfiles_dir.glob("*")]
root_entries.sort()
for root_entry in root_entries:
root_entry_name = root_entry.name
logger.debug(f"root entry name is {root_entry_name}")
logger.debug(f"root entry is {root_entry}")
if root_entry_name.endswith(("swp",)):
logger.debug(f"ignoring entry... ends with - {root_entry}")
continue
root_dest_dir = waflibs.filedir.get_parent_directory(
waflibs.filedir.get_dest_location(
root_entry,
target_dir=home_dir,
)
)
if not root_dest_dir.is_dir():
logger.debug(
f"{root_dest_dir} directory does not exist, creating..."
)
root_dest_dir.mkdir()
logger.debug(f"root entry is dir: {root_entry}")
append_dirs = config["append"]
logger.debug(f"append dirs: {append_dirs}")
if root_entry_name in append_dirs:
logger.debug(f"will append to root directory {root_entry}")
entries = [e for e in root_entry.glob("*")]
entries.sort()
logger.debug(f"entries: {entries}")
for entry in entries:
logger.debug(f"append entry is {entry}")
dest = waflibs.filedir.get_dest_location(
entry,
target_dir=home_dir,
)
logger.debug(f"append dest is {dest}")
append_dirs_entry = append_dirs[root_entry_name]
logger.debug(f"append dirs entry: {append_dirs_entry}")
if isinstance(append_dirs_entry, list):
waflibs.filedir.backup(dest, dry_run=dry_run)
print(entry.name)
else:
waflibs.filedir.backup_and_remove(dest, dry_run=dry_run)
logger.debug(
f"appending root entry {entry} to dest {dest}"
)
waflibs.filedir.copy(entry, dest, dry_run=dry_run)
logger.debug(waflibs.text.divider(char="*", times=66))
else:
logger.debug(f"not appending {root_entry} to root dir")
dest = waflibs.filedir.get_dest_location(
root_entry,
target_dir=home_dir,
)
logger.debug("dest is {}".format(dest))
waflibs.filedir.backup_and_remove(dest, dry_run=dry_run)
d_p_dir = dest.parent
logger.debug(f"dest parent dir: {d_p_dir}")
if not pathlib.Path(d_p_dir).exists():
logger.debug(
f"dest parent dir {d_p_dir} does not exist... creating dir"
)
pathlib.Path(d_p_dir).mkdir()
logger.debug(f"copying root entry {root_entry} to dest {dest}")
waflibs.filedir.copy(root_entry, dest, dry_run=dry_run)
logger.debug(waflibs.text.divider(char="*", times=66))
else:
dest = waflibs.filedir.get_dest_location(
root_entry,
target_dir=home_dir,
)
logger.debug("dest is {}".format(dest))
waflibs.filedir.backup_and_remove(dest, dry_run=dry_run)
logger.debug(f"copying root entry {root_entry} to dest {dest}")
waflibs.filedir.copy(root_entry, dest, dry_run=dry_run)
logger.debug(waflibs.text.divider(char="-", times=66))
eprint("done.")
if not no_custom or only_custom:
logger.debug(f"will run custom setup script")
for custom_setup_script in config["custom_setup_scripts"]:
custom_setup_script = pathlib.Path(os.path.expandvars(custom_setup_script))
logger.debug(f"custom setup script path: {custom_setup_script}")
if custom_setup_script.is_file():
eprint(waflibs.text.divider(char="*", times=77))
eprint(
f"running custom setup script {custom_setup_script}"
)
cmd = [
str(custom_setup_script),
]
if verbose:
cmd.append("-vvvvvv")
if force:
cmd.append("-ffffff")
logger.debug(f"command to run: `{cmd}`")
custom_stdout, custom_stderr, _ = waflibs.shell.command(
cmd,
)
logger.debug("custom stdout: {}".format(custom_stdout))
logger.debug("custom stderr: {}".format(custom_stderr))
else:
logger.debug(
f"no custom run is {no_custom} or only custom is {only_custom}... not running custom setup script"
)
if test:
print(home_dir)