Skip to content

Commit 2d8e8ae

Browse files
committed
github action to pull REMARKs
1 parent b5d0bf3 commit 2d8e8ae

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
name: Copy REMARK metadata
2+
on:
3+
push:
4+
branches:
5+
- master
6+
permissions:
7+
contents: write
8+
jobs:
9+
copy:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- uses: actions/checkout@v4
13+
with:
14+
ref: master
15+
- name: set up Python
16+
uses: actions/setup-python@v5
17+
with:
18+
python-version: '3.11.x'
19+
- name: install dependencies
20+
run: |
21+
python -m pip install --upgrade pip
22+
python -m pip install pyyaml==6.0.0
23+
- name: copy metadata
24+
run: python scripts/populate_materials.py
25+
- name: update gh-pages branch
26+
run: |
27+
git config --global user.name github-actions
28+
git config --global user.email [email protected]
29+
git checkout --orphan gh-pages
30+
git add .
31+
git commit -m "autopull REMARKs $(date)"
32+
git push --force origin gh-pages

scripts/populate_materials.py

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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

Comments
 (0)