|
| 1 | +from subprocess import run |
| 2 | +from tempfile import TemporaryDirectory |
| 3 | +from pathlib import Path |
| 4 | +from time import sleep |
| 5 | +from yaml import safe_load, dump as yaml_dump |
| 6 | +from concurrent.futures import ThreadPoolExecutor, as_completed |
| 7 | +from io import StringIO |
| 8 | + |
| 9 | +def parse_yaml_header(f): |
| 10 | + metadata = StringIO() |
| 11 | + # advance to metadata section |
| 12 | + for line in f: |
| 13 | + if line.strip() == '---': |
| 14 | + break |
| 15 | + # copy metadata section |
| 16 | + for line in f: |
| 17 | + if line.strip() == '---': |
| 18 | + break |
| 19 | + metadata.write(line) |
| 20 | + metadata.seek(0) |
| 21 | + return safe_load(metadata), f |
| 22 | + |
| 23 | +repo_root = Path(__file__).parents[1] |
| 24 | + |
| 25 | +with TemporaryDirectory() as d: |
| 26 | + tmpdir = Path(d) |
| 27 | + run(['git', 'clone', '--filter=tree:0', 'https://github.com/econ-ark/REMARK'], cwd=tmpdir) |
| 28 | + |
| 29 | + remotes = {} |
| 30 | + for yml_path in tmpdir.glob('REMARK/REMARKs/*yml'): |
| 31 | + with open(yml_path) as f: |
| 32 | + metadata = safe_load(f) |
| 33 | + remotes[yml_path.stem] = metadata['remote'] |
| 34 | + |
| 35 | + futures = {} |
| 36 | + with ThreadPoolExecutor(4) as pool: |
| 37 | + for name, uri in remotes.items(): |
| 38 | + futures[name] = pool.submit( |
| 39 | + lambda name, uri: run(['git', 'clone', '--sparse', uri], cwd=tmpdir), |
| 40 | + name=name, uri=uri |
| 41 | + ) |
| 42 | + |
| 43 | + for name, result in futures.items(): |
| 44 | + result.result() |
| 45 | + cff = tmpdir / name / 'CITATION.cff' |
| 46 | + if not cff.exists(): |
| 47 | + continue |
| 48 | + with open(cff) as f: |
| 49 | + remark_data = safe_load(f) |
| 50 | + |
| 51 | + if (tmpdir / name / 'REMARK.md').exists(): |
| 52 | + with open(tmpdir / name / 'REMARK.md') as f: |
| 53 | + mdata, f = parse_yaml_header(f) |
| 54 | + body = f.read() |
| 55 | + remark_data.update(mdata) |
| 56 | + |
| 57 | + with open(repo_root / '_materials' / f'{name}.md', 'w') as f: |
| 58 | + f.write('---\n') |
| 59 | + yaml_dump(remark_data, f, default_flow_style=False) |
| 60 | + f.write('---\n') |
| 61 | + f.write(body) |
| 62 | + |
0 commit comments