Files
likang 7ddc3c22c5
CI / typecheck + test + build (push) Has been cancelled
commit
2026-11-16 17:50:15 +08:00

115 lines
5.3 KiB
Python
Executable File

#!/usr/bin/env python3
"""Create a backend-only upload bundle; exclude credentials, tasks and EDA binaries."""
import argparse
import hashlib
from pathlib import Path
import re
import shutil
import subprocess
import tarfile
import tempfile
import urllib.request
ROOT = Path(__file__).resolve().parents[1]
DEPLOY_FILES = (
'.env.example', 'Dockerfile', 'Dockerfile.dockerignore', 'compose.yaml',
'docker-entrypoint.sh', 'deploy.sh', 'requirements.txt', 'instructions.md',
'DEPLOY.md', 'THIRD-PARTY.md',
'build-assets/README.md',
)
API_ROOT = Path('backend/easyeda_backend/skills/easyeda-api')
def routing_assets():
# Read the release pin from the Dockerfile so packaging and building agree.
dockerfile = (ROOT / 'codex_pcb/Dockerfile').read_text()
checksum = re.search(r'^ARG FREEROUTING_SHA256=([0-9a-f]{64})$', dockerfile, re.M).group(1)
cache = ROOT / 'dist/pcb-backend-downloads'
cache.mkdir(parents=True, exist_ok=True)
files = []
for name, argument in (('freerouting.jar', 'FREEROUTING_URL'), ('LICENSE.freerouting', 'FREEROUTING_LICENSE_URL')):
path = cache / name
if path.is_file() and (name != 'freerouting.jar' or hashlib.sha256(path.read_bytes()).hexdigest() == checksum):
files.append(path)
continue
url = re.search(r'^ARG ' + argument + r'=(https://\S+)$', dockerfile, re.M).group(1)
print(f'Downloading {name} from the pinned upstream release...', flush=True)
with tempfile.NamedTemporaryFile(dir=cache, delete=False) as stream:
pending = Path(stream.name)
try:
curl = shutil.which('curl')
if curl:
subprocess.run([curl, '--fail', '--location', '--silent', '--show-error',
'--retry', '3', '--connect-timeout', '15', '--max-time', '180',
'--output', str(pending), url], check=True)
else:
with urllib.request.urlopen(url, timeout=60) as response, pending.open('wb') as stream:
shutil.copyfileobj(response, stream)
if name == 'freerouting.jar' and hashlib.sha256(pending.read_bytes()).hexdigest() != checksum:
raise ValueError('Freerouting checksum mismatch; no upload bundle was changed')
if not pending.stat().st_size:
raise ValueError(f'Empty download: {name}')
if name == 'LICENSE.freerouting' and b'GNU GENERAL PUBLIC LICENSE' not in pending.read_bytes():
raise ValueError('Freerouting license download is not a GPL license')
pending.replace(path)
finally:
pending.unlink(missing_ok=True)
files.append(path)
return files
def package(output, with_freerouting=False):
assets = routing_assets() if with_freerouting else []
output = output.expanduser().resolve()
output.parent.mkdir(parents=True, exist_ok=True)
with tempfile.TemporaryDirectory(prefix='pcb-backend-package-') as temporary:
bundle = Path(temporary) / 'vibemake-pcb-backend'
code = bundle / 'codex_pcb'
code.mkdir(parents=True)
sources = [ROOT / 'codex_pcb' / name for name in DEPLOY_FILES]
sources += sorted((ROOT / 'codex_pcb').glob('*.py'))
for source in sources:
target = code / source.relative_to(ROOT / 'codex_pcb')
target.parent.mkdir(parents=True, exist_ok=True)
shutil.copy2(source, target)
for source in assets:
shutil.copy2(source, code / 'build-assets' / source.name)
shutil.copy2(ROOT / 'LICENSE', bundle / 'LICENSE')
api = bundle / API_ROOT
api.mkdir(parents=True)
shutil.copytree(ROOT / API_ROOT / 'references', api / 'references')
shutil.copy2(ROOT / API_ROOT / 'SKILL.md', api / 'SKILL.md')
if not (api / 'references/_index.md').is_file():
raise ValueError('The standalone EDA API reference index is missing')
vendor = bundle / 'vendor/lceda-pro'
vendor.mkdir(parents=True)
(vendor / 'PLACE-LINUX-APP-HERE.txt').write_text(
'Place the complete Linux EasyEDA Pro application directory here.\n'
'The configured executable must be /opt/lceda-pro/lceda-pro.\n'
'See codex_pcb/DEPLOY.md. No application binary is bundled.\n'
)
# Write next to the destination, so the final replacement is atomic.
with tempfile.NamedTemporaryFile(dir=output.parent, suffix='.tar.gz', delete=False) as stream:
pending = Path(stream.name)
try:
with tarfile.open(pending, 'w:gz') as archive:
archive.add(bundle, arcname=bundle.name)
pending.replace(output)
finally:
pending.unlink(missing_ok=True)
return output
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('--output', type=Path, default=ROOT / 'dist/vibemake-pcb-backend.tar.gz')
parser.add_argument('--with-freerouting', action='store_true', help='Include the verified JAR and license so the server need not download from GitHub')
args = parser.parse_args()
output = package(args.output, with_freerouting=args.with_freerouting)
print(f'Backend upload bundle: {output} ({output.stat().st_size:,} bytes)')
if __name__ == '__main__':
main()