-
Notifications
You must be signed in to change notification settings - Fork 50
/
generate-matrix.py
executable file
·138 lines (109 loc) · 3.91 KB
/
generate-matrix.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
#!/usr/bin/env python3
import subprocess
import itertools
import argparse
import sys
import os
import re
import yaml
def get_minor_version(ver):
return re.sub(r"^(\d+\.\d+).*", r"\1", ver)
def should_include_combo(combination, exclusions):
for exclusion in exclusions:
if all([combination[key] == exclusion[key] for key in set(exclusion.keys())]):
return False
return True
def find_all_matrix_rows(matrix):
matrix_vars = matrix['matrix']
for TOMCAT_VERSION in matrix_vars['TOMCAT_VERSION']:
for TOMCAT_JAVA_VERSION in matrix_vars['TOMCAT_JAVA_VERSION']:
for TOMCAT_BASE_IMAGE in matrix_vars['TOMCAT_BASE_IMAGE']:
for LUCEE_MINOR in matrix_vars['LUCEE_MINOR']:
for LUCEE_SERVER in matrix_vars['LUCEE_SERVER']:
for LUCEE_VARIANT in matrix_vars['LUCEE_VARIANT']:
yield {
'TOMCAT_VERSION': TOMCAT_VERSION,
'TOMCAT_JAVA_VERSION': TOMCAT_JAVA_VERSION,
'TOMCAT_BASE_IMAGE': TOMCAT_BASE_IMAGE,
'LUCEE_MINOR': LUCEE_MINOR,
'LUCEE_SERVER': LUCEE_SERVER,
'LUCEE_VARIANT': LUCEE_VARIANT,
}
def combine_rows_by_tomcat(rows):
def group_by_tomcat(row):
return (row['TOMCAT_VERSION'], row['TOMCAT_JAVA_VERSION'], row['TOMCAT_BASE_IMAGE'])
for tomcat, combination in itertools.groupby(rows, group_by_tomcat):
result = {
'TOMCAT_VERSION': tomcat[0],
'TOMCAT_JAVA_VERSION': tomcat[1],
'TOMCAT_BASE_IMAGE': tomcat[2],
'LUCEE_MINOR': set(),
'LUCEE_SERVER': set(),
'LUCEE_VARIANT': set(),
}
for row in combination:
result['LUCEE_MINOR'].add(str(row['LUCEE_MINOR']))
result['LUCEE_SERVER'].add(str(row['LUCEE_SERVER']))
result['LUCEE_VARIANT'].add(str(row['LUCEE_VARIANT']))
yield result
def coalesce_combinations(combinations):
for combo in combinations:
lucee_minors = ",".join(sorted(combo['LUCEE_MINOR']))
lucee_servers = ",".join(sorted(combo['LUCEE_SERVER']))
lucee_variants = ",".join(sorted(combo['LUCEE_VARIANT']))
yield {
'TOMCAT_VERSION': combo['TOMCAT_VERSION'],
'TOMCAT_JAVA_VERSION': combo['TOMCAT_JAVA_VERSION'],
'TOMCAT_BASE_IMAGE': combo['TOMCAT_BASE_IMAGE'],
'LUCEE_MINOR': lucee_minors,
'LUCEE_SERVER': lucee_servers,
'LUCEE_VARIANTS': lucee_variants,
}
def combination_to_env_line(combo):
return " ".join([f"{key}={value}" for key, value in combo.items()])
def main():
parser = argparse.ArgumentParser(description='Start the build process.')
parser.add_argument('--list-tags', action='store_true', default=False,
help='only list the tags that would be generated')
parser.add_argument('--dry-run', action='store_true', default=False,
help='print the new travis config, but do not write it')
parser.add_argument('--quiet', action='store_true', default=False,
help='do not print the travis config')
args = parser.parse_args()
with open('./matrix.yaml') as matrix_input:
matrix = yaml.safe_load(matrix_input)
rows = [
row
for row in find_all_matrix_rows(matrix)
if should_include_combo(row, matrix['exclusions'])
]
combinations = list(combine_rows_by_tomcat(rows))
coalesced = list(coalesce_combinations(combinations))
if args.list_tags:
for row in coalesced:
if os.name == "nt":
run_args = [sys.executable, "./build-images.py", "--list-tags"]
else:
run_args = ["./build-images.py", "--list-tags"],
subprocess.run(
run_args,
universal_newlines=True,
check=True,
env={**row, 'LUCEE_VERSION': os.getenv('LUCEE_VERSION'), 'PATH': os.getenv('PATH'), 'SYSTEMROOT': os.getenv('SYSTEMROOT')},
)
return
travis_env_rows = [combination_to_env_line(combo) for combo in coalesced]
config = {
**matrix['travis'],
'env': {
'jobs': travis_env_rows,
},
}
conf_stringified = yaml.dump(config, default_flow_style=False, width=240, indent=2)
if not args.quiet:
print(conf_stringified)
if not args.dry_run:
with open('./.travis.yml', 'w') as travis_config:
travis_config.write(conf_stringified)
if __name__ == '__main__':
main()