forked from trinodb/trino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build-matrix-from-impacted.py
executable file
·188 lines (172 loc) · 5.24 KB
/
build-matrix-from-impacted.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
#!/usr/bin/env python3
import argparse
import json
import logging
import sys
import tempfile
import unittest
import yaml
def main():
parser = argparse.ArgumentParser(
description="Filter test matrix modules using list of impacted modules."
)
parser.add_argument(
"-m",
"--matrix",
type=argparse.FileType("r"),
default=".github/test-matrix.yaml",
help="A YAML file with the test matrix",
)
parser.add_argument(
"-i",
"--impacted",
type=argparse.FileType("r"),
default="gib-impacted.log",
help="File containing list of impacted modules, one per line, "
"as paths, not artifact ids",
)
parser.add_argument(
"-o",
"--output",
type=argparse.FileType("w"),
default=sys.stdout,
help="Filename to write impacted modules matrix JSON to",
)
parser.add_argument(
"-v",
"--verbose",
action="store_const",
dest="loglevel",
const=logging.INFO,
default=logging.WARNING,
help="Print info level logs",
)
parser.add_argument(
"-t",
"--test",
action='store_true',
help="test this script instead of executing it",
)
args = parser.parse_args()
logging.basicConfig(level=args.loglevel)
if args.test:
sys.argv = [sys.argv[0]]
unittest.main()
return
build(args.matrix, args.impacted, args.output)
def build(matrix_file, impacted_file, output_file):
matrix = yaml.load(matrix_file, Loader=yaml.Loader)
impacted = list(filter(None, [line.strip() for line in impacted_file.readlines()]))
logging.info("Read matrix: %s", matrix)
logging.info("Read impacted: %s", impacted)
modules = []
for item in matrix.get("modules", []):
module = check_modules(item, impacted)
if module is None:
logging.info("Excluding matrix section: %s", item)
continue
modules.append(module)
if "modules" in matrix:
matrix["modules"] = modules
include = []
for item in matrix.get("include", []):
modules = check_modules(item.get("modules", []), impacted)
if modules is None:
logging.info("Excluding matrix section: %s", item)
continue
item["modules"] = modules
include.append(item)
if "include" in matrix:
if include:
matrix["include"] = include
else:
del matrix["include"]
json.dump(matrix, output_file)
output_file.write("\n")
def check_modules(modules, impacted):
if isinstance(modules, str):
modules = [modules]
if impacted and not any(module in impacted for module in modules):
return None
# concatenate because matrix values should be primitives
return ",".join(modules)
class TestBuild(unittest.TestCase):
def test_build(self):
cases = [
# basic test
(
{
"modules": ["a", "b"],
},
["a"],
{
"modules": ["a"],
},
),
# include adds a new entry
(
{
"modules": ["a", "b"],
"include": [
{"modules": "c"},
{"modules": "d"},
],
},
["a", "d"],
{
"modules": ["a"],
"include": [
{"modules": "d"},
],
},
),
# include entry overwrites one in matrix
(
{
"modules": ["a", "b"],
"include": [
{"modules": "a", "options": "-Pprofile"},
],
},
["a"],
{
"modules": ["a"],
"include": [
{"modules": "a", "options": "-Pprofile"},
],
},
),
# with excludes
(
{
"modules": ["a", "b"],
"exclude": ["b"],
},
["a"],
{
"modules": ["a"],
"exclude": ["b"],
},
),
]
for matrix, impacted, expected in cases:
with self.subTest():
# given
matrix_file = tempfile.TemporaryFile("w+")
yaml.dump(matrix, matrix_file)
matrix_file.seek(0)
impacted_file = tempfile.TemporaryFile("w+")
impacted_file.write("\n".join(impacted))
impacted_file.seek(0)
output_file = tempfile.TemporaryFile("w+")
# when
build(matrix_file, impacted_file, output_file)
output_file.seek(0)
output = json.load(output_file)
# then
self.assertEqual(output, expected)
matrix_file.close()
impacted_file.close()
output_file.close()
if __name__ == "__main__":
main()