-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathmdakit.py
550 lines (450 loc) Β· 20.2 KB
/
mdakit.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
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
#!/usr/bin/env python3
# MIT License
# Copyright (c) 2022 MDAnalysis Development Team
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import os
import sys
import inspect
import importlib
from urllib import request
from pathlib import Path
import requests
from packaging import specifiers
import json
import yaml
from yaml.loader import SafeLoader
import version
from pydantic import BaseModel, validator, Extra
from typing import Dict, List, Union, Optional, Any, Iterable
from github import Github
def read_yaml(filepath):
with open(filepath) as f:
data = yaml.load(f, Loader=SafeLoader)
return data
class Status:
def __init__(self, path):
if path.is_file():
status = read_yaml(path)
develop = TestStatusDict(**status['develop'])
latest = TestStatusDict(**status['latest'])
badges = BadgesStatusDict(**status['badges'])
self.data = StatusData(develop=develop, latest=latest, badges=badges)
else:
self.data = StatusData()
def write_status(self, basepath):
outfile = basepath / "status.yaml"
with open(outfile, 'w') as f:
yaml.dump(self.data.dict(), f, default_flow_style=False)
class TestStatusDict(BaseModel):
install_python: bool = True
install_mdakit: bool = True
install_mdanalysis: bool = True
install_test_deps: bool = True
run_tests: bool = True
numfails: int = 0
class BadgesStatusDict(BaseModel):
coverage: bool = False
converter: bool = False
reader: bool = False
writer: bool = False
topology: bool = False
transformation: bool = False
analysis: bool = False
class StatusData(BaseModel):
"""
Pydantic class to hold the mdakit status data
"""
develop: TestStatusDict = TestStatusDict()
latest: TestStatusDict = TestStatusDict()
badges: BadgesStatusDict = BadgesStatusDict()
class Config:
extra = Extra.allow
class MetaData(BaseModel):
"""
Pydantic class to hold the mdakit metadata.
"""
project_name: str
authors: List[str]
maintainers: List[str]
description: str
keywords: List[str]
license: str
project_home: str
python_requires: str
documentation_home: str
documentation_type: str
project_org: Optional[str]
community_home: Optional[str]
install: Optional[List[str]] = None
import_name: Optional[str] = None
src_install: Optional[List[str]] = None
run_tests: Optional[List[str]] = None
test_dependencies: Optional[List[str]] = None
development_status: Optional[str]
changelog: Optional[str]
publications: Optional[List[str]]
# TODO: what about authors, that can also optionally be a URL
#@validator('project_home', 'documentation_home', 'community_home',
# 'changelog')
#def url_exists(cls, v):
# if v is not None:
# user_agent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.7) Gecko/2009021910 Firefox/3.0.7'
# headers = {'User-Agent': user_agent,}
# req = request.Request(v, None, headers)
# code = request.urlopen(req).getcode()
# if code != 200:
# raise ValueError(f"unreachable URL: {v}")
class MDAKit:
def __init__(self, path):
self.path = Path(path)
self._read_metadata(self.path)
self._read_status(self.path)
def _read_metadata(self, path):
"""
Read mdakit metadata yaml file
"""
metadata = read_yaml(path / "metadata.yaml")
self.metadata = MetaData(
project_name=metadata['project_name'],
authors=metadata['authors'],
maintainers=metadata['maintainers'],
description=metadata['description'],
keywords=metadata['keywords'],
license=metadata['license'],
project_home=metadata['project_home'],
python_requires=metadata['python_requires'],
documentation_home=metadata['documentation_home'],
documentation_type=metadata['documentation_type'],
project_org=metadata.get('project_org', None),
community_home=metadata.get('community_home', None),
install=metadata.get('install', None),
import_name=metadata.get('import_name', None),
src_install=metadata.get('src_install', None),
run_tests=metadata.get('run_tests', None),
test_dependencies=metadata.get('test_dependencies', None),
development_status=metadata.get('development_status', None),
changelog=metadata.get('changelog', None),
publications=metadata.get('publications', None),)
def _read_status(self, path):
"""
Read the status file or create one if it's missing
"""
self.status = Status(path / "status.yaml")
def gen_badges(self):
#try:
badges = self._get_class_badges(self.metadata.import_name)
#except:
# badges = BadgesStatusDict()
try:
badges.coverage = self._get_codecov_status(
self.metadata.project_org,
self.metadata.project_name
)
except:
badges.coverage = False
self.status.data.badges = badges
@staticmethod
def _get_class_badges(name: Optional[str]):
if name is None:
return BadgesStatusDict()
# Get all the necessary MDAnalysis classes
from MDAnalysis.analysis.base import AnalysisBase
from MDAnalysis.coordinates.base import ProtoReader, WriterBase, ConverterBase
from MDAnalysis.topology.base import TopologyReaderBase
from MDAnalysis.transformations.base import TransformationBase
def get_modules(mod):
return inspect.getmembers(mod[1], inspect.ismodule)
def get_submods(mods):
modules = []
submods = [get_modules(mod) for mod in mods]
for mod in submods:
modules.extend(mod)
return modules
mdakit = importlib.import_module(name)
for mod in inspect.getmembers(sys.modules[name], inspect.ismodule):
submod = inspect.getmembers(mod[1], inspect.ismodule)
subsubmods = get_submods(submod)
allmods.extend(submod)
allmods.extend(subsubmods)
mod_classes = [inspect.getmembers(mod[1], inspect.isclass) for mod in allmods]
classes = []
for i in mod_classes:
classes.extend(i)
badges = BadgesStatusDict()
badges.converter = any(issubclass(i[1], ConverterBase) for i in classes)
badges.reader = any(issubclass(i[1], ProtoReader) for i in classes)
badges.writer = any(issubclass(i[1], WriterBase) for i in classes)
badges.topology = any(issubclass(i[1], TopologyBase) for i in classes)
badges.transformation = any(issubclass(i[1], TransformationBase) for i in classes)
badges.analysis = any(issubclass(i[1], AnalysisBase) for i in classes)
return badges
@staticmethod
def _get_codecov_status(orgname: Optional[str], projname: str):
# orgname is optional
if orgname is None:
return False
# Assume that codecov token is set
token = os.environ['CODECOV_TOKEN']
headers = {"Authorization": f"bearer {token}"}
endpoint = f"https://codecov.io/api/v2/github/{orgname}/repos/{project_name}/commits"
response = requests.get(endpoint, headers=headers)
content = json.loads(response.content)
coverage = max(commit['totals']['coverage'] for commit in content['results'])
return coverage > 80
def get_matching_version(self, max_ver: str = "3.11", min_ver: str = "3.8",
version_field: str = "python_requires"):
version_pin = getattr(self.metadata, version_field)
if version_pin is None:
return max_ver
current_version = version.TargetVersion.from_str(max_ver)
min_version = version.TargetVersion.from_str(min_ver)
pin_specifiers = [specifiers.Specifier(i) for i in version_pin.split(',')]
compatible_version = False
while (current_version.to_version() >= min_version.to_version()):
for spec in pin_specifiers:
compatible_version = version._operators[spec.operator](
current_version.to_string(),
spec.version,
)
if compatible_version:
break
else:
current_version.minor -= 1
if not compatible_version:
raise ValueError("Could not find compatible version which matched "
"the python version specs")
return current_version.to_string()
def raise_issues(self):
"""
Raise issues for statuses with > 1 fails
"""
git = Github(os.environ['GITHUB_TOKEN'])
repo = git.get_repo('MDAnalysis/MDAKits')
issues = [issue for issue in repo.get_issues()]
def _bool_status(arg: bool) -> str:
return 'passed' if arg else 'fail'
def _create_issue(run_type: str):
stat = getattr(self.status.data, run_type)
issue_tag = f"[{self.metadata.project_name}-{run_type}]"
issue_title = f"{issue_tag} Failed CI run"
# quit if issue already exists
for issue in issues:
if issue_tag in issue.title:
# clear issues if resolved
if stat.numfails == 0:
issue.edit(state="closed")
return
# if numfails < 1 and you've not done so yet return
if stat.numfails < 1:
return
# generate list of maintainers to tag
maint = ""
for m in self.metadata.maintainers:
maint += f"@{m} "
issue_body = (
f"At least two repeated CI runs for "
f"{self.metadata.project_name} have failed.\n"
f"Here is the last recorded status of the tests:\n"
f" - installing python: {_bool_status(stat.install_python)}\n"
f" - installing the mdakit: {_bool_status(stat.install_mdakit)}\n"
f" - installing mdanalysis: {_bool_status(stat.install_mdanalysis)}\n"
f" - installing the test dependencies: {_bool_status(stat.install_test_deps)}\n"
f" - running tests: {_bool_status(stat.run_tests)}\n"
f"Pinging maintainers: {maint}"
)
repo.create_issue(title=issue_title, body=issue_body)
for run_type in ['latest', 'develop']:
_create_issue(run_type)
@staticmethod
def _get_custom_badge(left, right, colour):
return f"https://img.shields.io/badge/{left}-{right}-{colour}.svg"
def gen_ci_badges(self, run_type):
status = getattr(self.status.data, run_type)
if ((self.metadata.run_tests is None) or
(run_type == 'develop' and self.metadata.install is None) or
(run_type == 'latest' and self.metadata.src_install is None)):
return self._get_custom_badge(run_type, "unavailable", "inactive")
elif status.numfails > 1:
return self._get_custom_badge(run_type, "failed", "red")
else:
return self._get_custom_badge(run_type, "passed", "green")
def gen_code_badges(self):
badges = []
# coverage
if self.status.data.badges.coverage:
badges.append(self._get_custom_badge('coverage', '> 80%', 'ff69b4'))
if self.status.data.badges.converter:
badges.append(self._get_custom_badge('type', 'converter', 'blue'))
if self.status.data.badges.reader:
badges.append(self._get_custom_badge('type', 'reader', 'blue'))
if self.status.data.badges.writer:
badges.append(self._get_custom_badge('type', 'writer', 'blue'))
if self.status.data.badges.topology:
badges.append(self._get_custom_badge('type', 'topology', 'blue'))
if self.status.data.badges.transformation:
badges.append(self._get_custom_badge('type', 'transformation', 'blue'))
if self.status.data.badges.analysis:
badges.append(self._get_custom_badge('type', 'analysis', 'blue'))
return ' '.join(badges)
def gen_authors(self, urls):
if 'https' in self.metadata.authors[0]:
auths = f"`{self.metadata.project_name} authors`_"
urls.append(f".. _`{self.metadata.project_name} authors`:\n"
f" {self.metadata.authors[0]}\n\n")
else:
auths = ','.join(self.metadata.authors)
return auths
def write_mdakit_page(self):
name = self.metadata.project_name
urls = []
authors = self.gen_authors(urls)
title = ("************************************************\n"
f"{name}\n"
"************************************************\n\n")
description = (f"| **Description:**\n"
f"| *{self.metadata.description}*\n")
keywords = f"| π **Keywords:** {', '.join(self.metadata.keywords)}\n"
authors = f"| ποΈ **Authors**: {authors}\n"
project_home = f"| π **Project home:** {self.metadata.project_home}\n"
documentation_home = f"| π **Documentation:** {self.metadata.documentation_home}\n"
license = f"| βοΈ **License:** {self.metadata.license}\n"
if self.metadata.development_status is not None:
development_status = f"| π **Development status:** {self.metadata.development_status}\n"
else:
development_status = ""
if self.metadata.changelog is not None:
changelog = f"| π **Changelog:** {self.metadata.changelog}\n"
else:
changelog = ""
if self.metadata.publications is not None:
publications = f"| π **Publications:**\n"
for pub in self.metadata.publications:
publications += f"| * {pub}\n"
else:
publications = ""
latest_ci = f"| π§ͺ **Tests (latest):** |{name}_latest| \n"
develop_ci = f"| π§ͺ **Tests (develop):** |{name}_develop| \n"
urls.append(f".. |{name}_latest| image:: {self.gen_ci_badges('latest')}\n"
f" :alt: {name} develop CI status\n"
f" :target: https://github.com/MDAnalysis/MDAKits/actions\n\n")
urls.append(f".. |{name}_develop| image:: {self.gen_ci_badges('develop')}\n"
f" :alt: {name} develop CI status\n"
f" :target: https://github.com/MDAnalysis/MDAKits/actions\n\n")
if self.gen_code_badges() != '':
badges = (f"| π **Badges**\n"
f" {self.gen_code_badges()}\n")
else:
badges = "\n"
if ((self.metadata.install is not None) or
(self.metadata.src_install is not None)):
installation_instructions = (
"Installation instructions\n"
"=========================\n\n"
)
if self.metadata.install is not None:
installation_instructions += (
f"The latest version of {name} can be "
"installed using the following:\n\n"
".. code-block:: bash\n\n"
)
for i in self.metadata.install:
installation_instructions += (
f" {i}\n"
)
if self.metadata.src_install is not None:
installation_instructions += (
f"\nThe source code of {name} can be "
"installed using the following:\n\n"
".. code-block:: bash\n\n"
)
for i in self.metadata.src_install:
installation_instructions += (
f" {i}\n"
)
installation_instructions += "\n"
else:
installation_instructions = None
with open(f"{name}.rst", "w") as kitf:
kitf.write(f".. _{name}:\n\n")
kitf.write(title)
kitf.write(authors)
kitf.write(project_home)
kitf.write(documentation_home)
kitf.write(license)
kitf.write(keywords)
kitf.write(development_status)
kitf.write(changelog)
kitf.write(publications)
kitf.write(latest_ci)
kitf.write(develop_ci)
kitf.write(description)
kitf.write(badges)
kitf.write("\n\n")
if installation_instructions is not None:
kitf.write(installation_instructions)
for entry in urls:
kitf.write(entry)
def write_table_entry(self, f, urls, toctree):
name = self.metadata.project_name
keywords = ', '.join(self.metadata.keywords)
authors = self.gen_authors(urls)
ci_latest = f"|{name}_latest|"
ci_develop = f"|{name}_develop|"
urls.append(f".. |{name}_latest| image:: {self.gen_ci_badges('latest')}\n"
f" :alt: {name} develop CI status\n"
f" :target: https://github.com/MDAnalysis/MDAKits/actions\n\n")
urls.append(f".. |{name}_develop| image:: {self.gen_ci_badges('develop')}\n"
f" :alt: {name} develop CI status\n"
f" :target: https://github.com/MDAnalysis/MDAKits/actions\n\n")
f.write(f' * - :ref:`{name}`\n')
f.write(f' - {keywords}\n')
f.write(f' - {authors}\n')
f.write(f' - {ci_latest} {ci_develop}\n')
toctree.append(f' {name}\n')
def get_install(self, install_type):
if install_type == "src":
if self.metadata.src_install is None:
return ""
else:
return ';'.join(self.metadata.src_install)
else:
if self.metadata.install is None:
return ""
else:
return ';'.join(self.metadata.install)
def get_test_deps(self):
if self.metadata.test_dependencies is None:
return ""
return ';'.join(self.metadata.test_dependencies)
def get_run_tests(self, runtype):
if self.metadata.run_tests is None:
return ""
if ((runtype == "latest" and self.metadata.install is None) or
(runtype == "develop" and self.metadata.src_install is None)):
return ""
test_steps = []
for step in self.metadata.run_tests:
# special case for cloning the latest tag
if step == "git clone latest":
test_steps.append(f'git clone {self.metadata.project_home}.git && cd "$(basename "$_" .git)"')
if runtype == "latest":
test_steps.append("git fetch --tags")
test_steps.append("latestTag=$(git describe --tags `git rev-list --tags --max-count=1`)")
test_steps.append("git checkout $latestTag")
else:
test_steps.append(step)
return ';'.join(test_steps)