File size: 3,185 Bytes
9126e0d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
"""Compile vector figures and the unmodified Springer Nature journal template.
Uses an existing TeX installation; the fallback only creates a local format/map
when a workspace runtime has TeX sources but no configured format database.
"""
from pathlib import Path
import os,subprocess,argparse,shutil
ROOT=Path(__file__).resolve().parents[1]
def environment():
    env=os.environ.copy()
    tmp=ROOT/'build/tmp';tmp.mkdir(parents=True,exist_ok=True);env['TMPDIR']=str(tmp)
    if subprocess.run(['kpsewhich','article.cls'],capture_output=True,text=True).stdout.strip():return env
    tex=Path('/usr/share/texlive/texmf-dist')
    if not (tex/'tex/latex/base/article.cls').exists():raise RuntimeError('Install a standard TeX Live distribution with pgfplots, standalone and Springer dependencies')
    aux=ROOT/'build/texlocal';aux.mkdir(parents=True,exist_ok=True)
    env.update(TEXMF=str(tex),TEXMFVAR=str(aux),TEXMFCONFIG=str(aux),TEXFORMATS=str(aux)+'//:',TEXFONTMAPS=str(aux)+'//:'+str(tex/'fonts/map')+'//:')
    if not (aux/'pdflatex.fmt').exists():
        run(['pdftex','-ini','-interaction=batchmode','-halt-on-error','-jobname=pdflatex','-progname=pdflatex','-output-directory='+str(aux),'-etex','pdflatex.ini'],ROOT,env)
    maps=tex/'fonts/map/dvips';files=list((maps/'amsfonts').glob('*.map'))+[maps/'helvetic/uhv.map',maps/'psnfss/psnfss.map']
    (aux/'pdftex.map').write_text('\n'.join(f.read_text() for f in files))
    return env
def run(cmd,cwd,env):
    p=subprocess.run(cmd,cwd=cwd,env=env,capture_output=True,text=True)
    if p.returncode:
        print(p.stdout[-4000:]);print(p.stderr[-1000:])
        if cmd[0]=='pdflatex':
            log=Path(cwd)/Path(cmd[-1]).with_suffix('.log')
            if log.exists():print(log.read_text(errors='replace')[-5000:])
        raise RuntimeError('Command failed: '+' '.join(cmd))
    return p
def main():
    a=argparse.ArgumentParser();a.add_argument('--figures-only',action='store_true');a.add_argument('--manuscript-only',action='store_true');args=a.parse_args();env=environment()
    figout=ROOT/'build/verified_figures';figout.mkdir(parents=True,exist_ok=True)
    for f in ([] if args.manuscript_only else sorted((ROOT/'figures').glob('Fig*.tex'))):
        run(['pdflatex','-interaction=nonstopmode','-halt-on-error','-output-directory='+str(figout),f.name],f.parent,env)
        import fitz
        pdf=figout/f.with_suffix('.pdf').name
        with fitz.open(pdf) as doc:assert doc.page_count==1,f.name
        shutil.copy2(pdf,f.with_suffix('.pdf'));print('Compiled',f.name,flush=True)
        # Keep the journal-facing PDF as the canonical vector artifact.
        # EPS conversion is intentionally omitted: the available Poppler
        # wrapper is not reliable in this runtime.
    main=ROOT/'latex/RefSeg_CA_MVA.tex'
    if not args.figures_only and main.exists():
        run(['pdflatex','-interaction=nonstopmode','-halt-on-error',main.name],main.parent,env)
        run(['bibtex',main.stem],main.parent,env)
        for _ in range(2):run(['pdflatex','-interaction=nonstopmode','-halt-on-error',main.name],main.parent,env)
        print('Compiled manuscript',flush=True)
if __name__=='__main__':main()