|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Build the libtommath PDF manual from LaTeX sources |
| 3 | +
|
| 4 | +This script is called by meson with the following environment variables set: |
| 5 | +LATEX, PDFLATEX, MAKEINDEX |
| 6 | +
|
| 7 | +Only requires standard Python library - no external tools beyond LaTeX itself. |
| 8 | +""" |
| 9 | + |
| 10 | +import os |
| 11 | +import sys |
| 12 | +import shutil |
| 13 | +import subprocess |
| 14 | +import re |
| 15 | +from datetime import datetime, timezone |
| 16 | + |
| 17 | + |
| 18 | +def main(): |
| 19 | + if len(sys.argv) != 4: |
| 20 | + print("Usage: build-manual.py INPUT OUTDIR OUTPUT", file=sys.stderr) |
| 21 | + sys.exit(1) |
| 22 | + |
| 23 | + input_file = sys.argv[1] |
| 24 | + outdir = sys.argv[2] |
| 25 | + output_file = sys.argv[3] |
| 26 | + |
| 27 | + # Get tools from environment |
| 28 | + latex = os.environ.get('LATEX', 'latex') |
| 29 | + pdflatex = os.environ.get('PDFLATEX', 'pdflatex') |
| 30 | + makeindex = os.environ.get('MAKEINDEX', 'makeindex') |
| 31 | + |
| 32 | + # Convert paths to absolute before changing directories |
| 33 | + input_file = os.path.abspath(input_file) |
| 34 | + output_file = os.path.abspath(output_file) |
| 35 | + |
| 36 | + # Change to output directory |
| 37 | + os.chdir(outdir) |
| 38 | + |
| 39 | + # Copy input to bn.tex |
| 40 | + shutil.copy2(input_file, 'bn.tex') |
| 41 | + |
| 42 | + # Create backup with same timestamp |
| 43 | + shutil.copy2('bn.tex', 'bn.bak') |
| 44 | + |
| 45 | + # Get modification time of bn.tex and format for PDF |
| 46 | + mtime = os.stat('bn.tex').st_mtime |
| 47 | + dt = datetime.fromtimestamp(mtime, tz=timezone.utc) |
| 48 | + |
| 49 | + # Format as PDF date string: D:YYYYMMDDHHmmSS+HH'mm' |
| 50 | + # For UTC, timezone offset is +00'00' |
| 51 | + date_str = dt.strftime("D:%Y%m%d%H%M%S+00'00'") |
| 52 | + |
| 53 | + # Write deterministic PDF header |
| 54 | + with open('bn-deterministic.tex', 'w') as f: |
| 55 | + f.write(f"\\def\\fixedpdfdate{{{date_str}}}\n") |
| 56 | + f.write("\\pdfinfo{\n") |
| 57 | + f.write(" /CreationDate (\\fixedpdfdate)\n") |
| 58 | + f.write(" /ModDate (\\fixedpdfdate)\n") |
| 59 | + f.write("}\n") |
| 60 | + |
| 61 | + # Append original content |
| 62 | + with open('bn.tex', 'r') as orig: |
| 63 | + f.write(orig.read()) |
| 64 | + |
| 65 | + # Replace bn.tex with deterministic version |
| 66 | + shutil.move('bn-deterministic.tex', 'bn.tex') |
| 67 | + |
| 68 | + # Restore original timestamp |
| 69 | + shutil.copystat('bn.bak', 'bn.tex') |
| 70 | + |
| 71 | + # Build the manual |
| 72 | + with open('bn.ind', 'w') as f: |
| 73 | + f.write('hello\n') |
| 74 | + |
| 75 | + subprocess.run([latex, 'bn'], stdout=subprocess.DEVNULL, check=True) |
| 76 | + subprocess.run([latex, 'bn'], stdout=subprocess.DEVNULL, check=True) |
| 77 | + subprocess.run([makeindex, 'bn'], check=True) |
| 78 | + subprocess.run([latex, 'bn'], stdout=subprocess.DEVNULL, check=True) |
| 79 | + subprocess.run([pdflatex, 'bn'], stdout=subprocess.DEVNULL, check=True) |
| 80 | + |
| 81 | + # Make PDF ID deterministic using Python |
| 82 | + with open('bn.pdf', 'rb') as f: |
| 83 | + pdf_data = f.read() |
| 84 | + |
| 85 | + # Replace /ID [<...> <...>] with /ID [<0> <0>] |
| 86 | + pdf_data = re.sub(rb'^/ID \[.*\]$', rb'/ID [<0> <0>]', pdf_data, flags=re.MULTILINE) |
| 87 | + |
| 88 | + with open('bn.pdf', 'wb') as f: |
| 89 | + f.write(pdf_data) |
| 90 | + |
| 91 | + # Rename to desired output name |
| 92 | + shutil.move('bn.pdf', output_file) |
| 93 | + |
| 94 | + # Cleanup |
| 95 | + shutil.move('bn.bak', 'bn.tex') |
| 96 | + |
| 97 | + cleanup_files = [ |
| 98 | + 'bn.aux', 'bn.dvi', 'bn.log', 'bn.idx', |
| 99 | + 'bn.lof', 'bn.out', 'bn.toc', 'bn.ilg', |
| 100 | + 'bn.ind', 'bn.tex' |
| 101 | + ] |
| 102 | + for f in cleanup_files: |
| 103 | + try: |
| 104 | + os.remove(f) |
| 105 | + except FileNotFoundError: |
| 106 | + pass |
| 107 | + |
| 108 | + |
| 109 | +if __name__ == '__main__': |
| 110 | + main() |
0 commit comments