Files
absdev-deployment/scripts/build-release.py
T
admin d6e2317016
ABSDEV Framework CI / framework-validation (push) Canceled after 0s
Initial ABSDEV deployment framework development preview
2026-11-26 11:35:28 +00:00

227 lines
5.1 KiB
Python
Executable File

#!/usr/bin/env python3
"""Reproducible development-preview source packaging."""
import argparse
import gzip
import hashlib
import io
import os
import re
import subprocess
import tarfile
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
DIRECTORIES = {
"core", "adapters", "tests", "config",
"bin", "scripts", "docs", ".gitea",
}
TOP_LEVEL = {
"README.md", "CHANGELOG.md",
"VERSION", ".gitignore",
}
EXTENSIONS = {
".py", ".sh", ".json",
".md", ".yaml", ".yml",
}
DENIED = {
"runtime", "reference", "__pycache__",
".git", "backups", "secrets",
".env", "credentials", "credentials.json",
"secrets.json", "auth.json",
"id_rsa", "id_ed25519",
}
def require(condition, message):
if not condition:
raise RuntimeError(message)
def source_files():
raw = subprocess.check_output([
"git", "ls-files", "-z",
"--cached", "--others",
"--exclude-standard",
], cwd=ROOT)
names = sorted(set(
os.fsdecode(item)
for item in raw.split(b"\0")
if item
))
files = []
for name in names:
relative = Path(name)
require(
not relative.is_absolute()
and ".." not in relative.parts
and not any(
part.lower() in DENIED
for part in relative.parts
),
f"Unsafe source path: {name}",
)
require(
all(
re.fullmatch(r"[A-Za-z0-9_.-]+", part)
for part in relative.parts
),
f"Invalid filename: {name}",
)
require(
name in TOP_LEVEL
or (
relative.parts[0] in DIRECTORIES
and (
relative.suffix in EXTENSIONS
or name == "bin/absdev-deploy"
)
),
f"Unapproved source: {name}",
)
path = ROOT / relative
require(
path.is_file()
and not path.is_symlink()
and path.resolve() == path
and path.stat().st_size <= 10485760,
f"Unsafe source file: {name}",
)
files.append((name, path))
for required in (
"README.md",
"CHANGELOG.md",
"VERSION",
"bin/absdev-deploy",
"config/site.example.json",
"scripts/ci-check.sh",
"scripts/build-release.py",
"docs/ARCHITECTURE.md",
"docs/PRODUCTION-READINESS.md",
".gitea/workflows/framework-ci.yaml",
):
require(
required in names,
f"Missing source: {required}",
)
return files
def build(output):
version = (ROOT / "VERSION").read_text().strip()
require(
re.fullmatch(
r"[0-9]+\.[0-9]+\.[0-9]+-dev\.[0-9]+",
version,
) is not None,
"Development-preview version required",
)
prefix = "absdev-deployment-" + version
target = output / (prefix + ".tar.gz")
require(
not os.path.lexists(target)
and not os.path.lexists(output / "SHA256SUMS"),
"Release already exists",
)
files = source_files()
payloads = []
for name, path in files:
content = path.read_bytes()
mode = 0o755 if path.stat().st_mode & 0o111 else 0o644
payloads.append((name, content, mode))
manifest = "".join(
hashlib.sha256(content).hexdigest()
+ " " + name + "\n"
for name, content, mode in payloads
).encode()
payloads.append(
("SOURCE.sha256", manifest, 0o644)
)
with target.open("xb") as raw:
with gzip.GzipFile(
filename="",
fileobj=raw,
mode="wb",
mtime=0,
compresslevel=9,
) as compressed:
with tarfile.open(
fileobj=compressed,
mode="w",
format=tarfile.PAX_FORMAT,
) as archive:
for name, content, mode in payloads:
entry = tarfile.TarInfo(
prefix + "/" + name
)
entry.size = len(content)
entry.mode = mode
entry.uid = 0
entry.gid = 0
entry.uname = ""
entry.gname = ""
entry.mtime = 0
archive.addfile(
entry, io.BytesIO(content)
)
with target.open("rb") as handle:
checksum = hashlib.file_digest(
handle, "sha256"
).hexdigest()
with (output / "SHA256SUMS").open("x") as handle:
handle.write(
checksum + " " + target.name + "\n"
)
print(f"Source files: {len(files)}")
print(f"Package: {target}")
print(f"SHA256: {checksum}")
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"--output", required=True, type=Path
)
args = parser.parse_args()
require(
args.output.is_dir(),
"Output directory must exist",
)
build(args.output)