-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsymlink_setup.py
executable file
·71 lines (61 loc) · 2 KB
/
symlink_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
#!/usr/bin/python
"""Setup dotfiles symlinks.
Mirror directory tree of the git repositroy based on
links_{UNAME}.json
all links are relative from $HOME.
"""
import shutil
import os
import json
import platform
def copytree(src, dst, symlinks=False):
names = os.listdir(src)
os.makedirs(dst, exist_ok=True)
errors = []
for name in names:
srcname = os.path.join(src, name)
dstname = os.path.join(dst, name)
try:
if symlinks and os.path.islink(srcname):
linkto = os.readlink(srcname)
os.symlink(linkto, dstname)
elif os.path.isdir(srcname):
copytree(srcname, dstname, symlinks)
else:
os.symlink(srcname, dstname)
except FileExistsError:
pass
except OSError as why:
errors.append((srcname, dstname, str(why)))
except Error as err:
errors.extend(err.args[0])
try:
shutil.copystat(src, dst)
except OSError as why:
errors.extend((src, dst, str(why)))
if errors:
raise Exception(errors)
def unlink_all(directory):
for root, _, files in os.walk(directory):
for f in files:
os.unlink(os.path.join(os.path.abspath(root), f))
def main(cfg, home, current):
for item in cfg['symlinks']:
src_dir = current + item['from']
dest_dir = home + item['to']
try:
if src_dir == "root":
unlink_all(dest_dir)
print('{:45} \u2192 {}'.format(src_dir, dest_dir))
copytree(src_dir, dest_dir, symlinks=True)
except Exception as error:
print('Failed to setup symlink: {}'.format(error))
if __name__ == '__main__':
# Base path to symlink from
# All paths are based from $HOME
HOME = os.environ['HOME'] + '/'
CURRENT = os.getcwd() + '/'
CFG = 'links_%s.json' % platform.uname().system
with open(CFG) as data_file:
JSON_CFG = json.load(data_file)
main(JSON_CFG, HOME, CURRENT)