-
Notifications
You must be signed in to change notification settings - Fork 2
/
go.py
executable file
·189 lines (152 loc) · 4.4 KB
/
go.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
#!/usr/bin/env python2.7
import sys
import subprocess
import getopt
dry_run = True
# DB format: list of <local file>, or 2-tuple of (<local file>, <target file>)
# If only local file is specified, we use it as its target file.
#
# NOTE: absolute paths are not well tested
#
# If local file ends:
# '/': this is a directory (copy recursively, except hidden files)
# If local file starts with:
# '+': create an empty file or directory.
# (beware that existing files are deleted)
# '!': remove the target file, if exists.
common = [
'+.vim/',
'.vim/after/',
'+.vim/bundle/',
'.vim/bundle/Vundle.vim/',
'+.vim/backups/',
'+.vim/swaps/',
'.vimrc',
'.terminfo/',
'.bash_env',
'.bash_prompt',
'.bash_profile',
'.bashrc',
'.dircolors',
'.tmux.conf',
'.gitconfig',
'+.ssh/',
'.ssh/config',
'+.config/',
'+.config/htop/',
'.config/htop/htoprc',
'.ackrc',
'.inputrc',
]
linux = [
'.bash_aliases',
]
osx = [
'.bash_aliases',
'.iterm2/',
'.iterm2-bitmap/',
]
def run_cmd(cmd):
global dry_run
print '\t\tcmd: %s' % cmd
if not dry_run:
subprocess.check_call(cmd, shell=True)
def parse_entry(entry, local_prefix):
flag_dir = False
flag_add = False
flag_del = False # flag_add and flag_del cannot be set at the same time
target = None
if isinstance(entry, tuple):
local, target = entry
else:
local = entry
if local[-1] == '/':
flag_dir = True
local = local[:-1]
if local[0] == '+':
local = local[1:]
flag_add = True
elif local[0] == '!':
local = local[1:]
flag_del = True
if local[0] == '/':
local_path = local
else:
local_path = '%s/%s' % (local_prefix, local)
if target == None:
target = local
if target[0] == '/':
target_path = target
else:
target_path = '%s/%s' % ('~', target)
return local_path, target_path, flag_dir, flag_add, flag_del
def deploy(db, local_prefix):
print 'Processing %s' % local_prefix
for entry in db:
local_path, target_path, flag_dir, flag_add, flag_del = \
parse_entry(entry, local_prefix)
print '\tFile %s' % local_path
if flag_del:
run_cmd('rm -rf %s' % target_path)
elif flag_add:
if flag_dir:
run_cmd('mkdir -p %s' % target_path)
else:
run_cmd('touch %s' % target_path)
else:
if flag_dir:
run_cmd('cp -r %s %s' % (local_path, target_path))
else:
run_cmd('cp %s %s' % (local_path, target_path))
def collect(db, local_prefix):
print 'Processing %s' % local_prefix
run_cmd('rm -rf %s' % local_prefix)
run_cmd('mkdir %s' % local_prefix)
for entry in db:
local_path, target_path, flag_dir, flag_add, flag_del = \
parse_entry(entry, local_prefix)
print '\tFile %s' % local_path
if flag_del:
continue
if flag_add:
run_cmd('rm -rf %s' % local_path)
if flag_dir:
run_cmd('mkdir -p %s' % local_path)
else:
run_cmd('rm -f %s' % local_path)
run_cmd('touch %s' % local_path)
else:
if flag_dir:
run_cmd('cp -r %s %s' % (target_path, local_path))
else:
run_cmd('cp %s %s' % (target_path, local_path))
def print_usage():
print >> sys.stderr, 'Usage: %s [--run] <deploy | collect>' % sys.argv[0]
sys.exit(2)
def main():
global dry_run
opts, args = getopt.gnu_getopt(sys.argv[1:], '', ['run'])
if len(args) != 1 or args[0] not in ['deploy', 'collect']:
print_usage()
for opt, optarg in opts:
if opt == '--run':
dry_run = False
else:
print_usage()
if dry_run:
print 'Running in dry-run mode. Use --run to actually run it'
uname = subprocess.check_output('uname', shell=True).strip()
assert(uname in ['Linux', 'Darwin'])
if args[0] == 'deploy':
func = deploy
elif args[0] == 'collect':
func = collect
else:
assert False
func(common, 'common')
if uname == 'Linux':
func(linux, 'linux')
elif uname == 'Darwin':
func(osx, 'osx')
if __name__ == '__main__':
main()