|
| 1 | +#!/usr/bin/env python3 |
| 2 | +from __future__ import annotations |
| 3 | + |
| 4 | +import argparse |
| 5 | +import hashlib |
| 6 | +import io |
| 7 | +import os.path |
| 8 | +import secrets |
| 9 | +import subprocess |
| 10 | +import tarfile |
| 11 | +import tempfile |
| 12 | +import urllib.request |
| 13 | + |
| 14 | +# matches libxml2 from lxml==5.3.0 |
| 15 | +XML2_URL = "https://download.gnome.org/sources/libxml2/2.12/libxml2-2.12.9.tar.xz" |
| 16 | +XML2_SHA256 = "59912db536ab56a3996489ea0299768c7bcffe57169f0235e7f962a91f483590" |
| 17 | +# matches xmlsec from xmlsec==1.3.14 |
| 18 | +XMLSEC_URL = ( |
| 19 | + "https://github.com/lsh123/xmlsec/releases/download/1.3.4/xmlsec1-1.3.4.tar.gz" |
| 20 | +) |
| 21 | +XMLSEC_SHA256 = "45ad9078d41ae76844ad2f8651600ffeec0fdd128ead988a8d69e907c57aee75" |
| 22 | + |
| 23 | + |
| 24 | +def _join_env( |
| 25 | + *, |
| 26 | + name: str, |
| 27 | + value: str, |
| 28 | + sep: str, |
| 29 | +) -> str: |
| 30 | + if name in os.environ: |
| 31 | + return f"{value}{sep}{os.environ[name]}" |
| 32 | + else: |
| 33 | + return value |
| 34 | + |
| 35 | + |
| 36 | +def main() -> int: |
| 37 | + parser = argparse.ArgumentParser() |
| 38 | + parser.add_argument("prefix") |
| 39 | + args = parser.parse_args() |
| 40 | + |
| 41 | + os.environ["PATH"] = _join_env( |
| 42 | + name="PATH", |
| 43 | + value=os.path.join(args.prefix, "bin"), |
| 44 | + sep=os.pathsep, |
| 45 | + ) |
| 46 | + os.environ["CPPFLAGS"] = _join_env( |
| 47 | + name="CPPFLAGS", |
| 48 | + value=f'-I{os.path.join(args.prefix, "include")}', |
| 49 | + sep=" ", |
| 50 | + ) |
| 51 | + os.environ["LDFLAGS"] = _join_env( |
| 52 | + name="LDFLAGS", |
| 53 | + value=f'-L{os.path.join(args.prefix, "lib")}', |
| 54 | + sep=" ", |
| 55 | + ) |
| 56 | + os.environ["LD_LIBRARY_PATH"] = _join_env( |
| 57 | + name="LD_LIBRARY_PATH", |
| 58 | + value=os.path.join(args.prefix, "lib"), |
| 59 | + sep=os.pathsep, |
| 60 | + ) |
| 61 | + os.environ["PKG_CONFIG_PATH"] = _join_env( |
| 62 | + name="PKG_CONFIG_PATH", |
| 63 | + value=os.path.join(args.prefix, "lib", "pkgconfig"), |
| 64 | + sep=os.pathsep, |
| 65 | + ) |
| 66 | + |
| 67 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 68 | + resp = urllib.request.urlopen(XML2_URL) |
| 69 | + bts = resp.read() |
| 70 | + h = hashlib.sha256(bts).hexdigest() |
| 71 | + if not secrets.compare_digest(h, XML2_SHA256): |
| 72 | + raise SystemExit(f"checksum mismatch: {(XML2_SHA256, h)=}") |
| 73 | + |
| 74 | + with tarfile.open(fileobj=io.BytesIO(bts)) as tarf: |
| 75 | + tarf.extractall(tmpdir) |
| 76 | + h = hashlib.sha256(bts).hexdigest() |
| 77 | + |
| 78 | + srcroot = os.path.join(tmpdir, "libxml2-2.12.9") |
| 79 | + subprocess.check_call( |
| 80 | + ( |
| 81 | + "./configure", |
| 82 | + f"--prefix={args.prefix}", |
| 83 | + "--disable-silent-rules", |
| 84 | + "--with-history", |
| 85 | + "--with-icu", |
| 86 | + "--without-python", |
| 87 | + "--without-lzma", |
| 88 | + ), |
| 89 | + cwd=srcroot, |
| 90 | + ) |
| 91 | + subprocess.check_call(("make", "install"), cwd=srcroot) |
| 92 | + |
| 93 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 94 | + resp = urllib.request.urlopen(XMLSEC_URL) |
| 95 | + bts = resp.read() |
| 96 | + h = hashlib.sha256(bts).hexdigest() |
| 97 | + if not secrets.compare_digest(h, XMLSEC_SHA256): |
| 98 | + raise SystemExit(f"checksum mismatch: {(XMLSEC_SHA256, h)=}") |
| 99 | + |
| 100 | + with tarfile.open(fileobj=io.BytesIO(bts)) as tarf: |
| 101 | + tarf.extractall(tmpdir) |
| 102 | + |
| 103 | + srcroot = os.path.join(tmpdir, "xmlsec1-1.3.4") |
| 104 | + subprocess.check_call( |
| 105 | + ( |
| 106 | + "./configure", |
| 107 | + "--disable-dependency-tracking", |
| 108 | + f"--prefix={args.prefix}", |
| 109 | + "--disable-crypto-dl", |
| 110 | + "--disable-apps-crypto-dl", |
| 111 | + "--with-nss=no", |
| 112 | + "--with-nspr=no", |
| 113 | + "--enable-mscrypto=no", |
| 114 | + "--enable-mscng=no", |
| 115 | + ), |
| 116 | + cwd=srcroot, |
| 117 | + ) |
| 118 | + subprocess.check_call(("make", "install"), cwd=srcroot) |
| 119 | + |
| 120 | + return 0 |
| 121 | + |
| 122 | + |
| 123 | +if __name__ == "__main__": |
| 124 | + raise SystemExit(main()) |
0 commit comments