-
Notifications
You must be signed in to change notification settings - Fork 4
/
bootstrap.py
executable file
·88 lines (61 loc) · 2.21 KB
/
bootstrap.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
#!/usr/bin/env python
#
# Bootstrap script
# ===========================================
#
# Jose Galarza <[email protected]>
#
import glob
import os
import subprocess
# Path to this file
SELF_PATH = os.path.dirname(os.path.abspath(__file__))
def create_symlinks(source_extension, target_folder, *, hidden=False, extension=None):
created_symlinks = []
symlinks = glob.glob('*/' + source_extension)
always_overwrite = False
answer = 'n'
for symlink in symlinks:
origin = os.path.join(SELF_PATH, symlink)
filename = os.path.splitext(os.path.basename(symlink))[0]
if hidden:
filename = '.' + filename
destine = os.path.expanduser(os.path.join(target_folder, filename))
if extension:
destine = destine + '.' + extension
if os.path.lexists(destine):
# Ask the user if overwrite the file
if not always_overwrite:
answer = confirm_overwrite(destine)
always_overwrite = answer == 'a'
# Remove the file if the user answered yes
if always_overwrite or answer in ('y', 'a'):
os.remove(destine)
# Keep the file if the user answered no
if answer == 'n':
continue
# Create the symlink
os.symlink(origin, destine)
created_symlinks.append(destine)
return created_symlinks
def run_agents(agents):
for agent in agents:
subprocess.run(['launchctl', 'load', '-F', agent])
def confirm_overwrite(path):
'Ask the user to overwrite a file'
while True:
answer = input('%s exists, overwrite it? (a/y/n) ' % os.path.basename(path))
if answer in ('y', 'n', 'a'):
break
return answer
if __name__ == '__main__':
os.chdir(SELF_PATH)
try:
os.mkdir(os.path.expanduser('~/.config'))
except FileExistsError:
pass # The .config dir already exists
create_symlinks('*.symlink', '~', hidden=True)
create_symlinks('*.configsymlink', '~/.config')
agents = create_symlinks('*.launchagent', '~/Library/LaunchAgents', extension='plist')
run_agents(agents)
print("Now, to finish, run the packages/install.sh script")