Debian Codegraph @

scripts/test-matrix.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # coding: utf-8
3
4 # Copyright ⓒ 2016 Daniel Keep.
5 #
6 # Licensed under the MIT license (see LICENSE or <http://opensource.org
7 # /licenses/MIT>) or the Apache License, Version 2.0 (see LICENSE of
8 # <http://www.apache.org/licenses/LICENSE-2.0>), at your option. All
9 # files in the project carrying such notice may not be copied, modified,
10 # or distributed except according to those terms.
11
12 import os.path
13 import re
14 import sys
15 import yaml
16
17 from common import *
18 from itertools import chain
19
20 LOG_DIR = os.path.join('local', 'tests')
21
22 set_toolbox_trace(env_var='TRACE_TEST_MATRIX')
23
24 def main():
25 load_globals_from_metadata('test-matrix', globals(),
26 {
27 'LOG_DIR',
28 })
29
30 travis = yaml.load(open('.travis.yml'))
31 script = translate_script(travis.get('script', None))
32 default_rust_vers = travis['rust']
33
34 matrix_includes = travis.get('matrix', {}).get('include', [])
35
36 vers = set(default_rust_vers)
37 include_vers = []
38 exclude_vers = set()
39
40 if not os.path.exists(LOG_DIR):
41 os.makedirs(LOG_DIR)
42
43 for arg in sys.argv[1:]:
44 if arg in vers and arg not in include_vers:
45 include_vers.append(arg)
46 elif arg.startswith('-') and arg[1:] in vers:
47 exclude_vers.add(arg[1:])
48 else:
49 msg("Don't know how to deal with argument `%s`." % arg)
50 sys.exit(1)
51
52 if include_vers == []:
53 include_vers = default_rust_vers[:]
54
55 rust_vers = [v for v in include_vers if v not in exclude_vers]
56 msg('Tests will be run for: %s' % ', '.join(rust_vers))
57
58 results = []
59 for rust_ver in rust_vers:
60 seq_id = 0
61 for env_var_str in travis.get('env', [""]):
62 env_vars = parse_env_vars(env_var_str)
63 for row in chain([{}], matrix_includes):
64 if row.get('rust', None) not in (None, rust_ver):
65 continue
66
67 row_env_vars = parse_env_vars(row.get('env', ""))
68
69 cmd_env = {}
70 cmd_env.update(env_vars)
71 cmd_env.update(row_env_vars)
72
73 success = run_script(script, rust_ver, seq_id, cmd_env)
74 results.append((rust_ver, seq_id, success))
75 seq_id += 1
76
77 print("")
78
79 msg('Results:')
80 for rust_ver, seq_id, success in results:
81 msg('%s #%d: %s' % (rust_ver, seq_id, 'OK' if success else 'Failed!'))
82
83 def parse_env_vars(s):
84 env_vars = {}
85 for m in re.finditer(r"""([A-Za-z0-9_]+)=(?:"([^"]+)"|(\S*))""", s.strip()):
86 k = m.group(1)
87 v = m.group(2) or m.group(3)
88 env_vars[k] = v
89 return env_vars
90
91 def run_script(script, rust_ver, seq_id, env):
92 target_dir = os.path.join('target', '%s-%d' % (rust_ver, seq_id))
93 log_path = os.path.join(LOG_DIR, '%s-%d.log' % (rust_ver, seq_id))
94 log_file = open(log_path, 'wt')
95 msg('Running tests for %s #%d...' % (rust_ver, seq_id))
96 success = True
97
98 def sub_env(m):
99 name = m.group(1) or m.group(2)
100 return cmd_env[name]
101
102 log_file.write('# %s #%d\n' % (rust_ver, seq_id))
103 for k, v in env.items():
104 log_file.write('# %s=%r\n' % (k, v))
105
106 cmd_env = os.environ.copy()
107 cmd_env['CARGO_TARGET_DIR'] = target_dir
108 cmd_env.update(env)
109
110 for cmd in script:
111 cmd = re.sub(r"\$(?:([A-Za-z0-9_]+)|{([A-Za-z0-9_]+)})\b", sub_env, cmd)
112 cmd_str = '> %s run %s %s' % (RUSTUP, rust_ver, cmd)
113 log_file.write(cmd_str)
114 log_file.write("\n")
115 log_file.flush()
116 success = sh(
117 '%s run %s %s' % (RUSTUP, rust_ver, cmd),
118 checked=False,
119 stdout=log_file, stderr=log_file,
120 env=cmd_env,
121 )
122 if not success:
123 log_file.write('Command failed.\n')
124 log_file.flush()
125 break
126 msg('... ', 'OK' if success else 'Failed!')
127 log_file.close()
128 return success
129
130 def translate_script(script):
131 script = script or "rustc -vV && cargo -vV && cargo build --verbose && cargo test --verbose"
132 parts = script.split("&&")
133 return [p.strip() for p in parts]
134
135 if __name__ == '__main__':
136 main()