| | @@ -6,6 +6,7 @@ from pathlib import Path |
| 6 | 6 | import shutil |
| 7 | 7 | import subprocess |
| 8 | 8 | import sys |
| 9 | +추가: import tomllib |
| 9 | 10 | |
| 10 | 11 | |
| 11 | 12 | ROOT = Path(__file__).resolve().parents[2] |
| | @@ -13,6 +14,9 @@ TOOLCHAIN = "+1.99.1" |
| 13 | 14 | WINDOWS_TARGET = "x86_64-pc-windows-gnu" |
| 14 | 15 | LINUX_ONLY = {"rust-ui-system-font-regressions"} |
| 15 | 16 | JOB_NAME = re.compile(r"[A-Za-z0-9][A-Za-z0-9_-]{0,63}\Z") |
| 17 | +추가: LITERAL_SOURCE_PATH = re.compile( |
| 18 | +추가: r'(?:\binclude(?:_str|_bytes)?!\s*\(\s*|#\s*\[\s*path\s*=\s*)"([^"]+)"' |
| 19 | +추가: ) |
| 16 | 20 | |
| 17 | 21 | |
| 18 | 22 | def work_directory(root, name): |
| | @@ -70,6 +74,123 @@ def manifests(root): |
| 70 | 74 | return paths |
| 71 | 75 | |
| 72 | 76 | |
| 77 | +추가: def changed_files(root): |
| 78 | +추가: try: |
| 79 | +추가: base = subprocess.check_output( |
| 80 | +추가: ["git", "merge-base", "origin/main", "HEAD"], cwd=root, text=True |
| 81 | +추가: ).strip() |
| 82 | +추가: tracked = subprocess.check_output( |
| 83 | +추가: ["git", "diff", "--no-renames", "--name-only", "-z", base], cwd=root |
| 84 | +추가: ) |
| 85 | +추가: untracked = subprocess.check_output( |
| 86 | +추가: ["git", "ls-files", "--others", "--exclude-standard", "-z"], cwd=root |
| 87 | +추가: ) |
| 88 | +추가: except subprocess.CalledProcessError as error: |
| 89 | +추가: raise ValueError("origin/main과 변경 파일을 비교할 수 없음") from error |
| 90 | +추가: changes = {os.fsdecode(path) for path in (tracked + untracked).split(b"\0") if path} |
| 91 | +추가: work = os.environ.get("ZIPELO_TEST_WORK_DIR") |
| 92 | +추가: if work and Path(work).resolve() == root.resolve(): |
| 93 | +추가: outputs = {"target", "tools", "tmp", "fixtures"} |
| 94 | +추가: changes = { |
| 95 | +추가: path for path in changes |
| 96 | +추가: if path.split("/", 1)[0] not in outputs |
| 97 | +추가: and not re.fullmatch(r"(local|build|test|cross-build)-results\.json", path) |
| 98 | +추가: } |
| 99 | +추가: return changes |
| 100 | +추가: |
| 101 | +추가: |
| 102 | +추가: def path_dependencies(document): |
| 103 | +추가: for key, value in document.items(): |
| 104 | +추가: if key in {"dependencies", "dev-dependencies", "build-dependencies"}: |
| 105 | +추가: for dependency in value.values(): |
| 106 | +추가: if isinstance(dependency, dict) and isinstance(dependency.get("path"), str): |
| 107 | +추가: yield dependency["path"] |
| 108 | +추가: elif isinstance(value, dict): |
| 109 | +추가: yield from path_dependencies(value) |
| 110 | +추가: |
| 111 | +추가: |
| 112 | +추가: def manifest_sources(root, manifest, document): |
| 113 | +추가: sources = {manifest.parent.resolve()} |
| 114 | +추가: for section in ("lib", "bin", "test", "bench", "example"): |
| 115 | +추가: entries = document.get(section, []) |
| 116 | +추가: if isinstance(entries, dict): |
| 117 | +추가: entries = [entries] |
| 118 | +추가: for entry in entries: |
| 119 | +추가: path = entry.get("path") |
| 120 | +추가: if isinstance(path, str): |
| 121 | +추가: source = (manifest.parent / path).resolve().parent |
| 122 | +추가: if source.is_relative_to(root): |
| 123 | +추가: sources.add(source) |
| 124 | +추가: return sources |
| 125 | +추가: |
| 126 | +추가: |
| 127 | +추가: def literal_source_inputs(root, directories): |
| 128 | +추가: pending = [ |
| 129 | +추가: source |
| 130 | +추가: for directory in directories |
| 131 | +추가: for source in directory.rglob("*.rs") |
| 132 | +추가: ] |
| 133 | +추가: checked = set() |
| 134 | +추가: inputs = set() |
| 135 | +추가: while pending: |
| 136 | +추가: source = pending.pop().resolve() |
| 137 | +추가: if source in checked or not source.is_relative_to(root) or not source.is_file(): |
| 138 | +추가: continue |
| 139 | +추가: checked.add(source) |
| 140 | +추가: for relative in LITERAL_SOURCE_PATH.findall(source.read_text(encoding="utf-8")): |
| 141 | +추가: dependency = (source.parent / relative).resolve() |
| 142 | +추가: if dependency.is_relative_to(root): |
| 143 | +추가: inputs.add(dependency) |
| 144 | +추가: if dependency.suffix == ".rs": |
| 145 | +추가: pending.append(dependency) |
| 146 | +추가: return inputs |
| 147 | +추가: |
| 148 | +추가: |
| 149 | +추가: def select_manifests(root, paths, changes): |
| 150 | +추가: root = root.resolve() |
| 151 | +추가: documents = { |
| 152 | +추가: manifest: tomllib.loads(manifest.read_text(encoding="utf-8")) |
| 153 | +추가: for manifest in paths |
| 154 | +추가: } |
| 155 | +추가: owners = { |
| 156 | +추가: manifest: manifest_sources(root, manifest, document) |
| 157 | +추가: for manifest, document in documents.items() |
| 158 | +추가: } |
| 159 | +추가: literal_inputs = { |
| 160 | +추가: manifest: literal_source_inputs(root, directories) |
| 161 | +추가: for manifest, directories in owners.items() |
| 162 | +추가: } |
| 163 | +추가: by_directory = {manifest.parent.resolve(): manifest for manifest in paths} |
| 164 | +추가: dependents = {manifest: set() for manifest in paths} |
| 165 | +추가: for manifest, document in documents.items(): |
| 166 | +추가: for dependency in path_dependencies(document): |
| 167 | +추가: target = (manifest.parent / dependency).resolve() |
| 168 | +추가: if target in by_directory: |
| 169 | +추가: dependents[by_directory[target]].add(manifest) |
| 170 | +추가: |
| 171 | +추가: selected = set() |
| 172 | +추가: for change in changes: |
| 173 | +추가: changed_path = (root / change).resolve() |
| 174 | +추가: direct = { |
| 175 | +추가: manifest for manifest, directories in owners.items() |
| 176 | +추가: if any(changed_path.is_relative_to(directory) for directory in directories) |
| 177 | +추가: or changed_path in literal_inputs[manifest] |
| 178 | +추가: } |
| 179 | +추가: if direct: |
| 180 | +추가: selected.update(direct) |
| 181 | +추가: elif change.startswith("docs/") or change.endswith(".md") or change == "LICENSE": |
| 182 | +추가: continue |
| 183 | +추가: else: |
| 184 | +추가: return paths |
| 185 | +추가: |
| 186 | +추가: pending = list(selected) |
| 187 | +추가: while pending: |
| 188 | +추가: for dependent in dependents[pending.pop()] - selected: |
| 189 | +추가: selected.add(dependent) |
| 190 | +추가: pending.append(dependent) |
| 191 | +추가: return [manifest for manifest in paths if manifest in selected] |
| 192 | +추가: |
| 193 | +추가: |
| 73 | 194 | def command(manifest, phase): |
| 74 | 195 | arguments = [ |
| 75 | 196 | "cargo", |
| | @@ -98,8 +219,8 @@ def prepare_fixtures(root): |
| 98 | 219 | return skipped |
| 99 | 220 | |
| 100 | 221 | |
| 101 | | -삭제: def run(root, phase): |
| 102 | | -삭제: paths = manifests(root) |
| 222 | +추가: def run(root, phase, selected=None): |
| 223 | +추가: paths = manifests(root) if selected is None else selected |
| 103 | 224 | skipped = {} |
| 104 | 225 | if phase == "test" and sys.platform == "linux": |
| 105 | 226 | skipped = prepare_fixtures(root) |
| | @@ -197,13 +318,13 @@ def prepare_local_tools(root): |
| 197 | 318 | prepare_cross_build() |
| 198 | 319 | |
| 199 | 320 | |
| 200 | | -삭제: def execute_phases(root, phases): |
| 321 | +추가: def execute_phases(root, phases, selected=None): |
| 201 | 322 | results = [] |
| 202 | 323 | for phase in phases: |
| 203 | 324 | try: |
| 204 | | -삭제: if phase == "cross-build": |
| 325 | +추가: if phase == "cross-build" and selected != []: |
| 205 | 326 | prepare_cross_build() |
| 206 | | -삭제: rows = run(root, phase) |
| 327 | +추가: rows = run(root, phase, selected) |
| 207 | 328 | except (OSError, ValueError, subprocess.CalledProcessError) as error: |
| 208 | 329 | rows = [{"manifest": "preflight", "status": "failed", "reason": str(error)}] |
| 209 | 330 | for row in rows: |
| | @@ -215,12 +336,16 @@ def main(): |
| 215 | 336 | parser = argparse.ArgumentParser(description="로컬 Linux 시험과 Windows 교차 빌드") |
| 216 | 337 | parser.add_argument("phase", choices=["local", "build", "test", "cross-build"]) |
| 217 | 338 | parser.add_argument("--job", default=os.environ.get("ZIPELO_CI_JOB", "rust-local")) |
| 339 | +추가: parser.add_argument("--changed", action="store_true", |
| 340 | +추가: help="origin/main과 비교해 영향받는 crate만 실행함") |
| 218 | 341 | args = parser.parse_args() |
| 219 | 342 | if args.phase == "local" and sys.platform != "linux": |
| 220 | 343 | parser.error("local 모드는 Linux에서 실행해야 함") |
| 221 | 344 | work = work_directory(ROOT, args.job) |
| 222 | 345 | temporary = output_directory(work, "tmp", create=True) |
| 223 | 346 | target = output_directory(work, "target") |
| 347 | +추가: cargo = output_directory(output_directory(work, "tools", create=True), |
| 348 | +추가: "cargo", create=True) |
| 224 | 349 | os.environ["ZIPELO_CI_JOB"] = args.job |
| 225 | 350 | os.environ["ZIPELO_TEST_WORK_DIR"] = str(work) |
| 226 | 351 | os.environ["TMPDIR"] = str(temporary) |
| | @@ -229,8 +354,16 @@ def main(): |
| 229 | 354 | os.environ["PYTHONDONTWRITEBYTECODE"] = "1" |
| 230 | 355 | os.environ.setdefault("CARGO_BUILD_JOBS", "2") |
| 231 | 356 | os.environ["CARGO_TARGET_DIR"] = str(target) |
| 357 | +추가: os.environ["CARGO_HOME"] = str(cargo) |
| 232 | 358 | prepare_tools(ROOT) |
| 233 | 359 | phases = ["build", "test", "cross-build"] if args.phase == "local" else [args.phase] |
| 360 | +추가: selected = None |
| 361 | +추가: selection_error = None |
| 362 | +추가: if args.changed: |
| 363 | +추가: try: |
| 364 | +추가: selected = select_manifests(ROOT, manifests(ROOT), changed_files(ROOT)) |
| 365 | +추가: except (OSError, ValueError) as error: |
| 366 | +추가: selection_error = str(error) |
| 234 | 367 | checks = subprocess.run( |
| 235 | 368 | [sys.executable, "-m", "unittest", "discover", "-s", "tests/ci", "-p", "test_*.py"], |
| 236 | 369 | cwd=ROOT, check=False, |
| | @@ -242,9 +375,14 @@ def main(): |
| 242 | 375 | "status": "failed", |
| 243 | 376 | "exit_code": checks.returncode, |
| 244 | 377 | }] |
| 378 | +추가: elif selection_error is not None: |
| 379 | +추가: results = [{ |
| 380 | +추가: "phase": "selection", "manifest": "preflight", |
| 381 | +추가: "status": "failed", "reason": selection_error, |
| 382 | +추가: }] |
| 245 | 383 | else: |
| 246 | 384 | try: |
| 247 | | -삭제: if args.phase == "local": |
| 385 | +추가: if args.phase == "local" and selected != []: |
| 248 | 386 | prepare_local_tools(ROOT) |
| 249 | 387 | except (OSError, ValueError, subprocess.CalledProcessError) as error: |
| 250 | 388 | results = [{ |
| | @@ -252,12 +390,16 @@ def main(): |
| 252 | 390 | "status": "failed", "reason": str(error), |
| 253 | 391 | }] |
| 254 | 392 | else: |
| 255 | | -삭제: results = execute_phases(ROOT, phases) |
| 393 | +추가: results = execute_phases(ROOT, phases, selected) |
| 256 | 394 | report = { |
| 257 | 395 | "head": subprocess.check_output(["git", "rev-parse", "HEAD"], cwd=ROOT, text=True).strip(), |
| 258 | 396 | "platform": sys.platform, |
| 259 | 397 | "windows_cross_target": WINDOWS_TARGET if "cross-build" in phases else None, |
| 260 | 398 | "windows_runtime_tested": checks.returncode == 0 and sys.platform == "win32" and "test" in phases, |
| 399 | +추가: "changed_only": args.changed, |
| 400 | +추가: "selected_manifests": [ |
| 401 | +추가: path.relative_to(ROOT).as_posix() for path in selected |
| 402 | +추가: ] if selected is not None else None, |
| 261 | 403 | "results": results, |
| 262 | 404 | } |
| 263 | 405 | (work / f"{args.phase}-results.json").write_text( |