-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.py
executable file
·123 lines (99 loc) · 3.34 KB
/
install.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
#!/usr/bin/env python
import sys
import os
import logging
if sys.version_info[0] == 3:
import configparser
else:
import ConfigParser as configparser
logging.basicConfig(level=logging.INFO)
FILEPATH = os.path.abspath(sys.argv[0])
PATH = os.path.dirname(FILEPATH)
CONF_PATH = os.path.join(PATH, 'install.conf')
COMPONENT_PATH = os.path.join(PATH, 'components')
def test_install(target_paths):
is_success = True
for from_path, to_path in target_paths:
if not os.path.exists(from_path):
logging.error('No such file: {0}'.format(from_path))
is_success = False
if os.path.exists(to_path):
if not os.path.islink(to_path):
logging.error('The file exists(not symbolic link): {0}'.format(to_path))
is_success = False
return is_success
def install(target_paths, is_debug=False):
for from_path, to_path in target_paths:
logging.info('installing {f} to {t}'.format(
f=from_path,
t=to_path,
))
if not is_debug:
if os.path.islink(to_path):
os.remove(to_path)
to_dir = os.path.dirname(to_path)
if not os.path.exists(to_dir):
os.makedirs(to_dir)
os.symlink(from_path, to_path)
def get_components(conf, profile_name):
return [
component
for component in
conf.get('PROFILES', profile_name).splitlines()
if component
]
def parse_args(conf):
profile_names = conf.options('PROFILES')
component_names = sorted(set(conf.sections()) - set('PROFILES'))
try:
import argparse
is_argparse = True
except:
is_argparse = False
if is_argparse:
parser = argparse.ArgumentParser()
parser.add_argument('profile', choices=profile_names)
parser.add_argument('components', nargs='*', choices=[[]] + component_names)
parser.add_argument('--debug', action='store_true')
return parser.parse_args()
else:
if len(sys.argv) < 2:
print('\n'.join([
"Usage: {cmd} profile",
" profiles: {profiles}"
]).format(cmd=sys.argv[0], profiles=', '.join(profile_names))
)
class Args:
def __init__(self, profile):
self.profile = profile
self.components = []
self.debug = False
profile = sys.argv[1]
if profile in profile_names:
args = Args(profile)
return args
def main():
conf = configparser.ConfigParser()
conf.read(CONF_PATH)
args = parse_args(conf)
components = get_components(conf, args.profile) + args.components
raw_paths = []
for component in components:
raw_paths += conf.items(component)
target_paths = []
for from_path, to_pathes in raw_paths:
from_path = from_path.format(**{
'path': COMPONENT_PATH,
})
to_pathes = [os.path.expanduser(path)for path in to_pathes.splitlines() if path]
for to_path in to_pathes:
target_paths.append((from_path, to_path))
if test_install(target_paths):
install(target_paths, is_debug=args.debug)
print("")
print("install successed!!!")
print("")
else:
raise BaseException()
if __name__ == '__main__':
main()