-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmpt.py
467 lines (438 loc) · 19.6 KB
/
mpt.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Copyright (c) 2024 Jianshan Jiang
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import argparse
import logging
import os
import re
import shlex
import subprocess
import sys
import yaml
from collections import deque
from datetime import datetime
from rich import print
from rich.console import Console
from rich.logging import RichHandler
from rich.pretty import pprint
from rich.table import Table
from rich.theme import Theme
from rich.tree import Tree as RichTree
from rich.traceback import install
from typing import List
from yaml import SafeDumper
from graphlib import TopologicalSorter, CycleError
def get_dependencies(pkg_with_step, deps, rich_tree):
"""
Build dependencies tree recursively accoding to config.yaml on each package
"""
if ':' in pkg_with_step:
pkg = pkg_with_step.split(':')[0]
pkg_step = pkg_with_step.split(':')[1]
else:
pkg = pkg_with_step
pkg_step = 'a'
conf_file = os.path.join(pkgs_dir, pkg, "config.yaml")
with open(conf_file, 'r', newline='', encoding="utf-8") as f:
pkg_conf = yaml.safe_load(f)
if pkg_conf['steps'][pkg_step]['dependencies']:
if pkg_with_step not in deps:
deps[pkg_with_step] = []
for dep_with_step in pkg_conf['steps'][pkg_step]['dependencies']:
if ':' in dep_with_step:
dep = dep_with_step.split(':')[0]
dep_step = dep_with_step.split(':')[1]
else:
dep = dep_with_step
dep_step = 'a'
if dep_with_step not in deps[pkg_with_step]:
deps[pkg_with_step].append(dep_with_step)
child_tree = rich_tree.add(dep_with_step, guide_style="bold bright_blue")
get_dependencies(dep_with_step, deps, child_tree)
def create_dirs(folders):
"""
Create folders according to input list
"""
for name in folders:
folder = os.path.join(root_dir, name)
if not os.path.exists(folder):
os.makedirs(folder)
def get_newer_files(path, ref_time, matched, file_types=['.exe','.dll', '.lib']):
"""
Check whether has newer files with filter
"""
with os.scandir(path) as it:
for entry in it:
if not entry.name.startswith('.'):
if entry.is_symlink():
continue
elif entry.is_file():
file_name = entry.name
file_ext = os.path.splitext(file_name)[1]
if file_ext in file_types:
last_modified = datetime.fromtimestamp(entry.stat().st_mtime)
if last_modified > ref_time:
matched.append(file_name)
elif entry.is_dir():
get_newer_files(entry.path, ref_time, matched, file_types)
return matched
def configure_envars(proc_env, arch, pkg):
"""
Configure environment variables that will be used for each packages
"""
logger.debug(f"Configuring environment variables for package '{pkg}'")
proc_env['ARCH'] = arch
proc_env['ROOT_DIR'] = root_dir
prefix = os.path.join(root_dir, arch)
prefix_path = os.path.join(root_dir, arch)
# PREFIX_PATH
if settings_conf:
if arch in settings_conf['prefix'].keys() and settings_conf['prefix'][arch]:
for p in settings_conf['prefix'][arch].keys():
pkg_prefix = settings_conf['prefix'][arch][p]
if p == pkg:
prefix = pkg_prefix
prefix_env = p.replace('-','_').upper() + '_PREFIX'
proc_env[prefix_env] = pkg_prefix
if pkg_prefix not in prefix_path:
prefix_path += os.pathsep + pkg_prefix
# bin
bin_path = proc_env['PATH']
for p in prefix_path.split(os.pathsep):
_p = os.path.join(p, 'bin')
if os.path.exists(_p) and (_p not in bin_path):
bin_path += os.pathsep + _p
proc_env['PATH'] = bin_path
proc_env['PREFIX_PATH'] = prefix_path
proc_env['PREFIX'] = prefix
return prefix
def execute(proc_env, commands, shell=False):
"""
"""
err_list = ['Cannot', 'No such file', 'Error:', 'error:', ': error', ' Error ', 'syntax error', 'Failed', 'FAILED:', 'fatal:', 'fatal error']
warn_list = ['Warning:', 'warning:', ': warning', ' Warning ']
p = subprocess.Popen(commands, shell=shell, env=proc_env,
stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
with p.stdout:
for line in iter(p.stdout.readline, b''):
stdout_line = line.decode('utf-8', 'ignore').rstrip()
if any(ele in stdout_line for ele in warn_list):
logger.warning(stdout_line)
elif any(ele in stdout_line for ele in err_list):
logger.error(stdout_line)
else:
logger.info(stdout_line)
if p.poll() is not None:
break
exit_code = p.wait()
logger.debug(f"Process exit code: {exit_code}")
return exit_code
def run_script(proc_env, pkgs_dir, pkg, file_name):
"""
Run script .bat or .sh from config.yaml that has been defined in the package
"""
os.chdir(os.path.join(pkgs_dir, pkg))
result = False
script = os.path.join(pkgs_dir, pkg, file_name)
if os.path.exists(script):
if file_name.endswith('.bat'):
commands = [file_name]
elif file_name.endswith('.sh'):
commands = ['C:/Program Files/Git/bin/bash.exe', file_name]
if execute(proc_env, commands, False):
logger.error(f"Build {pkg} failed")
else:
result = True
os.chdir(root_dir)
return result
def compare_version(current, previous):
"""
"""
if '.' in current and '.' in previous:
v1 = list(map(int, current.split('.')))
v2 = list(map(int, previous.split('.')))
elif '-' in current and '-' in previous:
v1 = list(map(int, current.split('-')))
v2 = list(map(int, previous.split('-')))
elif '_' in current and '_' in previous:
v1 = list(map(int, current.split('_')))
v2 = list(map(int, previous.split('_')))
else:
v1 = current
v2 = previous
if v1 > v2:
return 1
elif v1 < v2:
return -1
else:
return 0
def build_decision(arch, deps, pkg_with_step, pkg_ver, pkg_url, installed_conf):
"""
"""
if ':' in pkg_with_step:
pkg = pkg_with_step.split(':')[0]
pkg_step = pkg_with_step.split(':')[1]
else:
pkg = pkg_with_step
pkg_step = 'a'
logger.debug(f"Checking build decision for package '{pkg}'")
if not installed_conf:
logger.debug(f"Whole packages were not built yet")
return True
elif arch not in installed_conf.keys():
logger.debug(f"ARCH '{arch}' was not built yet")
return True
elif not pkg in installed_conf[arch].keys():
logger.debug(f"Package '{pkg}' was not built yet")
return True
elif 'step' in installed_conf[arch][pkg]:
pkg_built_step = installed_conf[arch][pkg]['step']
if pkg_built_step < pkg_step:
logger.debug(f"Current step of {pkg} wasn't built yet")
return True
else:
matched = []
installed_ver = str(installed_conf[arch][pkg]['version'])
pkg_built_time = installed_conf[arch][pkg]['built']
logger.debug(f"{'Built time' : <29}: {pkg_built_time}")
if compare_version(pkg_ver, installed_ver) > 0:
logger.debug(f"There is a newer version of package '{pkg}'")
return True
if get_newer_files(os.path.join(pkgs_dir, pkg), pkg_built_time, matched, file_types=['.diff', '.yaml', '.sh', '.bat']):
logger.debug(f"There are newer files exist in package directory")
return True
if pkg_url.endswith('.git'):
src_dir = os.path.join(root_dir, 'releases', pkg)
if get_newer_files(src_dir, pkg_built_time, matched, file_types=['.c', '.cc', '.cpp', '.h', '.hpp', '.f']):
logger.debug(f"There are newer files exist in source directory")
return True
if pkg_with_step in deps and deps[pkg_with_step]:
for dep in deps[pkg_with_step]:
if dep in installed_conf[arch].keys():
dep_built_time = installed_conf[arch][dep]['built']
if dep_built_time > pkg_built_time:
logger.debug(f"The built time {dep_built_time} of dependency '{dep}' is newer than package '{pkg}'")
return True
return False
def build_pkg(arch, deps, pkg_with_step):
"""
"""
success = True
if ':' in pkg_with_step:
pkg = pkg_with_step.split(':')[0]
pkg_step = pkg_with_step.split(':')[1]
else:
pkg = pkg_with_step
pkg_step = 'a'
conf_file = os.path.join(pkgs_dir, pkg, "config.yaml")
with open(conf_file, 'r', newline='', encoding="utf-8") as f:
pkg_conf = yaml.safe_load(f)
pkg_ver = str(pkg_conf['version'])
pkg_url = pkg_conf['url']
proc_env = os.environ.copy()
prefix = configure_envars(proc_env, arch, pkg)
if run_script(proc_env, pkgs_dir, pkg, 'sync.sh'):
installed_conf = {}
installed_file = os.path.join(root_dir, "installed.yaml")
if os.path.exists(installed_file):
with open(installed_file, 'r', newline='', encoding="utf-8") as g:
installed_conf = yaml.safe_load(g)
if pkg_conf['steps'][pkg_step]['run']:
logger.debug(f"Build step '{pkg_step}' for '{pkg}'")
if build_decision(arch, deps, pkg_with_step, pkg_ver, pkg_url, installed_conf):
logger.debug(f"{'Decide to build ' + pkg : <29}: yes")
start_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
logger.debug(f"{'Start time of build' : <29}: {start_time}")
log_file = os.path.join(root_dir, 'logs', pkg+'.txt')
file_handler = logging.FileHandler(log_file, mode='w', encoding='utf-8')
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)
if run_script(proc_env, pkgs_dir, pkg, pkg_conf['steps'][pkg_step]['run']):
logger.debug(f"Excute step '{pkg_step}' of '{pkg}' was success")
if arch not in installed_conf.keys():
installed_conf[arch] = {}
if pkg not in installed_conf[arch]:
installed_conf[arch][pkg] = {}
installed_conf[arch][pkg]['version'] = pkg_conf['version']
installed_conf[arch][pkg]['built'] = datetime.now()
if len(pkg_conf['steps'].keys()) > 1:
installed_conf[arch][pkg]['step'] = pkg_step
with open(installed_file, 'w', newline='', encoding='utf-8') as g:
logger.debug(f"Updating installed.yaml")
yaml.safe_dump(installed_conf, g, indent=2)
elif arch in installed_conf.keys():
if pkg in installed_conf[arch]:
if len(pkg_conf['steps'].keys()) > 1:
if 'step' in installed_conf[arch][pkg]:
built_step = installed_conf[arch][pkg]['step']
if built_step == pkg_step:
logger.debug(f"Delete installed information of {pkg}")
del installed_conf[arch][pkg]
else:
logger.debug(f"Delete installed information of {pkg}")
del installed_conf[arch][pkg]
with open(installed_file, 'w', newline='', encoding='utf-8') as g:
logger.debug(f"Updating installed.yaml")
yaml.safe_dump(installed_conf, g, indent=2)
logger.debug(f"Stop further build because build '{pkg}' failed")
success = False
end_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
logger.removeHandler(file_handler)
file_handler.close()
logger.debug(f"{'Finish time of build' : <29}: {end_time}")
else:
logger.debug(f"{'Decide to build ' + pkg : <29}: no")
else:
logger.debug(f"Stop because of package '{pkg}' was synchronized fail")
# NOTE: don't set success to False, because it will prevent to build other dependencies
# success = False
return success
def build_pkgs(arch, deps, build_order):
"""
"""
for pkg_with_step in build_order:
if not build_pkg(arch, deps, pkg_with_step):
break
def traverse_pkgs(arch, pkgs):
"""
"""
for pkg in pkgs:
conf_file = os.path.join(pkgs_dir, pkg, "config.yaml")
with open(conf_file, 'r', newline='', encoding="utf-8") as f:
pkg_conf = yaml.safe_load(f)
# NOTE: here use pkg_name but not pkg, because pkg maybe as parameter input
# from command line is all lower case
pkg_name = pkg_conf['name']
for step in pkg_conf['steps']:
deps = {}
pkg_with_step = pkg_name
if step > 'a':
pkg_with_step += ':' + step
rich_tree = RichTree(pkg_with_step, guide_style="bold bright_blue")
get_dependencies(pkg_with_step, deps, rich_tree)
logger.debug(f"Dependencies tree of package '{pkg_name}' on step '{step}'")
console.print(rich_tree)
try:
sorter = TopologicalSorter(deps)
build_order = tuple(sorter.static_order())
if not build_order:
# Only have one node in graph
build_order = tuple([pkg_with_step])
logger.debug(f"Build order: {build_order}")
build_pkgs(arch, deps, build_order)
except CycleError as e:
logger.error(f"Cycle detected: {e}")
logger.debug(f"You can fix this error in config.yaml of package {e[0]} and {e[1]}")
break
def list_pkgs(arch):
"""
"""
i = 1
installed_conf = {}
installed_file = os.path.join(root_dir, "installed.yaml")
if os.path.exists(installed_file):
with open(installed_file, 'r', newline='', encoding="utf-8") as f:
installed_conf = yaml.safe_load(f)
table = Table(title="Summary of avaiable packages")
table.add_column("No.", justify="left", style="cyan", no_wrap=True)
table.add_column("Name", style="magenta")
table.add_column("Version", style="green")
table.add_column("URL", style="magenta")
table.add_column("Installed", justify="left", style="green")
for pkg in os.listdir(pkgs_dir):
if pkg == 'gnulib':
continue
conf_file = os.path.join(pkgs_dir, pkg, "config.yaml")
with open(conf_file, 'r', newline='', encoding="utf-8") as f:
pkg_conf = yaml.safe_load(f)
pkg_name = pkg_conf['name']
pkg_ver = str(pkg_conf['version'])
pkg_url = pkg_conf['url']
is_installed = '[bold red]No'
if arch in installed_conf.keys():
if pkg in installed_conf[arch].keys():
installed_ver = str(installed_conf[arch][pkg]['version'])
if pkg_ver == installed_ver:
is_installed = 'Yes'
if is_installed != 'Yes':
table.add_row(str(i), '[bold red]'+pkg_name, '[bold red]'+pkg_ver, '[bold red]'+pkg_url, is_installed)
else:
table.add_row(str(i), pkg_name, pkg_ver, pkg_url, is_installed)
i += 1
console.print(table)
SafeDumper.add_representer(
type(None),
lambda dumper, value: dumper.represent_scalar(u'tag:yaml.org,2002:null', '')
)
root_dir = os.getcwd()
pkgs_dir = os.path.join(root_dir, 'packages')
console = Console()
logger = logging.getLogger('rich')
logger.setLevel(logging.DEBUG)
log_format = "%(message)s"
formatter = logging.Formatter(log_format)
for h in logger.handlers[:]:
logger.removeHandler(h)
h.close()
console_handler = RichHandler(show_level=True, show_path=False,
show_time=False, rich_tracebacks=True, console=console)
console_handler.setFormatter(formatter)
logger.addHandler(console_handler)
install(console=console, show_locals=True)
settings_file = os.path.join(root_dir, "settings.yaml")
settings_conf = {}
if os.path.exists(settings_file):
with open(settings_file, 'r', newline='', encoding="utf-8") as f:
settings_conf = yaml.safe_load(f)
if __name__ == '__main__':
pkgs = []
parser = argparse.ArgumentParser(usage='mpt --help', add_help=False)
parser.add_argument('--list', dest='list', default=None, action='store_true')
# Parse the given arguments. Don't signal an error if non-option arguments
# occur between or after options.
cmdargs, unhandled = parser.parse_known_args()
# Report unhandled arguments.
arch = 'x64'
for arg in unhandled:
if arg.startswith('-'):
message = '%s: Unrecognized option \'%s\'.\n' % ('mpt', arg)
message += 'Try \'mpt --help\' for more information.\n'
sys.stderr.write(message)
sys.exit(1)
elif (arg == 'x86') or (arg == 'x64'):
if pkgs:
message = '%s: \'%s must be the first arguments\'.\n' % ('mpt', arg)
message += 'Try \'mpt --help\' for more information.\n'
sys.stderr.write(message)
sys.exit(1)
else:
arch = arg
else:
pkgs.append(arg)
if cmdargs.list:
list_pkgs(arch)
else:
create_dirs(['logs', 'releases', 'tags'])
if not pkgs:
for name in os.listdir(pkgs_dir):
pkgs.append(name)
traverse_pkgs(arch, pkgs)