Skip to content

Commit e2df544

Browse files
refactor(tools): read setup metadata through the shared catalog (#203)
Setup scripts and feature tests need the catalog’s version and checksum metadata. Keeping its preparation in the shared reader makes that data available independently of the container installer and keeps metadata rules together after #198. Move the metadata preparation to `catalog.load_setup_metadata()` and have the installer use it for setup environments and `shell-metadata`. Move the existing metadata assertion to the catalog tests and document the boundary. Existing shell exports and installation behavior are preserved. Part of #190; follows #196 and #198.
1 parent ffb59c3 commit e2df544

5 files changed

Lines changed: 51 additions & 49 deletions

File tree

‎tools/internal/README.md‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,10 @@ supported platforms, download URLs, checksums, and archive layouts.
8686
`rules_multitool` consumes the files for Bazel. The standard-library-only
8787
[`catalog.py`](catalog.py) reads metadata for
8888
[`devcontainer/install.py`](devcontainer/install.py) and README generation.
89-
The reader has no installation side effects; download, platform selection,
90-
and installation remain in the container installer.
89+
The reader also prepares version and checksum variables for setup scripts and
90+
feature tests through `load_setup_metadata()`. It has no installation side
91+
effects; shell quoting, download, platform selection and installation remain in
92+
the container installer.
9193
Other direct packages and special setup are defined in
9294
[`providers.json`](../providers.json): Python package pins, APT constraints,
9395
npm package identities, Bazel preinstallation and CodeQL metadata. The selections reference these names

‎tools/internal/catalog.py‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from __future__ import annotations
1818

1919
import json
20+
import re
2021
from collections.abc import Iterator
2122
from pathlib import Path
2223
from typing import NotRequired, TypedDict
@@ -323,3 +324,38 @@ def apt_requirements(
323324
package += "=" + tool["version"] + "*"
324325
requirements.append(package)
325326
return requirements
327+
328+
329+
def load_setup_metadata() -> dict[str, str]:
330+
"""Turn definition fields into variables such as codeql_version for setup and tests."""
331+
values: dict[str, str] = {}
332+
333+
def emit(name: str, value: object) -> None:
334+
if not re.fullmatch(r"[a-zA-Z_][a-zA-Z0-9_]*", name):
335+
raise SystemExit(f"Invalid metadata variable: {name}")
336+
if isinstance(value, dict):
337+
for key, child in value.items():
338+
emit(name + "_" + key, child)
339+
elif isinstance(value, str):
340+
values[name] = value
341+
342+
for name, definition in load_providers().items():
343+
# Use only version and checksum fields. Other data, such as platform
344+
# names containing slashes, cannot be used as shell variable names.
345+
for field in ("version", "compatibility_version", "amd64", "arm64"):
346+
if field in definition:
347+
emit(name.replace("-", "_") + "_" + field, definition[field])
348+
# Unpinned APT tools can still have expected versions for feature tests.
349+
# Give these expectations the same _version names as pinned tools.
350+
if "compatibility_version" in definition:
351+
emit(
352+
name.replace("-", "_") + "_version", definition["compatibility_version"]
353+
)
354+
# Native and Python command versions use the same variable names as the
355+
# other definitions, so feature tests need just one metadata query.
356+
for name, version in load_catalog_versions().items():
357+
emit(name.replace("-", "_") + "_version", version)
358+
# Python package and entrypoint metadata is also available to setup scripts.
359+
for name, definition in load_python_tools().items():
360+
emit(name.replace("-", "_"), definition)
361+
return values

‎tools/internal/devcontainer/install.py‎

Lines changed: 3 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
load_environments,
5454
load_python_tools,
5555
load_providers,
56+
load_setup_metadata,
5657
load_tool,
5758
resolve_lockfile,
5859
)
@@ -429,56 +430,19 @@ def _apply_environment(args, selection, definitions, setup_scripts) -> int:
429430
# Pass variables to the setup processes. Python cannot change the
430431
# environment of the shell that launched this installer.
431432
if "setup" in args.providers and setup_scripts:
432-
environment = {**os.environ, **_metadata_values()}
433+
environment = {**os.environ, **load_setup_metadata()}
433434
environment["SCORE_TOOL_ENVIRONMENT"] = args.environment
434435
environment["SCORE_TOOLS_INSTALLER"] = str(Path(__file__).resolve())
435436
for script in setup_scripts:
436437
subprocess.run(["bash", str(script)], check=True, env=environment)
437438
return 0
438439

439440

440-
def _metadata_values() -> dict[str, str]:
441-
"""Turn definition fields into variables such as codeql_version for setup and tests."""
442-
import re
443-
444-
values: dict[str, str] = {}
445-
446-
def emit(name, value):
447-
if not re.fullmatch(r"[a-zA-Z_][a-zA-Z0-9_]*", name):
448-
raise SystemExit(f"Invalid metadata variable: {name}")
449-
if isinstance(value, dict):
450-
for key, child in value.items():
451-
emit(name + "_" + key, child)
452-
elif isinstance(value, str):
453-
values[name] = value
454-
455-
for name, definition in load_providers().items():
456-
# Use only version and checksum fields. Other data, such as platform
457-
# names containing slashes, cannot be used as shell variable names.
458-
for field in ("version", "compatibility_version", "amd64", "arm64"):
459-
if field in definition:
460-
emit(name.replace("-", "_") + "_" + field, definition[field])
461-
# Unpinned APT tools can still have expected versions for feature tests.
462-
# Give these expectations the same _version names as pinned tools.
463-
if "compatibility_version" in definition:
464-
emit(
465-
name.replace("-", "_") + "_version", definition["compatibility_version"]
466-
)
467-
# Native and Python command versions use the same variable names as the
468-
# other definitions, so feature tests need just one metadata query.
469-
for name, version in load_catalog_versions().items():
470-
emit(name.replace("-", "_") + "_version", version)
471-
# Python package and entrypoint metadata is also available to setup scripts.
472-
for name, definition in load_python_tools().items():
473-
emit(name.replace("-", "_"), definition)
474-
return values
475-
476-
477441
def _cmd_shell_metadata(args: argparse.Namespace) -> int:
478442
"""Print shell-quoted exports so feature tests can read versions in their shell."""
479443
import shlex
480444

481-
for name, value in _metadata_values().items():
445+
for name, value in load_setup_metadata().items():
482446
print("export " + name + "=" + shlex.quote(value))
483447
return 0
484448

‎tools/tests/catalog_test.py‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@
3232

3333

3434
class CatalogTest(unittest.TestCase):
35+
def test_setup_metadata_includes_every_native_and_python_command_version(
36+
self,
37+
) -> None:
38+
# Feature tests should get all expected versions in one shell-metadata call.
39+
metadata = catalog.load_setup_metadata()
40+
for name, version in catalog.load_catalog_versions().items():
41+
self.assertEqual(metadata[name.replace("-", "_") + "_version"], version)
42+
3543
def test_metadata_matches_definitions(self) -> None:
3644
versions = {}
3745
descriptions = {}

‎tools/tests/environment_test.py‎

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -425,14 +425,6 @@ def test_default_lifecycle_stops_if_runtime_installation_fails(self) -> None:
425425
[call.args[0].phase for call in apply.call_args_list], ["build", "runtime"]
426426
)
427427

428-
def test_shell_metadata_includes_every_native_and_python_command_version(
429-
self,
430-
) -> None:
431-
# Feature tests should get all expected versions in one shell-metadata call.
432-
metadata = installer._metadata_values()
433-
for name, version in catalog.load_catalog_versions().items():
434-
self.assertEqual(metadata[name.replace("-", "_") + "_version"], version)
435-
436428
def test_apt_tool_setup_is_retained_when_installation_is_delegated(self) -> None:
437429
# Java is selected through APT, but also needs java.sh to set JAVA_HOME.
438430
# Both an image build and a normal user must execute that setup.

0 commit comments

Comments
 (0)