|
1 | 1 | #!/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 |
2 | 6 | import json
|
3 | 7 | from fuzzywuzzy import fuzz, process
|
4 |
| -import shutil |
5 |
| -import os |
6 | 8 | import subprocess as sp
|
7 | 9 |
|
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 |
42 | 11 | # for entries not found in line-contributions
|
43 |
| -missing_entries = [ |
| 12 | +MISSING_ENTRIES = [ |
44 | 13 | {"name": "Varada, Jan"},
|
45 | 14 | {"name": "Schwabacher, Isaac"},
|
46 | 15 | {"affiliation": "Child Mind Institute / Nathan Kline Institute",
|
|
61 | 30 | {"name": "Lai, Jeff"}
|
62 | 31 | ]
|
63 | 32 |
|
64 |
| -for entry in missing_entries: |
65 |
| - name_matches.append(entry) |
66 |
| - |
67 | 33 |
|
68 | 34 | def fix_position(creators):
|
| 35 | + """Place Satra last.""" |
69 | 36 | # position first / last authors
|
70 |
| - f_authr = None |
71 | 37 | l_authr = None
|
72 | 38 |
|
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 |
78 | 42 |
|
79 |
| - if f_authr is None or l_authr is None: |
| 43 | + if l_authr is None: |
80 | 44 | raise AttributeError('Missing important people')
|
81 | 45 |
|
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) |
84 | 48 | return creators
|
85 | 49 |
|
86 | 50 |
|
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() |
88 | 57 |
|
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