|
| 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 |
| 6 | +import json |
| 7 | +from fuzzywuzzy import fuzz, process |
| 8 | +import subprocess as sp |
| 9 | + |
| 10 | +# These ORCIDs should go last |
| 11 | +CREATORS_LAST = ['Rokem, Ariel', 'Esteban, Oscar'] |
| 12 | +# for entries not found in line-contributions |
| 13 | +MISSING_ENTRIES = [ |
| 14 | +] |
| 15 | + |
| 16 | +if __name__ == '__main__': |
| 17 | + contrib_file = Path('line-contributors.txt') |
| 18 | + lines = [] |
| 19 | + if contrib_file.exists(): |
| 20 | + print('WARNING: Reusing existing line-contributors.txt file.', file=sys.stderr) |
| 21 | + lines = contrib_file.read_text().splitlines() |
| 22 | + |
| 23 | + git_line_summary_path = shutil.which('git-line-summary') |
| 24 | + if not lines and git_line_summary_path: |
| 25 | + print("Running git-line-summary on nipype repo") |
| 26 | + lines = sp.check_output([git_line_summary_path]).decode().splitlines() |
| 27 | + contrib_file.write_text('\n'.join(lines)) |
| 28 | + |
| 29 | + if not lines: |
| 30 | + raise RuntimeError("""\ |
| 31 | +Could not find line-contributors from git repository.%s""" % """ \ |
| 32 | +git-line-summary not found, please install git-extras. """ * (git_line_summary_path is None)) |
| 33 | + |
| 34 | + data = [' '.join(line.strip().split()[1:-1]) for line in lines if '%' in line] |
| 35 | + |
| 36 | + # load zenodo from master |
| 37 | + zenodo_file = Path('.zenodo.json') |
| 38 | + zenodo = json.loads(zenodo_file.read_text()) |
| 39 | + zen_names = [' '.join(val['name'].split(',')[::-1]).strip() |
| 40 | + for val in zenodo['creators']] |
| 41 | + total_names = len(zen_names) + len(MISSING_ENTRIES) |
| 42 | + |
| 43 | + name_matches = [] |
| 44 | + position = 1 |
| 45 | + for ele in data: |
| 46 | + matches = process.extract(ele, zen_names, scorer=fuzz.token_sort_ratio, |
| 47 | + limit=2) |
| 48 | + # matches is a list [('First match', % Match), ('Second match', % Match)] |
| 49 | + if matches[0][1] > 80: |
| 50 | + val = zenodo['creators'][zen_names.index(matches[0][0])] |
| 51 | + else: |
| 52 | + # skip unmatched names |
| 53 | + print("No entry to sort:", ele) |
| 54 | + continue |
| 55 | + |
| 56 | + if val not in name_matches: |
| 57 | + if val['name'] not in CREATORS_LAST: |
| 58 | + val['position'] = position |
| 59 | + position += 1 |
| 60 | + else: |
| 61 | + val['position'] = total_names + CREATORS_LAST.index(val['name']) |
| 62 | + name_matches.append(val) |
| 63 | + |
| 64 | + for missing in MISSING_ENTRIES: |
| 65 | + missing['position'] = position |
| 66 | + position += 1 |
| 67 | + name_matches.append(missing) |
| 68 | + |
| 69 | + zenodo['creators'] = sorted(name_matches, key=lambda k: k['position']) |
| 70 | + # Remove position |
| 71 | + for creator in zenodo['creators']: |
| 72 | + del creator['position'] |
| 73 | + |
| 74 | + zenodo_file.write_text('%s\n' % json.dumps(zenodo, indent=2, sort_keys=True)) |
0 commit comments