-
Notifications
You must be signed in to change notification settings - Fork 532
/
Copy pathupdate_zenodo.py
executable file
·73 lines (61 loc) · 2.57 KB
/
update_zenodo.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
#!/usr/bin/env python3
"""Update and sort the creators list of the zenodo record."""
import sys
import shutil
from pathlib import Path
import json
from fuzzywuzzy import fuzz, process
import subprocess as sp
# These names should go last
CREATORS_LAST = ['Gorgolewski, Krzysztof J.', 'Ghosh, Satrajit']
if __name__ == '__main__':
contrib_file = Path('line-contributors.txt')
lines = []
if contrib_file.exists():
print('WARNING: Reusing existing line-contributors.txt file.', file=sys.stderr)
lines = contrib_file.read_text().splitlines()
git_line_summary_path = shutil.which('git-line-summary')
if not lines and git_line_summary_path:
print("Running git-line-summary on nipype repo")
lines = sp.check_output([git_line_summary_path]).decode().splitlines()
contrib_file.write_text('\n'.join(lines))
if not lines:
raise RuntimeError("""\
Could not find line-contributors from git repository.%s""" % """ \
git-line-summary not found, please install git-extras. """ * (git_line_summary_path is None))
data = [' '.join(line.strip().split()[1:-1]) for line in lines if '%' in line]
# load zenodo from master
zenodo_file = Path('.zenodo.json')
zenodo = json.loads(zenodo_file.read_text())
zen_names = [' '.join(val['name'].split(',')[::-1]).strip()
for val in zenodo['creators']]
total_names = len(zen_names)
name_matches = []
position = 1
for ele in data:
matches = process.extract(ele, zen_names, scorer=fuzz.token_sort_ratio,
limit=2)
# matches is a list [('First match', % Match), ('Second match', % Match)]
if matches[0][1] <= 80:
# skip unmatched names
print("No entry to sort:", ele)
continue
idx = zen_names.index(matches[0][0])
val = zenodo['creators'][idx]
if val not in name_matches:
if val['name'] not in CREATORS_LAST:
val['position'] = position
position += 1
else:
val['position'] = total_names + CREATORS_LAST.index(val['name'])
name_matches.append(val)
for missing in zenodo['creators']:
if 'position' not in missing:
missing['position'] = position
position += 1
name_matches.append(missing)
zenodo['creators'] = sorted(name_matches, key=lambda k: k['position'])
# Remove position
for creator in zenodo['creators']:
del creator['position']
zenodo_file.write_text('%s\n' % json.dumps(zenodo, indent=2, sort_keys=True))