-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.py
executable file
·91 lines (71 loc) · 2.57 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
#!/usr/bin/env python
"""Install script."""
from __future__ import print_function
from argparse import ArgumentParser
from glob import glob
from os import path
from subprocess import call
# User Configurations
NON_TOPICS = ['bin']
TOPIC_PRIORITY = ['install']
DEFAULT_CONFIG = 'client.arch'
# Dotbot Configurations
DOTBOT = 'install/dotbot/bin/dotbot'
PLUGINS = ['-p', 'install/dotbot-yaourt/yaourt.py']
# Path Processing
BASE_DIR = path.dirname(path.realpath(__file__))
DOTBOT = path.join(BASE_DIR, DOTBOT)
def main():
"""Main logic."""
parser = ArgumentParser()
parser.add_argument('profile', type=str, default=DEFAULT_CONFIG)
args = parser.parse_args()
configs = collect(args.profile)
final_config = path.join(BASE_DIR, 'final.' + args.profile + '.yaml')
combine(configs, final_config)
install(final_config)
def collect(profile):
"""Collect all configuration files belong to a profile."""
configs = []
for topic in get_topics():
configs += get_configs(topic, profile)
return configs
def get_topics():
"""Collect all topics."""
# Start with all subdirectories
topics = [path.basename(p)
for p in glob(path.join(BASE_DIR, '*')) if path.isdir(p)]
# Remove non-topics and topic priority
topics = [t for t in topics if t not in (NON_TOPICS + TOPIC_PRIORITY)]
# Sort in order
topics = TOPIC_PRIORITY + sorted(topics)
return topics
def get_configs(topic, profile):
"""Collect all configuration files belong to a topic of a profile."""
print('Process', topic)
configs = []
while profile:
to_consider = path.join(BASE_DIR, topic, profile + '.yaml')
if path.exists(to_consider):
print('Add', path.basename(to_consider))
configs.insert(0, to_consider)
profile = profile[:profile.rfind('.')]
print()
return configs
def combine(configs, final_config):
"""Combine individual configuration files into a single final one."""
with open(final_config, 'w+') as fc:
for config in configs:
with open(config, 'r') as c:
# Comment the origin of following configurations
fc.write('# ' + path.relpath(config, BASE_DIR) + '\n')
# Copy the configurations
fc.writelines(c.readlines())
fc.write('\n')
print('Combine into', path.basename(final_config))
def install(config):
"""Use Dotbot to install according to configuration."""
print('Install')
call([DOTBOT, '-d', BASE_DIR] + PLUGINS + ['-c', config])
if __name__ == '__main__':
main()