-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.py
executable file
·115 lines (95 loc) · 2.83 KB
/
build.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
#!/usr/bin/env python
'''
auto
'''
from __future__ import print_function
import os
SITE_ROOT = 'docs/'
TMPLATE_DIR = 'template/'
LIST_DIR = 'source/'
LIST_EXT = '.txt'
def read_list(fname):
'''
read source list
@return dict
'''
with open(fname) as infile:
source_list = {}
for line in infile:
line = line.strip()
if not line or line.startswith('#'):
continue
data = line.split('#', 1)
url = data[0].strip()
if len(data) == 1:
name = url.rstrip('/').rstrip('.cn').rstrip('.edu')
name = name.rstrip('.org').rsplit('.', 1)[-1]
else:
name = data[1].strip()
name = name.split(None, 1)[0].split('#,:', 1)[0].strip()
if name in source_list:
exit('ERROR: too many source named "{0}"\n\t{1}\n\t{2}'.format(
name, url, source_list[name]))
else:
source_list[name] = url
return source_list
def read_template(fname):
'''
read template file
'''
with open(fname, 'r') as template:
return template.read()
def save(fname, data):
'''
save file
'''
fname = SITE_ROOT + fname
path = os.path.dirname(fname)
if path and (not os.path.exists(path)):
os.makedirs(path)
with open(fname, 'w') as out:
print('save to: ', fname)
out.write(data)
def generate_bash(name, prefix):
'''
generate source file for docker
save scripts to files
return script with speed testing
'''
slist = read_list(LIST_DIR + prefix + '/' + name + LIST_EXT)
template_source = read_template(TMPLATE_DIR + name + '.sh')
lst = []
for (key, url) in slist.items():
lst.append("'{0}' #{1}".format(url, key))
script = template_source.replace('${FAST_SRC}', url)
output_file = '{0}/{1}/{2}.sh'.format(prefix, name, key)
save(output_file, script)
source_list = "\n\t".join(lst)
template_speed = read_template(TMPLATE_DIR + 'speed.sh')
script = template_speed.replace('__SOURCE_LIST__', source_list)
script += template_source
save(prefix + '/' + name + '.sh', script)
return script
def generate():
'''
auto generate from input folder
'''
for folder in os.listdir(LIST_DIR):
# print(folder)
path = os.path.join(LIST_DIR, folder)
if not os.path.isdir(path):
continue
script = ''
for name in os.listdir(path):
name, ext = os.path.splitext(name)
if ext != LIST_EXT:
continue
script += generate_bash(name, folder)
save(os.path.join(folder, 'all.sh'), script)
# def main():
# '''
# entry
# '''
# generate('source/')
if __name__ == '__main__':
generate()