#!/usr/bin/env python3 """Stage source and restore files for the Linux amd64 Agent or Probe image build.""" from __future__ import annotations import argparse import shutil from pathlib import Path ROOT = Path(__file__).resolve().parents[1] BACKEND = ROOT / "backend" IGNORED_PARTS = {".git", ".release", "artifacts", "bin", "obj", "TestResults", "tests"} GLOBAL_BACKEND_FILES = { "Directory.Build.props", "Directory.Build.targets", "Directory.Packages.props", "global.json", "NuGet.Config", "NuGet.config", } # The container stages backend as a standalone tree. Copy the repository-wide # SDK and package versions into it so restore/publish use the same configuration. ROOT_BUILD_FILES = {"Directory.Packages.props", "global.json"} CONTAINERFILES = { "agent": ROOT / "packaging/container/Agent.NativeBuild.Containerfile", "probe": ROOT / "packaging/container/Probe.NativeBuild.Containerfile", } def stage(component: str, destination: Path) -> None: if destination.exists(): shutil.rmtree(destination) restore = destination / "restore/backend" source = destination / "source/backend" restore.mkdir(parents=True) source.mkdir(parents=True) shutil.copy2(CONTAINERFILES[component], destination / "Containerfile") for path in BACKEND.rglob("*"): if not path.is_file() or IGNORED_PARTS.intersection(path.parts): continue relative = path.relative_to(BACKEND) if ( path.suffix in {".csproj", ".props", ".targets", ".slnx"} or path.name in GLOBAL_BACKEND_FILES or path.name == "packages.lock.json" ): target = restore / relative target.parent.mkdir(parents=True, exist_ok=True) shutil.copy2(path, target) target = source / relative target.parent.mkdir(parents=True, exist_ok=True) shutil.copy2(path, target) for name in ROOT_BUILD_FILES: for directory in (restore, source): shutil.copy2(ROOT / name, directory / name) config = destination / "config" config.mkdir() (config / ".keep").write_text("", encoding="utf-8") # 更新清单和请求放在这里;空的命名卷第一次挂载时会带上这个目录 (config / "update").mkdir() (config / "update" / ".keep").write_text("", encoding="utf-8") def main() -> None: parser = argparse.ArgumentParser() parser.add_argument("component", choices=tuple(CONTAINERFILES)) parser.add_argument("destination", type=Path) args = parser.parse_args() stage(args.component, args.destination) if __name__ == "__main__": main()