-
Notifications
You must be signed in to change notification settings - Fork 3
/
rc.py
executable file
·126 lines (105 loc) · 3.09 KB
/
rc.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
#!/usr/bin/env python
from __future__ import print_function
import os
import sys
import shutil
from time import gmtime, strftime
HOME = os.environ['HOME']
RCHOME = os.path.join(HOME, '.rc')
backup_dir = os.path.join(RCHOME, '.backup' + strftime("_%b%d_%H:%M:%S", gmtime()))
app_config = {
'bash': {
'$HOME/.bashrc': '$RCHOME/shell/bashrc'
},
'bin': {
'$HOME/.bin': '$RCHOME/bin'
},
'curl': {
'$HOME/.curlrc': '$RCHOME/curl/curlrc'
},
'git': {
'$HOME/.gitconfig': '$RCHOME/git/gitconfig',
'$HOME/.config/git/ignore': '$RCHOME/git/ignore'
},
'R': {
'$HOME/.Renviron': '$RCHOME/R/Renviron'
},
'tmux': {
'$HOME/.tmux.conf': '$RCHOME/tmux/tmux.conf'
},
'vim': {
'$HOME/.vimrc': '$RCHOME/vim/vimrc',
'$HOME/.vim': '$RCHOME/vim'
},
'zsh': {
"$HOME/.zshrc": "$RCHOME/shell/zshrc"
},
}
def remove(path):
found = False
if os.path.islink(path):
print(path, "link has already been there, just delete it.")
os.remove(path)
found = True
return
elif os.path.isfile(path):
print(path, "file has already been there.")
found = True
elif os.path.isdir(path):
print(path, "folder has already been there.")
found = True
else:
print(path, "not found.")
if found:
print("move it to", backup_dir)
if not os.path.isdir(backup_dir):
os.makedirs(backup_dir)
shutil.move(path, backup_dir)
def is_installed(app):
"""Check if a given config is installed"""
if app not in app_config.keys():
return False
for path, rcfile in app_config[app].items():
path = path.replace('$HOME', HOME)
rcfile = rcfile.replace('$RCHOME', RCHOME)
if os.path.realpath(path) != rcfile:
return False
return True
def install(apps):
"""Install configs"""
for app in apps:
if is_installed(app):
print(app, 'is already installed, skip.')
continue
if app in app_config.keys():
print("configuring ", app)
else:
print(app, 'configuration not found, continuing...')
continue
for path,rcfile in app_config[app].items():
path = path.replace('$HOME', HOME)
rcfile = rcfile.replace('$RCHOME', RCHOME)
remove(path)
try:
os.makedirs(os.path.dirname(path))
except OSError:
pass
os.symlink(rcfile, path)
def check_status(apps):
for app in apps:
installed = is_installed(app)
print('%15s is%s installed' % (app, ' not' if not installed else ''))
def main():
if len(sys.argv) < 2:
sys.exit("rc.py status|install[ all|<app list>]")
if sys.argv[1] in ('install', 'i'):
if sys.argv[2] == 'all':
apps = app_config.keys()
else:
apps = sys.argv[1:]
install(apps)
elif sys.argv[1] in ('status', 's'):
apps = app_config.keys()
check_status(apps)
if __name__ == '__main__':
main()