m/market labResearch, with a record.
Source on GitHub ↗

build_source_release.py

1"""Build a public source archive from one Git commit, or an explicit local candidate.23Default: python tools/build_source_release.py --ref HEAD4For uncommitted review only: add --working-tree. Neither mode publishes anything.5"""67import argparse8import ast9import gzip10import io11import json12from pathlib import Path, PurePosixPath13import subprocess14import tarfile1516ROOT = Path(__file__).resolve().parents[1]17PROVENANCE = 'SOURCE_PROVENANCE.json'18SOURCE_DIRECTORIES = {19    '.github',20    'src',21    'include',22    'sim_platform',23    'examples',24    'tests',25    'tools',26    'docs',27}28SOURCE_FILES = {29    'README.md',30    'LICENSE',31    'NOTICE',32    'THIRD_PARTY_NOTICES.md',33    'SECURITY.md',34    'CONTRIBUTING.md',35    'GOVERNANCE.md',36    'CITATION.cff',37    'CHANGELOG.md',38    'CMakeLists.txt',39    'Makefile',40    'pyproject.toml',41    'setup.py',42    'MANIFEST.in',43    'requirements.txt',44    'requirements-dev.txt',45    'requirements-desktop.txt',46    'requirements-packaging.txt',47    'requirements-site.txt',48    'requirements-demo.txt',49    '.gitignore',50    '.clang-format',51    '.gitattributes',52}535455def public_path(name):56    """Allow public roots, excluding local state; reject noncanonical archive paths."""57    path = PurePosixPath(name)58    if not name or path.is_absolute() or path.as_posix() != name or '\\' in name:59        return False60    if any(61        p in ('.', '..', '.git', '__pycache__', 'node_modules', '.DS_Store') for p in path.parts62    ):63        return False64    if any(p.startswith('.env') for p in path.parts):65        return False66    if path.suffix in ('.bin', '.gz', '.csv', '.pem', '.key', '.pyc') or '.sqlite' in path.name:67        return False68    return name in SOURCE_FILES or path.parts[0] in SOURCE_DIRECTORIES697071def git(root, *args):72    # core.autocrlf=false: git archive must return the committed bytes on every OS.73    return subprocess.run(74        ['git', '-c', 'core.autocrlf=false', '-C', str(root), *args],75        check=True,76        capture_output=True,77    ).stdout787980def source_files(root=ROOT):81    """List the explicit working-tree candidate; never use this for commit exports."""82    names = git(root, 'ls-files', '--cached', '--others', '--exclude-standard', '-z')83    for name in sorted(set(names.decode().split('\0')) - {''}):84        if not public_path(name):85            continue86        path = root / name87        for parent in (path, *path.parents):88            if parent.is_symlink():89                raise ValueError('Source export cannot contain symlinks: ' + name)90            if parent == root:91                break92        if path.is_file():93            yield Path(name)949596def collect(root, ref, working_tree):97    commit = git(root, 'rev-parse', '--verify', ref + '^{commit}').decode().strip()98    if working_tree:99        files = {100            p.as_posix(): ((root / p).read_bytes(), (root / p).stat().st_mode & 0o111 != 0)101            for p in source_files(root)102        }103    else:104        files = {}105        with tarfile.open(106            fileobj=io.BytesIO(git(root, 'archive', '--format=tar', commit))107        ) as archive:108            for member in archive:109                if not public_path(member.name) or member.isdir():110                    continue111                if not member.isfile():112                    raise ValueError('Source export requires regular files: ' + member.name)113                files[member.name] = (archive.extractfile(member).read(), bool(member.mode & 0o111))114    return commit, files115116117def product_version(content):118    """Read literal product metadata without importing application code."""119    for node in ast.parse(content).body:120        if isinstance(node, ast.Assign) and any(121            isinstance(target, ast.Name) and target.id == '__version__' for target in node.targets122        ):123            version = ast.literal_eval(node.value)124            if (125                not isinstance(version, str)126                or not version127                or any(c not in '0123456789.-abcdefghijklmnopqrstuvwxyz' for c in version)128            ):129                break130            return version131    raise ValueError('Product version is missing or invalid')132133134def build(output, *, root=ROOT, ref='HEAD', working_tree=False):135    """Write a deterministic commit archive; label working-tree exports as candidates."""136    commit, files = collect(root, ref, working_tree)137    required = {138        'README.md',139        'LICENSE',140        'NOTICE',141        'sim_platform/__init__.py',142        'sim_platform/desktop/web/frontend/index.html',143    }144    if not required.issubset(files):145        raise ValueError('Required release files are missing')146    version = product_version(files['sim_platform/__init__.py'][0])147    provenance = {148        'source_commit': commit,149        'source_kind': 'working-tree-candidate' if working_tree else 'commit',150        'product_version': version,151    }152    files[PROVENANCE] = ((json.dumps(provenance, indent=2) + '\n').encode(), False)153    output.mkdir(parents=True, exist_ok=True)154    label = '-candidate' if working_tree else ''155    destination = output / ('market-lab-' + version + label + '-source.tar.gz')156    temporary = destination.with_suffix('.tmp')157    try:158        with (159            temporary.open('wb') as raw,160            gzip.GzipFile(filename='', fileobj=raw, mode='wb', mtime=0) as compressed,161        ):162            with tarfile.open(fileobj=compressed, mode='w') as archive:163                for name, (content, executable) in sorted(files.items()):164                    info = tarfile.TarInfo('market-lab-' + version + '/' + name)165                    info.size = len(content)166                    info.mode = 0o755 if executable else 0o644167                    archive.addfile(info, io.BytesIO(content))168        temporary.replace(destination)169    finally:170        temporary.unlink(missing_ok=True)171    print(destination)172    print(json.dumps(provenance, sort_keys=True))173    return destination174175176if __name__ == '__main__':177    parser = argparse.ArgumentParser(description=__doc__)178    parser.add_argument('--output', type=Path, default=ROOT / 'build' / 'releases')179    parser.add_argument(180        '--ref', default='HEAD', help='Exact source commit/ref; uncommitted files are excluded'181    )182    parser.add_argument(183        '--working-tree',184        action='store_true',185        help='Export uncommitted contents for local review only',186    )187    args = parser.parse_args()188    if args.working_tree and args.ref != 'HEAD':189        parser.error('--working-tree cannot be combined with another --ref')190    build(args.output.resolve(), ref=args.ref, working_tree=args.working_tree)