Skip to content

Commit 304dee2

Browse files
jfrocheyvan-sraka
authored andcommitted
chore: improve github matrix script type annotations
1 parent f5340bf commit 304dee2

File tree

1 file changed

+12
-31
lines changed

1 file changed

+12
-31
lines changed

scripts/github-matrix.py

Lines changed: 12 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,12 @@ class RunsOnConfig(TypedDict):
4343

4444

4545
class GitHubActionPackage(TypedDict):
46-
"""Processed package for GitHub Actions matrix."""
46+
"""Final package output for GitHub Actions matrix."""
4747

4848
attr: str
4949
name: str
5050
system: str
51-
already_cached: bool
5251
runs_on: RunsOnConfig
53-
drvPath: str
54-
neededSubstitutes: List[str]
55-
neededBuilds: List[str]
5652
postgresql_version: NotRequired[str]
5753

5854

@@ -94,9 +90,7 @@ def build_nix_eval_command(max_workers: int, flake_outputs: List[str]) -> List[s
9490
return nix_eval_cmd
9591

9692

97-
def parse_nix_eval_line(
98-
line: str, drv_paths: Set[str]
99-
) -> Optional[GitHubActionPackage]:
93+
def parse_nix_eval_line(line: str, drv_paths: Set[str]) -> Optional[NixEvalJobsOutput]:
10094
"""Parse a single line of nix-eval-jobs output"""
10195
if not line.strip():
10296
return None
@@ -106,25 +100,13 @@ def parse_nix_eval_line(
106100
if data["drvPath"] in drv_paths:
107101
return None
108102
drv_paths.add(data["drvPath"])
109-
110-
runs_on_config = BUILD_RUNNER_MAP[data["system"]]
111-
112-
return {
113-
"attr": f"{data['attr']}",
114-
"name": data["name"],
115-
"system": data["system"],
116-
"already_cached": data.get("cacheStatus") != "notBuilt",
117-
"runs_on": runs_on_config,
118-
"drvPath": data["drvPath"],
119-
"neededSubstitutes": data.get("neededSubstitutes", []),
120-
"neededBuilds": data.get("neededBuilds", []),
121-
}
103+
return data
122104
except json.JSONDecodeError:
123105
print(f"Skipping invalid JSON line: {line}", file=sys.stderr)
124106
return None
125107

126108

127-
def run_nix_eval_jobs(cmd: List[str]) -> Generator[GitHubActionPackage, None, None]:
109+
def run_nix_eval_jobs(cmd: List[str]) -> Generator[NixEvalJobsOutput, None, None]:
128110
"""Run nix-eval-jobs and yield parsed package data."""
129111
print(f"Running command: {' '.join(cmd)}", file=sys.stderr)
130112

@@ -146,21 +128,21 @@ def run_nix_eval_jobs(cmd: List[str]) -> Generator[GitHubActionPackage, None, No
146128
sys.exit(process.returncode)
147129

148130

149-
def is_extension_pkg(pkg: GitHubActionPackage) -> bool:
131+
def is_extension_pkg(pkg: NixEvalJobsOutput) -> bool:
150132
"""Check if the package is a postgresql extension package."""
151133
attrs = pkg["attr"].split(".")
152134
return attrs[-2] == "exts"
153135

154136

155137
# thank you buildbot-nix https://github.com/nix-community/buildbot-nix/blob/985d069a2a45cf4a571a4346107671adc2bd2a16/buildbot_nix/buildbot_nix/build_trigger.py#L297
156-
def sort_pkgs_by_closures(jobs: List[GitHubActionPackage]) -> List[GitHubActionPackage]:
138+
def sort_pkgs_by_closures(jobs: List[NixEvalJobsOutput]) -> List[NixEvalJobsOutput]:
157139
sorted_jobs = []
158140

159141
# Prepare job dependencies
160142
job_set = {job["drvPath"] for job in jobs}
161143
job_closures = {
162-
k["drvPath"]: set(k["neededSubstitutes"])
163-
.union(set(k["neededBuilds"]))
144+
k["drvPath"]: set(k.get("neededSubstitutes", []))
145+
.union(set(k.get("neededBuilds", [])))
164146
.intersection(job_set)
165147
.difference({k["drvPath"]})
166148
for k in jobs
@@ -196,13 +178,12 @@ def main() -> None:
196178

197179
gh_action_packages = sort_pkgs_by_closures(list(run_nix_eval_jobs(cmd)))
198180

199-
def clean_package_for_output(pkg: GitHubActionPackage) -> dict:
200-
"""Remove debug fields from package for final output"""
201-
returned_pkg = {
181+
def clean_package_for_output(pkg: NixEvalJobsOutput) -> GitHubActionPackage:
182+
"""Convert nix-eval-jobs output to GitHub Actions matrix package"""
183+
returned_pkg: GitHubActionPackage = {
202184
"attr": pkg["attr"],
203185
"name": pkg["name"],
204186
"system": pkg["system"],
205-
"runs_on": pkg["runs_on"],
206187
}
207188
if is_extension_pkg(pkg):
208189
# Extract PostgreSQL version from attribute path
@@ -213,7 +194,7 @@ def clean_package_for_output(pkg: GitHubActionPackage) -> dict:
213194
# Group packages by system
214195
grouped_by_system = defaultdict(list)
215196
for pkg in gh_action_packages:
216-
if not pkg["already_cached"]:
197+
if pkg.get("cacheStatus") == "notBuilt":
217198
grouped_by_system[pkg["system"]].append(clean_package_for_output(pkg))
218199

219200
# Create output with system-specific matrices

0 commit comments

Comments
 (0)