|
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 | +# These ORCIDs should go last |
| 11 | +CREATORS_LAST = ['Gorgolewski, Krzysztof J.', 'Ghosh, Satrajit'] |
42 | 12 | # for entries not found in line-contributions |
43 | | -missing_entries = [ |
| 13 | +MISSING_ENTRIES = [ |
44 | 14 | {"name": "Varada, Jan"}, |
45 | 15 | {"name": "Schwabacher, Isaac"}, |
46 | 16 | {"affiliation": "Child Mind Institute / Nathan Kline Institute", |
47 | 17 | "name": "Pellman, John", |
48 | 18 | "orcid": "0000-0001-6810-4461"}, |
49 | | - {"name": "Perez-Guevara, Martin"}, |
50 | 19 | {"name": "Khanuja, Ranjeet"}, |
51 | 20 | {"affiliation": |
52 | 21 | "Medical Imaging & Biomarkers, Bioclinica, Newark, CA, USA.", |
|
61 | 30 | {"name": "Lai, Jeff"} |
62 | 31 | ] |
63 | 32 |
|
64 | | -for entry in missing_entries: |
65 | | - name_matches.append(entry) |
| 33 | +if __name__ == '__main__': |
| 34 | + contrib_file = Path('line-contributors.txt') |
| 35 | + lines = [] |
| 36 | + if contrib_file.exists(): |
| 37 | + print('WARNING: Reusing existing line-contributors.txt file.', file=sys.stderr) |
| 38 | + lines = contrib_file.read_text().splitlines() |
66 | 39 |
|
67 | | - |
68 | | -def fix_position(creators): |
69 | | - # position first / last authors |
70 | | - f_authr = None |
71 | | - l_authr = None |
72 | | - |
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 |
78 | | - |
79 | | - if f_authr is None or l_authr is None: |
80 | | - raise AttributeError('Missing important people') |
81 | | - |
82 | | - creators.insert(0, creators.pop(f_authr)) |
83 | | - creators.insert(len(creators), creators.pop(l_authr + 1)) |
84 | | - return creators |
85 | | - |
86 | | - |
87 | | -zenodo['creators'] = fix_position(name_matches) |
88 | | - |
89 | | -with open('.zenodo.json', 'wt') as fp: |
90 | | - json.dump(zenodo, fp, indent=2, sort_keys=True) |
91 | | - fp.write('\n') |
| 40 | + git_line_summary_path = shutil.which('git-line-summary') |
| 41 | + if not lines and git_line_summary_path: |
| 42 | + print("Running git-line-summary on nipype repo") |
| 43 | + lines = sp.check_output([git_line_summary_path]).decode().splitlines() |
| 44 | + contrib_file.write_text('\n'.join(lines)) |
| 45 | + |
| 46 | + if not lines: |
| 47 | + raise RuntimeError("""\ |
| 48 | +Could not find line-contributors from git repository.%s""" % """ \ |
| 49 | +git-line-summary not found, please install git-extras. """ * (git_line_summary_path is None)) |
| 50 | + |
| 51 | + data = [' '.join(line.strip().split()[1:-1]) for line in lines if '%' in line] |
| 52 | + |
| 53 | + # load zenodo from master |
| 54 | + zenodo_file = Path('.zenodo.json') |
| 55 | + zenodo = json.loads(zenodo_file.read_text()) |
| 56 | + zen_names = [' '.join(val['name'].split(',')[::-1]).strip() |
| 57 | + for val in zenodo['creators']] |
| 58 | + total_names = len(zen_names) + len(MISSING_ENTRIES) |
| 59 | + |
| 60 | + name_matches = [] |
| 61 | + position = 1 |
| 62 | + for ele in data: |
| 63 | + matches = process.extract(ele, zen_names, scorer=fuzz.token_sort_ratio, |
| 64 | + limit=2) |
| 65 | + # matches is a list [('First match', % Match), ('Second match', % Match)] |
| 66 | + if matches[0][1] > 80: |
| 67 | + val = zenodo['creators'][zen_names.index(matches[0][0])] |
| 68 | + else: |
| 69 | + # skip unmatched names |
| 70 | + print("No entry to sort:", ele) |
| 71 | + continue |
| 72 | + |
| 73 | + if val not in name_matches: |
| 74 | + if val['name'] not in CREATORS_LAST: |
| 75 | + val['position'] = position |
| 76 | + position += 1 |
| 77 | + else: |
| 78 | + val['position'] = total_names + CREATORS_LAST.index(val['name']) |
| 79 | + name_matches.append(val) |
| 80 | + |
| 81 | + for missing in MISSING_ENTRIES: |
| 82 | + missing['position'] = position |
| 83 | + position += 1 |
| 84 | + name_matches.append(missing) |
| 85 | + |
| 86 | + zenodo['creators'] = sorted(name_matches, key=lambda k: k['position']) |
| 87 | + # Remove position |
| 88 | + for creator in zenodo['creators']: |
| 89 | + del creator['position'] |
| 90 | + |
| 91 | + zenodo_file.write_text('%s\n' % json.dumps(zenodo, indent=2, sort_keys=True)) |
0 commit comments