Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 88 additions & 23 deletions packages/desktop/scripts/native_release.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from __future__ import annotations

import argparse
import functools
import hashlib
import json
import os
Expand Down Expand Up @@ -353,6 +354,78 @@ def _require_existing_file(path: Path, *, label: str) -> Path:
return path


# Every git call here is a local read of history. The slowest takes well under a
# second, so one that runs this long is stuck.
GIT_TIMEOUT = 120


class GitTimeout(RuntimeError):
"""A git command did not finish in time."""


@functools.lru_cache(maxsize=None)
def _git_executable(search_path: str | None) -> str:
found = shutil.which("git", path=search_path)
if found is None:
raise FileNotFoundError("git is not on PATH")
return os.path.abspath(found)


def _hold_standard_descriptors() -> list[int]:
"""Occupy any of descriptors 0, 1 and 2 that are closed; return the ones opened."""

held: list[int] = []
if os.name != "posix":
return held
while True:
descriptor = os.open(os.devnull, os.O_RDWR)
if descriptor > 2:
os.close(descriptor)
return held
held.append(descriptor)


def run_git(
root: Path, *args: str, check: bool = True, input: bytes | None = None
) -> subprocess.CompletedProcess[bytes]:
"""Run one git command in ``root`` without forking this process.

On macOS a forked copy of a Python process that has loaded system
frameworks can stop before it ever runs git, and the parent then waits
inside Popen where no timeout applies. Python starts the child with
posix_spawn instead of fork when the program path is absolute, no working
directory is passed and inherited descriptors are left alone, so git gets
its directory from ``-C``. Python opens its own descriptors close-on-exec,
so git still inherits only its three pipes. The timeout then covers the
whole call, and a stuck command fails with its name.

Python also falls back to fork when one of git's pipes is numbered 0, 1 or
2, which happens when this process was started with a standard descriptor
closed. Those numbers are held open on the null device for the length of
the call so the pipes always get higher ones.
"""

command = [_git_executable(os.environ.get("PATH")), "-C", str(root), *args]
held = _hold_standard_descriptors()
try:
return subprocess.run(
command,
check=check,
capture_output=True,
input=input,
stdin=subprocess.DEVNULL if input is None else None,
close_fds=os.name != "posix",
timeout=GIT_TIMEOUT,
)
except subprocess.TimeoutExpired as exc:
raise GitTimeout(
f"git {' '.join(args)} did not finish within {GIT_TIMEOUT}s in {root}"
) from exc
finally:
for descriptor in held:
os.close(descriptor)


def _resolve_commit(root: Path, ref: str) -> str:
"""Resolve one caller ref to its 40-character commit id.

Expand All @@ -363,14 +436,8 @@ def _resolve_commit(root: Path, ref: str) -> str:

if not isinstance(ref, str) or not ref or ref.startswith("-"):
raise ValueError(f"invalid Git ref: {ref!r}")
result = subprocess.run(
["git", "rev-parse", "--verify", "--quiet", f"{ref}^{{commit}}"],
cwd=root,
check=False,
capture_output=True,
text=True,
)
commit = result.stdout.strip()
result = run_git(root, "rev-parse", "--verify", "--quiet", f"{ref}^{{commit}}", check=False)
commit = result.stdout.decode("utf-8", "replace").strip()
if result.returncode != 0 or not GIT_COMMIT_PATTERN.fullmatch(commit):
raise ValueError(f"Git ref {ref!r} does not name exactly one commit")
return commit
Expand All @@ -379,14 +446,9 @@ def _resolve_commit(root: Path, ref: str) -> str:
def _git_bytes(root: Path, commit: str, relative_path: str) -> bytes:
if not GIT_COMMIT_PATTERN.fullmatch(commit):
raise ValueError(f"expected a resolved commit id, got {commit!r}")
result = subprocess.run(
# "./" resolves the path from cwd=root, so this works whether root is
# the repository root or packages/desktop inside the OpenAdapt monorepo.
["git", "show", f"{commit}:./{relative_path}"],
cwd=root,
check=False,
capture_output=True,
)
# "./" resolves the path from root, so this works whether root is the
# repository root or packages/desktop inside the OpenAdapt monorepo.
result = run_git(root, "show", f"{commit}:./{relative_path}", check=False)
if result.returncode != 0:
raise ValueError(f"Git commit {commit!r} does not contain {relative_path!r}")
return result.stdout
Expand Down Expand Up @@ -436,17 +498,20 @@ def validate_git_version_transform(
raise ValueError(f"native version must be X.Y.Z, got {version!r}")
base_commit = _resolve_commit(root, base_ref)
candidate_commit = _resolve_commit(root, candidate_ref)
changed = subprocess.run(
# --relative: paths from root, limited to it (packages/desktop in the monorepo).
["git", "diff", "--relative", "--name-only", f"{base_commit}..{candidate_commit}", "--"],
cwd=root,
# --relative: paths from root, limited to it (packages/desktop in the monorepo).
changed = run_git(
root,
"diff",
"--relative",
"--name-only",
f"{base_commit}..{candidate_commit}",
"--",
check=False,
capture_output=True,
text=True,
)
if changed.returncode != 0:
raise ValueError("could not compare engine and native tag trees")
changed_paths = {line for line in changed.stdout.splitlines() if line}
listing = changed.stdout.decode("utf-8", "replace")
changed_paths = {line for line in listing.splitlines() if line}
unexpected = changed_paths.difference(VERSION_TRANSFORM_PATHS)
if unexpected:
raise ValueError(
Expand Down
53 changes: 3 additions & 50 deletions packages/desktop/scripts/prepare_release.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,8 @@
import copy
import datetime
import difflib
import functools
import json
import os
import re
import shutil
import subprocess
import sys
import tempfile
Expand Down Expand Up @@ -81,53 +78,9 @@ def breaking(self) -> bool:
return bool(match and match.group("bang")) or "BREAKING CHANGE:" in self.body


# Every git call here is a local read of history. The slowest takes well under a
# second, so one that runs this long is stuck.
GIT_TIMEOUT = 120


class GitTimeout(RuntimeError):
"""A git command did not finish in time."""


@functools.lru_cache(maxsize=None)
def _git_executable(search_path: str | None) -> str:
found = shutil.which("git", path=search_path)
if found is None:
raise FileNotFoundError("git is not on PATH")
return os.path.abspath(found)


def run_git(
root: Path, *args: str, check: bool = True, input: bytes | None = None
) -> subprocess.CompletedProcess[bytes]:
"""Run one git command in ``root`` without forking this process.

On macOS a forked copy of a Python process that has loaded system
frameworks can stop before it ever runs git, and the parent then waits
inside Popen where no timeout applies. Python starts the child with
posix_spawn instead of fork when the program path is absolute, no working
directory is passed and inherited descriptors are left alone, so git gets
its directory from ``-C``. Python opens its own descriptors close-on-exec,
so git still inherits only its three pipes. The timeout then covers the
whole call, and a stuck command fails with its name.
"""

command = [_git_executable(os.environ.get("PATH")), "-C", str(root), *args]
try:
return subprocess.run(
command,
check=check,
capture_output=True,
input=input,
stdin=subprocess.DEVNULL if input is None else None,
close_fds=os.name != "posix",
timeout=GIT_TIMEOUT,
)
except subprocess.TimeoutExpired as exc:
raise GitTimeout(
f"git {' '.join(args)} did not finish within {GIT_TIMEOUT}s in {root}"
) from exc
# One helper starts every git process here and in the tests; see its docstring.
GitTimeout = native_release.GitTimeout
run_git = native_release.run_git


def _git(root: Path, *args: str) -> str:
Expand Down
21 changes: 21 additions & 0 deletions packages/desktop/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from __future__ import annotations

import os
import subprocess
from pathlib import Path

import pytest
Expand Down Expand Up @@ -162,3 +163,23 @@ def no_usage_stats_or_update_check(monkeypatch):

monkeypatch.setenv("DO_NOT_TRACK", "1")
monkeypatch.setenv("OPENADAPT_UPDATE_CHECK", "false")


@pytest.fixture
def no_subprocess_fork(monkeypatch: pytest.MonkeyPatch) -> None:
"""Fail any subprocess that would fork pytest.

By the time the release tests run, earlier tests have loaded macOS system
frameworks into the pytest process. On the hosted macOS runners a forked
copy of that process sometimes never reached exec, and the parent waited
inside Popen until the job was cancelled. With this guard a call that
brings the fork back fails at once, on Linux as well as macOS.
"""

if not getattr(subprocess, "_USE_POSIX_SPAWN", False):
return

def refuse(args, *_rest, **_named):
raise AssertionError(f"{args!r} would fork pytest; run git through native_release.run_git")

monkeypatch.setattr(subprocess, "_fork_exec", refuse)
21 changes: 6 additions & 15 deletions packages/desktop/tests/test_native_release.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
native_version_at_ref,
native_versions,
python_distribution_names,
run_git,
set_native_version,
stage_artifacts,
sync_native_version_from_engine,
Expand All @@ -48,6 +49,9 @@
)

ROOT = Path(__file__).resolve().parents[1]
# These tests run in the same pytest process as the release-intent tests, so
# their git calls must not fork it either; see conftest.py.
pytestmark = pytest.mark.usefixtures("no_subprocess_fork")
# Desktop's workflows run from the OpenAdapt monorepo root.
MONOREPO_WORKFLOWS = ROOT.parents[1] / ".github" / "workflows"
WORKFLOW_FILES = {
Expand Down Expand Up @@ -557,14 +561,7 @@ def test_set_native_version_rejects_non_semver_input(tmp_path: Path) -> None:


def _git(tmp_path: Path, *args: str) -> str:
result = subprocess.run(
["git", *args],
cwd=tmp_path,
check=True,
capture_output=True,
text=True,
)
return result.stdout.strip()
return run_git(tmp_path, *args).stdout.decode("utf-8").strip()


def test_git_version_transform_requires_the_exact_reconstructed_tree(tmp_path: Path) -> None:
Expand Down Expand Up @@ -1416,13 +1413,7 @@ def test_git_helpers_refuse_an_option_shaped_ref(tmp_path: Path) -> None:


def test_native_version_at_ref_reads_git_objects_not_the_working_tree() -> None:
head = subprocess.run(
["git", "rev-parse", "HEAD"],
cwd=ROOT,
check=True,
capture_output=True,
text=True,
).stdout.strip()
head = _git(ROOT, "rev-parse", "HEAD")

assert native_version_at_ref(head) == native_version()
assert native_version_at_ref("HEAD") == native_version()
Expand Down
Loading
Loading