fix(services): clear service-repo bytecode with git pathspecs before a pull

Delete untracked __pycache__ before each pull and drop the global dont_write_bytecode, so service bytecode is cached again
This commit is contained in:
imSp4rky
2026-10-24 15:57:08 -06:00
parent cd9785d406
commit 34e8e37fdc
4 changed files with 85 additions and 72 deletions
+22 -66
View File
@@ -32,57 +32,24 @@ def stamp_for(dest: Path) -> Path:
SHORTHAND_RE = re.compile(r"^[\w.-]+/[\w.-]+$")
# Importing services rewrites tracked bytecode when upstream mistakenly commits
# ``__pycache__`` (e.g. playtv). That must not block refresh / look like source edits.
_JUNK_NAME_SUFFIXES = (".pyc", ".pyo")
_JUNK_BASENAMES = frozenset({".DS_Store"})
def is_junk_path(path: str) -> bool:
"""True for import/OS noise that is never a meaningful service-source edit."""
normalized = path.replace("\\", "/").strip()
if not normalized:
return False
parts = normalized.split("/")
if "__pycache__" in parts:
return True
name = parts[-1]
if name in _JUNK_BASENAMES:
return True
return name.endswith(_JUNK_NAME_SUFFIXES)
def porcelain_paths(porcelain: str) -> list[str]:
"""Paths from ``git status --porcelain`` lines (handles rename ``old -> new``)."""
paths: list[str] = []
for raw in porcelain.splitlines():
if len(raw) < 4:
continue
# XY<space>path or XY<space>old -> new
entry = raw[3:]
if " -> " in entry:
entry = entry.split(" -> ", 1)[1]
# quoted paths are rare here; strip optional quotes
if len(entry) >= 2 and entry[0] == entry[-1] == '"':
entry = entry[1:-1]
paths.append(entry)
return paths
JUNK_GLOBS = ("**/__pycache__/**", "**/*.pyc", "**/*.pyo", "**/.DS_Store")
JUNK = tuple(f":(glob){g}" for g in JUNK_GLOBS)
NOT_JUNK = (".", *(f":(exclude,glob){g}" for g in JUNK_GLOBS))
def discard_junk_workdir(dest: Path) -> None:
"""Restore tracked junk paths so ``git pull`` is not blocked by rewritten ``.pyc`` files."""
if not binaries.Git:
return
"""Restore tracked bytecode the import rewrote and delete untracked ``__pycache__`` files, so
neither can block ``git pull --ff-only``. It never touches compiled ``.so``/``.pyd`` modules."""
git = str(binaries.Git)
status = subprocess.run(
[git, "-C", str(dest), "status", "--porcelain", "--untracked-files=no"], capture_output=True
subprocess.run([git, "-C", str(dest), "clean", "-fdqx", "--", JUNK[0]], capture_output=True) # nosec B603
changed = subprocess.run([git, "-C", str(dest), "diff", "--name-only", "-z", "--", *JUNK], capture_output=True) # nosec B603
if not changed.stdout:
return
subprocess.run(
[git, "--literal-pathspecs", "-C", str(dest), "checkout", "--pathspec-from-file=-", "--pathspec-file-nul"],
input=changed.stdout,
capture_output=True,
) # nosec B603
if status.returncode != 0 or not status.stdout.strip():
return
junk = [p for p in porcelain_paths(status.stdout.decode(errors="ignore")) if is_junk_path(p)]
if not junk:
return
subprocess.run([git, "-C", str(dest), "checkout", "--", *junk], capture_output=True) # nosec B603
def is_repo_spec(value: object) -> bool:
@@ -173,8 +140,6 @@ def pull(dest: Path, stamp: Path) -> None:
if not binaries.Git:
write_stamp(stamp)
return
# Drop rewritten bytecode before the dirty check / pull so a tracked ``.pyc`` cannot
# false-trigger DirtyServiceRepo or block ``git pull --ff-only``.
discard_junk_workdir(dest)
# never clobber local edits: refuse to refresh a dirty clone and let the caller exit cleanly
if is_dirty(dest):
@@ -215,16 +180,12 @@ def head(dest: Path) -> Optional[str]:
def local_dirty_services(dest: Path) -> list[str]:
"""Top-level service dirs with uncommitted edits to tracked files (lost on a force reset)."""
r = subprocess.run([str(binaries.Git), "-C", str(dest), "diff", "--name-only", "HEAD"], capture_output=True) # nosec B603
r = subprocess.run(
[str(binaries.Git), "-C", str(dest), "diff", "--name-only", "HEAD", "--", *NOT_JUNK], capture_output=True
) # nosec B603
if r.returncode != 0:
return []
return sorted(
{
line.split("/")[0]
for line in r.stdout.decode(errors="ignore").splitlines()
if line and not is_junk_path(line)
}
)
return sorted({line.split("/")[0] for line in r.stdout.decode(errors="ignore").splitlines() if line})
def changed_services(dest: Path, old: Optional[str], new: Optional[str]) -> list[str]:
@@ -266,20 +227,15 @@ def force_sync(dest: Path, stamp: Path) -> None:
def is_dirty(dest: Path) -> bool:
"""True if the clone has uncommitted edits to tracked files or local commits not on its upstream.
Ignores untracked files (``git pull`` does not remove them) and tracked import/OS noise
(``__pycache__/``, ``*.pyc`` / ``*.pyo``, ``.DS_Store``). Upstream sometimes commits a
``.pyc``; importing under another interpreter rewrites it and must not look like a
service-source edit.
This function ignores untracked files, tracked bytecode and ``.DS_Store``: importing a service
rewrites a committed ``.pyc`` whose source modification time changed on checkout.
"""
git = str(binaries.Git)
# modifications to tracked files only (--untracked-files=no skips untracked __pycache__ etc.)
status = subprocess.run(
[git, "-C", str(dest), "status", "--porcelain", "--untracked-files=no"], capture_output=True
[git, "-C", str(dest), "status", "--porcelain", "--untracked-files=no", "--", *NOT_JUNK], capture_output=True
) # nosec B603
if status.returncode == 0:
meaningful = [p for p in porcelain_paths(status.stdout.decode(errors="ignore")) if not is_junk_path(p)]
if meaningful:
return True
if status.returncode == 0 and status.stdout.strip():
return True
# local commits ahead of upstream (committed but not pushed); no upstream → treat as clean
ahead = subprocess.run([git, "-C", str(dest), "rev-list", "--count", "@{u}..HEAD"], capture_output=True) # nosec B603
if ahead.returncode == 0:
-4
View File
@@ -28,10 +28,6 @@ log = logging.getLogger("services")
DIRTY_REPOS: list[str] = []
SHADOWED: list[str] = []
# Service clones live inside the tree; writing bytecode there rewrites tracked ``.pyc``
# files (when upstream committed them) and floods untracked ``__pycache__`` dirs.
sys.dont_write_bytecode = True
def discover_services() -> tuple[list[Path], list[str]]:
"""Resolve the configured service dirs and glob every ``TAG/__init__.py``.