Skip to content

Commit eb12503

Browse files
committed
MAINT: Revision of Zenodo update script
Unlocks first position as per private conversation with @chrisgorgo
1 parent 6314fde commit eb12503

File tree

1 file changed

+56
-53
lines changed

1 file changed

+56
-53
lines changed

tools/update_zenodo.py

Lines changed: 56 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,15 @@
11
#!/usr/bin/env python3
2+
"""Update and sort the creators list of the zenodo record."""
3+
import sys
4+
import shutil
5+
from pathlib import Path
26
import json
37
from fuzzywuzzy import fuzz, process
4-
import shutil
5-
import os
68
import subprocess as sp
79

8-
if os.path.exists('line-contributions.txt'):
9-
with open('line-contributions.txt', 'rt') as fp:
10-
lines = fp.readlines()
11-
else:
12-
if shutil.which('git-line-summary'):
13-
print("Running git-line-summary on nipype repo")
14-
lines = sp.check_output(['git-line-summary']).decode().split('\n')
15-
else:
16-
raise RuntimeError("Install Git Extras to view git contributors")
17-
18-
data = [' '.join(line.strip().split()[1:-1]) for line in lines if '%' in line]
19-
20-
# load zenodo from master
21-
with open('.zenodo.json', 'rt') as fp:
22-
zenodo = json.load(fp)
23-
zen_names = [' '.join(val['name'].split(',')[::-1]).strip()
24-
for val in zenodo['creators']]
25-
26-
name_matches = []
27-
28-
for ele in data:
29-
matches = process.extract(ele, zen_names, scorer=fuzz.token_sort_ratio,
30-
limit=2)
31-
# matches is a list [('First match', % Match), ('Second match', % Match)]
32-
if matches[0][1] > 80:
33-
val = zenodo['creators'][zen_names.index(matches[0][0])]
34-
else:
35-
# skip unmatched names
36-
print("No entry to sort:", ele)
37-
continue
38-
39-
if val not in name_matches:
40-
name_matches.append(val)
41-
10+
CREATORS_LAST_ORCID = '0000-0002-5312-6729' # This ORCID should go last
4211
# for entries not found in line-contributions
43-
missing_entries = [
12+
MISSING_ENTRIES = [
4413
{"name": "Varada, Jan"},
4514
{"name": "Schwabacher, Isaac"},
4615
{"affiliation": "Child Mind Institute / Nathan Kline Institute",
@@ -61,31 +30,65 @@
6130
{"name": "Lai, Jeff"}
6231
]
6332

64-
for entry in missing_entries:
65-
name_matches.append(entry)
66-
6733

6834
def fix_position(creators):
35+
"""Place Satra last."""
6936
# position first / last authors
70-
f_authr = None
7137
l_authr = None
7238

73-
for i, info in enumerate(creators):
74-
if info['name'] == 'Gorgolewski, Krzysztof J.':
75-
f_authr = i
76-
if info['name'] == 'Ghosh, Satrajit':
77-
l_authr = i
39+
for info in creators:
40+
if 'orcid' in info and info['orcid'] == CREATORS_LAST_ORCID:
41+
l_authr = info
7842

79-
if f_authr is None or l_authr is None:
43+
if l_authr is None:
8044
raise AttributeError('Missing important people')
8145

82-
creators.insert(0, creators.pop(f_authr))
83-
creators.insert(len(creators), creators.pop(l_authr + 1))
46+
creators.remove(l_authr)
47+
creators.append(l_authr)
8448
return creators
8549

8650

87-
zenodo['creators'] = fix_position(name_matches)
51+
if __name__ == '__main__':
52+
contrib_file = Path('line-contributors.txt')
53+
lines = []
54+
if contrib_file.exists():
55+
print('WARNING: Reusing existing line-contributors.txt file.', file=sys.stderr)
56+
lines = contrib_file.read_text().splitlines()
8857

89-
with open('.zenodo.json', 'wt') as fp:
90-
json.dump(zenodo, fp, indent=2, sort_keys=True)
91-
fp.write('\n')
58+
if not lines and shutil.which('git-line-summary'):
59+
print("Running git-line-summary on nipype repo")
60+
lines = sp.check_output(['git-line-summary']).decode().splitlines()
61+
contrib_file.write_text('\n'.join(lines))
62+
63+
if not lines:
64+
raise RuntimeError('Could not find line-contributors from git repository '
65+
'(hint: please install git-extras).')
66+
67+
data = [' '.join(line.strip().split()[1:-1]) for line in lines if '%' in line]
68+
69+
# load zenodo from master
70+
zenodo_file = Path('.zenodo.json')
71+
zenodo = json.loads(zenodo_file.read_text())
72+
zen_names = [' '.join(val['name'].split(',')[::-1]).strip()
73+
for val in zenodo['creators']]
74+
75+
name_matches = []
76+
for ele in data:
77+
matches = process.extract(ele, zen_names, scorer=fuzz.token_sort_ratio,
78+
limit=2)
79+
# matches is a list [('First match', % Match), ('Second match', % Match)]
80+
if matches[0][1] > 80:
81+
val = zenodo['creators'][zen_names.index(matches[0][0])]
82+
else:
83+
# skip unmatched names
84+
print("No entry to sort:", ele)
85+
continue
86+
87+
if val not in name_matches:
88+
name_matches.append(val)
89+
90+
for entry in MISSING_ENTRIES:
91+
name_matches.append(entry)
92+
93+
zenodo['creators'] = fix_position(name_matches)
94+
zenodo_file.write_text(json.dumps(zenodo, indent=2, sort_keys=True))

0 commit comments

Comments
 (0)