"""Build native CLI artifacts. Invoke on each target OS; never cross-label.""" import hashlib import importlib.metadata import json import os from pathlib import Path import platform import re import shutil import subprocess import sysconfig import tarfile ROOT = Path(__file__).resolve().parent.parent def main(): os.chdir(ROOT) version = (ROOT / 'VERSION').read_text().strip() if not re.fullmatch(r'0|[1-9][0-9]*', version.split('.')[0]) or not re.fullmatch(r'(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)', version): raise SystemExit('VERSION must be a stable SemVer version') if f"VERSION = '{version}'" not in (ROOT / 'pages').read_text(): raise SystemExit('VERSION and pages disagree') system = platform.system().lower() arch = {'x86_64':'amd64','AMD64':'amd64','arm64':'arm64','aarch64':'arm64'}.get(platform.machine()) if system not in ('linux','darwin') or arch not in ('amd64','arm64'): raise SystemExit('Native Windows credential/locking port is not yet validated; no unsupported binary will be published') subprocess.run(['python','-m','PyInstaller','--clean','--noconfirm','--onefile','--name','pages','pages'],check=True) binary = ROOT / 'dist/pages' output = subprocess.check_output([str(binary),'--version'],text=True).strip() if output != 'pages-cli '+version: raise SystemExit('Binary version mismatch') subprocess.run([str(binary),'task','--help'],check=True,stdout=subprocess.DEVNULL) subprocess.run([str(binary),'contacts','--help'],check=True,stdout=subprocess.DEVNULL) release=ROOT/'release' if release.exists():shutil.rmtree(release) release.mkdir() target=f'{system}-{arch}' name=f'pages-cli-v{version}-{target}' with tarfile.open(release/(name+'.tar.gz'),'w:gz') as tar: tar.add(binary,arcname='pages') for path in ('README.md','DISTRIBUTION.md','THIRD_PARTY.md','VERSION'): tar.add(ROOT/path,arcname=path) python_license=Path(sysconfig.get_path('stdlib'))/'LICENSE.txt' if not python_license.is_file():raise SystemExit('Missing Python license') tar.add(python_license,arcname='licenses/Python-LICENSE.txt') dist=importlib.metadata.distribution('pyinstaller') found=False for path in dist.files or []: if 'COPYING' in str(path) or 'LICENSE' in str(path): candidate=Path(dist.locate_file(path)) if candidate.is_file():tar.add(candidate,arcname='licenses/PyInstaller/'+str(path));found=True if not found:raise SystemExit('Missing PyInstaller license') for copyright_file in Path('/usr/share/doc').glob('*/copyright'): tar.add(copyright_file,arcname='licenses/system/'+copyright_file.parent.name+'/copyright') shutil.copy2(binary,release/(name+('.elf' if system=='linux' else ''))) # The source script remains available to Python users independently of the bundle. shutil.copy2(ROOT/'pages',release/f'pages-v{version}.py') metadata={'version':version,'target':target,'python':platform.python_version(),'libc':platform.libc_ver(),'source_revision':os.environ.get('SOURCE_REVISION','')} (release/(name+'.json')).write_text(json.dumps(metadata,indent=2)+'\n') hashes=[] for path in sorted(release.iterdir()): if path.name.endswith('SHA256SUMS'): continue hashes.append(hashlib.sha256(path.read_bytes()).hexdigest()+' '+path.name) (release/(name+'-SHA256SUMS')).write_text('\n'.join(hashes)+'\n') if __name__=='__main__':main()