-
Notifications
You must be signed in to change notification settings - Fork 11
/
translate-travis.yml-to-github-actions.py
204 lines (180 loc) · 5.95 KB
/
translate-travis.yml-to-github-actions.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
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyright © 2021 Shlomi Fish < https://www.shlomifish.org/ >
#
# Licensed under the terms of the MIT license.
"""
fc-solve/CI-testing/translate-travis.yml-to-github-actions.py :
This program translates fc-solve's .travis.yml to GitHub actions
and ACT workflows ( https://github.com/nektos/act ).
While ostensibly FOSS, it most probably is not generic enough
for your use cases.
"""
import re
import yaml
def _add_condition_while_excluding_gh_actions(job_dict, is_act):
"""
See:
https://github.com/actions/runner/issues/480
Workflow level `env` does not work properly in all fields. · Issue #480
"""
if is_act:
job_dict['if'] = \
"${{ ! contains(env.ACT_SKIP, matrix.env.WHAT) }}"
def generate(output_path, is_act):
"""docstring for main"""
env_keys = set()
def _process_env(env):
ret = {}
for line in env:
m = re.match(
'\\A([A-Za-z0-9_]+)=([A-Za-z0-9_]+)\\Z',
line)
key = m.group(1)
val = m.group(2)
assert key not in ret
ret[key] = val
env_keys.add(key)
return ret
with open("./.travis.yml", "rt") as infh:
data = yaml.safe_load(infh)
steps = []
steps.append({"uses": ("actions/checkout@v2"), })
if 0:
steps.append({
"run":
("cd workflow ; (ls -lrtA ; false)"), })
elif 0:
steps.append({
"run":
("cd . ; (ls -lrtA ; false)"), })
steps.append({"run": ("sudo apt-get update -qq"), })
steps.append({
"run": ("sudo apt-get --no-install-recommends install -y " +
" ".join(["eatmydata"])
),
}
)
steps.append({
"run": ("sudo eatmydata apt-get --no-install-recommends install -y " +
" ".join(data['addons']['apt']['packages'])
),
}
)
local_lib_shim = 'local_lib_shim() { eval "$(perl ' + \
'-Mlocal::lib=$HOME/' + \
'perl_modules)"; } ; local_lib_shim ; '
for arr in ['before_install', 'install', 'script']:
steps += [{"run": local_lib_shim + x} for x in data[arr]]
job = 'test-fc-solve'
o = {'jobs': {job: {'runs-on': 'ubuntu-latest',
'steps': steps, }},
'name': 'use-github-actions', 'on': ['push', ], }
if 'matrix' in data:
if 'include' in data['matrix']:
o['jobs'][job]['strategy'] = {'matrix': {'include': [
{'env': _process_env(x['env']), }
for x in data['matrix']['include']
], }, }
o['jobs'][job]['env'] = {
x: "${{ matrix.env." + x + " }}"
for x in env_keys
}
_add_condition_while_excluding_gh_actions(
job_dict=o['jobs'][job],
is_act=is_act,
)
else:
assert False
with open(output_path, "wt") as outfh:
# yaml.safe_dump(o, outfh)
yaml.safe_dump(o, stream=outfh, canonical=False, indent=4, )
def generate_windows_yaml(output_path, is_act):
"""docstring for main"""
env_keys = set()
def _process_env(env):
ret = {}
for line in env:
m = re.match(
'\\A([A-Za-z0-9_]+)=([A-Za-z0-9_]+)\\Z',
line)
key = m.group(1)
val = m.group(2)
assert key not in ret
ret[key] = val
env_keys.add(key)
return ret
with open("./.appveyor.yml", "rt") as infh:
data = yaml.safe_load(infh)
with open("./fc-solve/CI-testing/gh-actions--" +
"windows-yml--from-p5-UV.yml", "rt") as infh:
skel = yaml.safe_load(infh)
steps = skel['jobs']['perl']['steps']
while steps[-1]['name'] != 'perl -V':
steps.pop()
cpanm_step = {
"name": "install cpanm and mult modules",
"uses": "perl-actions/install-with-cpanm@v1",
}
steps.append(cpanm_step)
def _calc_batch_code(cmds):
batch = ""
batch += "@echo on\n"
for k, v in sorted(data['environment'].items()):
# batch += "SET " + k + "=\"" + v + "\"\n"
batch += "SET " + k + "=" + v + "\n"
for cmd in cmds:
if cmd.startswith("cpanm "):
words = cmd.split(' ')[1:]
dw = []
for w in words:
if not w.startswith("-"):
dw.append(w)
nonlocal cpanm_step
cpanm_step['with'] = {"install": "\n".join(dw), }
continue
if "choco install strawberryperl" not in cmd:
if 0:
r = re.sub(
"curl\\s+-o\\s+(\\S+)\\s+(\\S+)",
"lwp-download \\2 \\1",
cmd)
else:
r = cmd
batch += r + "\n"
return batch
if 0:
steps.append({'name': "install code", "run": _calc_batch_code(
cmds=data['install']), "shell": "cmd", })
steps.append({
'name': "install and test_script code",
"run": _calc_batch_code(
cmds=(data['install']+data['test_script'])
),
"shell": "cmd",
})
steps += [{
'name': "upload build artifacts - " + art['name'],
'uses': "actions/upload-artifact@v2",
'with': art
} for art in data['artifacts']]
with open(output_path, "wt") as outfh:
# yaml.safe_dump(o, outfh)
yaml.safe_dump(skel, stream=outfh, canonical=False, indent=4, )
def main():
generate(
output_path=".github/workflows/use-github-actions.yml",
is_act=False,
)
generate(
output_path=".act-github/workflows/use-github-actions.yml",
is_act=True,
)
generate_windows_yaml(
output_path=".github/workflows/windows.yml",
is_act=False,
)
if __name__ == "__main__":
main()