/
deploy.py
executable file
·78 lines (68 loc) · 2.22 KB
/
deploy.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
#!/usr/bin/env python3
import logging
import os
import subprocess
import sys
IGNORE = [
'.git',
'.vim',
'deploy.cmd',
'vendor',
os.path.basename(__file__),
]
SKIP_LINK = [
'.irssi/config',
]
def main():
base_dir = os.path.dirname(os.path.abspath(__file__))
for root, dirs, files in os.walk(base_dir):
_, _, rel_root = root.partition(base_dir)
rel_root = rel_root.strip('/')
if any(_ in rel_root.split(os.sep) for _ in IGNORE):
continue
for file_name in files:
if file_name in IGNORE:
continue
rel_path = os.path.join(rel_root, file_name)
logging.debug(rel_path)
make_link(rel_path)
def make_link(rel_path):
base_dir = os.path.dirname(os.path.abspath(__file__))
source_path = os.path.join(base_dir, rel_path)
target_path = os.path.join(os.path.expanduser('~'), rel_path)
target_dir = os.path.dirname(target_path)
if os.path.isdir(target_path):
logging.info("`{}' is a directory".format(target_path))
return
if os.path.isfile(target_path):
if os.path.islink(target_path):
link_path = os.path.join(target_dir, os.readlink(target_path))
if link_path == source_path:
return
logging.info("`{}' is a symbolic link to {}".format(
target_path, link_path))
else:
if open(source_path, 'rb').read() != open(target_path, 'rb').read():
vimdiff(source_path, target_path)
if rel_path in SKIP_LINK:
return
while True:
ans = input("Keep? (Y/n) ").strip()
if not ans or ans in 'yY':
return
if ans in 'nN':
break
os.unlink(target_path)
if not os.path.exists(target_dir):
os.makedirs(target_dir, exist_ok=True)
os.symlink(source_path, target_path)
def vimdiff(a, b):
cmd = []
if sys.platform in ['win32']:
cmd += ['C:\\Program Files (x86)\\Vim\\vim73\\gvim.exe', '-d']
else:
cmd += ['vimdiff']
subprocess.check_call(cmd + [a, b])
if __name__ == '__main__':
logging.basicConfig(level=logging.INFO)
main()