Add figure update script

This commit is contained in:
jaseg 2024-08-27 15:08:37 +02:00
parent 6d1a510304
commit 57307b5b0c
17 changed files with 108 additions and 0 deletions

View file

@ -0,0 +1 @@
https://git.jaseg.de/ihsm-secondary-mesh.git/plain/gear_plan_1.svg?h=3a7edbd1127cacc8f4c90376595b894105f3d479

View file

@ -0,0 +1 @@
gear_plan_1.svg

View file

@ -0,0 +1 @@
https://git.jaseg.de/ihsm-secondary-mesh.git/plain/gear_plan_2.svg?h=3a7edbd1127cacc8f4c90376595b894105f3d479

View file

@ -0,0 +1 @@
gear_plan_2.svg

View file

@ -0,0 +1 @@
https://git.jaseg.de/ihsm-secondary-mesh.git/plain/gear_plan_3.svg?h=3a7edbd1127cacc8f4c90376595b894105f3d479

View file

@ -0,0 +1 @@
gear_plan_3.svg

View file

@ -0,0 +1 @@
https://git.jaseg.de/ihsm-secondary-mesh.git/plain/render_exp_1.png?h=3a7edbd1127cacc8f4c90376595b894105f3d479

View file

@ -0,0 +1 @@
render_exp_1.png

View file

@ -0,0 +1 @@
https://git.jaseg.de/ihsm-secondary-mesh.git/plain/render_exp_2.png?h=3a7edbd1127cacc8f4c90376595b894105f3d479

View file

@ -0,0 +1 @@
render_exp_2.png

View file

@ -0,0 +1 @@
https://git.jaseg.de/ihsm-secondary-mesh.git/plain/render_side_1.png?h=3a7edbd1127cacc8f4c90376595b894105f3d479

View file

@ -0,0 +1 @@
render_side_1.png

View file

@ -0,0 +1 @@
https://git.jaseg.de/ihsm-secondary-mesh.git/plain/render_side_2.png?h=3a7edbd1127cacc8f4c90376595b894105f3d479

View file

@ -0,0 +1 @@
render_side_2.png

View file

@ -0,0 +1 @@
https://git.jaseg.de/ihsm-secondary-mesh.git/plain/schema_wire.svg?h=3a7edbd1127cacc8f4c90376595b894105f3d479

View file

@ -0,0 +1 @@
schema_wire.svg

View file

@ -0,0 +1,92 @@
#!/usr/bin/env python3
import subprocess
import sys
import fnmatch
import warnings
import filecmp
from pathlib import Path
import click
FILENAME_PATTERNS = ['*.svg', '*.png', '*.jpg', '*.pdf', '*.eps']
def git(*args, repo=None):
cmd = ['git']
if repo:
cmd += ['-C', str(repo)]
proc = subprocess.run([*cmd, *args], check=True, capture_output=True, text=True)
return proc.stdout + proc.stderr
def remote_url(repo=None):
branch = 'unknown'
try:
branch = git('branch', '--show-current', repo=repo).strip()
remote = git('config', f'branch.{branch}.remote', repo=repo).strip()
except subprocess.CalledProcessError:
warnings.warn(f'Cannot identify git remote for branch {branch} in repo {repo or "<this repo>"}, using first remote')
remote = git('remote', repo=repo).strip().splitlines()[0].strip()
if not remote:
raise SystemError(f'Repo {repo or "<this repo>"} has no remotes configured.')
return git('remote', 'get-url', remote, repo=repo).strip()
def paths(s):
return [Path(line.strip()) for line in s.strip().splitlines()]
@click.command()
@click.argument('figure_dir', default='.', type=click.Path(exists=True, file_okay=False, path_type=Path))
def cli(figure_dir):
figure_dir = figure_dir.resolve()
figure_files = set(fn.name for fn in figure_dir.iterdir()
if fn.is_file() and any(fnmatch.fnmatch(fn.name.lower(), pat) for pat in FILENAME_PATTERNS))
submodules = [p for p in paths(git('submodule', 'foreach', '--quiet', 'pwd'))
if p.parent == figure_dir]
repo_matches = {}
for mod in submodules:
for fn in paths(git('ls-tree', '-r', 'HEAD', '--name-only', '--full-tree', repo=mod)):
if fn.name in figure_files:
if fn.name in repo_matches:
prev_mod, prev_fn = repo_matches[fn.name]
warnings.warn(f'Ambiguous match for {fn.name}: found {mod / fn} in addition to previous match of {prev_mod / prev_fn}.')
else:
repo_matches[fn.name] = (mod, fn)
meta_file = lambda suffix: figure_dir / (fig_fn + suffix)
for fig_fn, (mod, mod_fn) in repo_matches.items():
fig_file = figure_dir / fig_fn
mod_file = mod / mod_fn
if filecmp.cmp(fig_file, mod_file):
print(f'{fig_fn} is up to date, updating metadata.')
else:
print(f'Updated {fig_fn} from {mod_fn}.')
shutil.copy(mod_file, fig_file)
meta_file('.git_path').write_text(str(mod_fn))
repo_url = remote_url(repo=mod)
meta_file('.git_remote').write_text(repo_url)
url_prefix = repo_url.replace('git@git.jaseg.de:', 'https://git.jaseg.de/')
git_rev = git('rev-parse', 'HEAD', repo=mod).strip()
cgit_url = f'{url_prefix}/plain/{mod_fn}?h={git_rev}'
meta_file('.git_hyperlink').write_text(cgit_url)
git_tag = git('describe', '--always', '--tags', repo=mod).strip()
meta_file('.git_rev').write_text(git_tag)
unmatched = figure_files - set(repo_matches.keys())
if unmatched:
print()
print('Unmatched files:', file=sys.stderr)
for fn in unmatched:
print(fn, file=sys.stderr)
if __name__ == '__main__':
cli()