-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_setup.py
executable file
·97 lines (81 loc) · 2.42 KB
/
_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
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
#!/usr/bin/env python3
import os
import shutil
import sys
homedir = os.environ["HOME"]
confdir = os.path.realpath(os.path.dirname(sys.argv[0]))
links = {
".bash_login": "/dev/null",
".bash_logout": "/dev/null",
".bash_profile": "bash/main.sh",
".bashrc": "bash/main.sh",
".dir_colors": "coreutils/dircolors-solarized/dircolors.256dark",
".gdbinit": "gdb/gdbinit",
".gitconfig": "git/config",
".gitignore": "git/ignore",
".hgrc": "hg/config",
".config/htop/htoprc": "htop/htoprc",
".lldbinit": "lldb/lldbinit",
".tmux.conf": "tmux/tmux.conf",
".vim/pack": "vim/pack",
".vimrc": "vim/vimrc",
}
def _delete_path(path):
for f in [ os.unlink, shutil.rmtree ]:
try:
f(path)
return
except OSError:
continue
def _conf_path(path):
if path[0] == '/':
return path
else:
return os.path.join(confdir, path)
def _home_path(path):
return os.path.join(homedir, path)
def links_create(force):
for f in links:
dst = _home_path(f)
src = _conf_path(links[f])
if os.path.islink(dst) and os.readlink(dst) == src:
continue
if not os.path.exists(os.path.dirname(dst)):
os.makedirs(os.path.dirname(dst))
done = False
try:
os.symlink(src, dst)
done = True
except FileExistsError:
if force:
_delete_path(dst)
os.symlink(src, dst)
done = True
finally:
print("[%s] %s -> %s" % ("DONE" if done else "SKIP", dst, src))
def links_remove():
for f in links:
path = _home_path(f)
print("removing %s" % path)
_delete_path(_home_path(path))
def links_show():
for f in links:
path = _home_path(f)
print("%s" % path)
def dump_help():
global cmd_map
for e in cmd_map:
print(e)
cmd_map = {
"links-create": lambda: links_create(False),
"links-create-force": lambda: links_create(True),
"links-remove": links_remove,
"links-show": links_show,
"help": dump_help,
}
if __name__ == "__main__":
if len(sys.argv) == 1:
command = "links-create"
else:
command = sys.argv[1]
cmd_map[command]()