-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathgenerate
executable file
·317 lines (259 loc) · 13.7 KB
/
generate
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
#!/usr/bin/env python3
import os
import os.path as op
import glob
import sys
import yaml
import jinja2
top_path = op.dirname(op.dirname(__file__))
DEFAULT_BRANCH = 'master'
# TODO: proper logigng etc
def log(msg, *args):
print(msg % args)
class ExtensionSpec(dict):
@property
def name(self):
return self["name"]
@property
def branch(self):
return self.get("branch", DEFAULT_BRANCH)
# Some properties which might tune the value
@property
def github(self):
from_spec = self.get("github", self.name.replace("_", "-"))
gh_url = "datalad/{}".format(from_spec) \
if '/' not in from_spec else from_spec
return gh_url
@property
def githubdash(self):
return self.github.replace("_", "-")
@property
def githubname(self):
return self.github.split('/')[1]
@property
def pypi(self):
return self.get("pypi", self.name.replace('_', '-'))
@property
def package(self):
return self.get("package", self.name)
@property
def conda(self):
return self.get('conda', self.package.replace('_', '-'))
@property
def apt_depends(self):
return self.get("apt_depends", [])
@property
def extra_setup_steps(self):
extras = self.get('extra_setup_steps', [])
if extras:
out = [f'{" " * 0 if idx == 0 else " " * 4}- {k}: {v}'
if k == 'name' else f'{" "*6}{k}: {v}'
for idx, extra in enumerate(extras) for k, v in extra.items()]
return out
@property
def tester(self):
return self.get('tester', 'pytest')
@property
def ci(self):
return self.get("ci", 'Travis').lower().split(':', 1)[0]
# unfortunately there is no badge across multiple workflows on github
# and for now encode specific workflow in ci
@property
def ci_workflow(self):
# ok to error out -- so we do not keep errors in spec
return self.get("ci", 'Travis').lower().split(':', 1)[1]
@property
def ci_badge(self):
ci = self.ci
if ci == 'travis':
return f"https://travis-ci.com/{self.githubdash}.svg?branch={self.branch}"
elif ci == 'appveyor':
return f"https://ci.appveyor.com/api/projects/status/github/{self.github}?" \
f"branch={self.branch}&svg=true"
elif ci == 'github':
return f"https://github.com/{self.githubdash}/actions/workflows/{self.ci_workflow}.yml/badge.svg?branch={self.branch}"
else:
raise ValueError("Unknown Ci specification: %s" % ci)
@property
def ci_link(self):
ci = self.ci
if ci == 'travis':
return f"https://travis-ci.com/github/{self.githubdash}"
elif ci == 'appveyor':
user = self.get('ci-user')
return f"https://ci.appveyor.com/project/{user}/{self.githubname}/" \
f"branch/{self.branch}"
elif ci == 'github':
return f'https://github.com/{self.githubdash}/actions/workflows/{self.ci_workflow}.yml?query=branch%3A{self.branch}'
else:
raise ValueError("Unknown Ci specification: %s" % ci)
#def __getattribute__(self, item):
# return self[item]
def jinja_render(tmpl, **kwargs):
"""Custom renderer, so we first replace github actions ${{ with a
custom placeholder to avoid jinja tying to handle it
"""
PLACEHOLDER = "[[["
if PLACEHOLDER in tmpl:
raise RuntimeError(
"Detected placeholder %r already being present in the text"
% PLACEHOLDER
)
tmpl_ = tmpl.replace("${{", PLACEHOLDER)
out_ = jinja2.Template(tmpl_).render(**kwargs)
return out_.replace(PLACEHOLDER, "${{")
def process_extension(ext_spec):
render_kwargs = {
"extension": ext_spec,
}
# TODO: proper recursive
templates_dir = op.join(top_path, 'templates')
for tmpl_p in glob.glob(op.join(templates_dir, '.github/*/*')):
datalad_branches = ['master']
if 'datalad_branch' in tmpl_p:
datalad_branches += ['maint']
for datalad_branch in datalad_branches:
render_kwargs['datalad_branch'] = datalad_branch
p = jinja_render(tmpl_p, **render_kwargs)
if p == tmpl_p:
log("Skipping %s since seems to be not extension specific", tmpl_p)
continue
with open(tmpl_p) as f:
tmpl = f.read()
rendered = jinja_render(tmpl, **render_kwargs)
if rendered == tmpl:
raise RuntimeError(
f"Rendered version of {tmpl_p} content is the same as template. "
"At the moment it is not expected"
)
prel_target = op.relpath(p, templates_dir)
p_target = op.join(top_path, prel_target)
if not op.exists(op.dirname(p_target)):
os.makedirs(op.dirname(p_target))
with open(p_target, "w") as f:
f.write(rendered)
log("Rendered %s", prel_target)
def generate_README(ext_specs):
with open(op.join(top_path, "README.md"), "w") as f:
f.write("""\
# DataLad healthchecks
This is a "dashboard" of various CIs of DataLad, its extensions, and underlying
3-rd party projects like git-annex.
**This README.md is autogenerated - do not edit.
See [CONTRIBUTING.md](CONTRIBUTING.md) for more information.**
## Git-annex Status
- Conda: 


- Current snapshot build/tests + DataLad tests:
[](https://github.com/datalad/git-annex/actions?query=workflow%3A%22Build+git-annex+on+Ubuntu%22)
[](https://github.com/datalad/git-annex/actions?query=workflow%3A%22Build+git-annex+on+macOS%22)
[](https://github.com/datalad/git-annex/actions?query=workflow%3A%22Build+git-annex+on+Windows%22)
[](https://github.com/datalad/git-annex#client-tests)
## DataLad Status
- Distributions:
[](https://GitHub.com/datalad/datalad/releases/)
[](https://anaconda.org/conda-forge/datalad)
[](https://repology.org/project/datalad/versions)
[](https://packages.debian.org/stable/datalad)
[](https://packages.debian.org/unstable/datalad)
[](https://repology.org/project/datalad/versions)
[](https://repology.org/project/datalad/versions)
[](https://repology.org/project/datalad/versions)
- CI:
[](https://app.travis-ci.com/datalad/datalad)
[](https://app.travis-ci.com/datalad/datalad)
[](https://ci.appveyor.com/project/mih/datalad/branch/maint)
[](https://ci.appveyor.com/project/mih/datalad/branch/master)
[](http://datalad.rtfd.org)
[](https://github.com/datalad/datalad/actions/workflows/test_extensions.yml)
[](https://github.com/datalad/datalad/actions/workflows/lint.yml)
- Misc:
[](https://codecov.io/github/datalad/datalad?branch=master)
## DataLad Extensions Status
""")
columns = {
'Name': 'ref',
'Release': 'release_github',
'PyPI Release': 'release_pypi',
'Conda Release': 'release_conda',
'CI: Released + DL master': 'ci_rel+dlmaster',
'CI: Released + DL maint': 'ci_rel+dlmaint',
'CI: develop + DL Release': 'ci_develop+dlrelease',
'Codecov': 'codecov',
'Issue Resolution': 'issues_restime',
'Open Issues': 'issues_open'
}
def write_row(entries):
f.write(' | '.join([''] + entries + ['']) + '\n')
# Header
write_row(list(columns))
write_row(['---']*len(columns))
# TODO: link to per-extension cron-github-CI results
https_extension = "https://github.com/datalad/datalad-extensions"
for ext, e in sorted(ext_specs.items()):
entry = {
'ref': f"[{ext}](https://github.com/{e.github})",
'release_github': f'[](https://GitHub.com/{e.githubdash}/releases/)',
'release_pypi': f'[](https://pypi.python.org/pypi/{e.pypi}/)',
'release_conda': f'',
'ci_rel+dlmaster': f'[]({https_extension}/actions?query=workflow%3Atest-{ext}-master)',
'ci_rel+dlmaint': f'[]({https_extension}/actions?query=workflow%3Atest-{ext}-maint)',
'ci_develop+dlrelease': f'[]({e.ci_link})',
'codecov': f'[](https://codecov.io/github/{e.githubdash}?branch=master)',
'issues_restime': f'[](http://isitmaintained.com/project/{e.githubdash} "Average time to resolve an issue")',
'issues_open': f'[](http://isitmaintained.com/project/{e.githubdash} "Percentage of issues still open")',
}
write_row([entry[k] for k in columns.values()])
# [](https://doi.org/10.5281/zenodo.3368666)
# f"[](http://{e.githubname}.rtfd.org)"
# f"[](
# https://opensource.org/licenses/MIT)
def main(extensions):
ext_specs = load_ext_specs()
for ext in extensions or list(ext_specs):
process_extension(ext_specs[ext])
# We generate/update README.md for all extensions for now
generate_README(ext_specs)
def add_default_branch():
# helper to add default branch if differs from 'master'
import json
import ruamel.yaml
from urllib.request import Request, urlopen
yaml = ruamel.yaml.YAML() # defaults to round-trip if no parameters given
with open(op.join(top_path, "extensions.yaml")) as f:
conf = yaml.load(f.read())
for name, rec in conf['extensions'].items():
rec_tuned = rec.copy()
rec_tuned['name'] = name
ed = ExtensionSpec(rec_tuned)
req = Request(f'https://api.github.com/repos/{ed.github}',
headers={'Accept': 'application/vnd.github+json'})
with urlopen(req) as r:
b = json.load(r)['default_branch']
# reassign only if not "default default" i.e. master
if b != DEFAULT_BRANCH:
rec['branch'] = b
with open(op.join(top_path, "extensions.yaml"), "w") as f:
yaml.dump(conf, f)
def load_ext_specs():
with open(op.join(top_path, "extensions.yaml")) as f:
spec = yaml.load(f.read(), Loader=yaml.BaseLoader)
# ATM expecting only a single structure
assert list(spec.keys()) == ['extensions']
ext_specs = spec["extensions"]
for ext in list(ext_specs):
if not ext_specs[ext]: # Was None
# If it had nothing -- prepare it to be a dict
ext_specs[ext] = {}
ext_spec = ext_specs[ext] \
= ExtensionSpec(**ext_specs[ext])
ext_spec["name"] = ext
return ext_specs
if __name__ == "__main__":
# add_default_branch()
main(sys.argv[1:])