1
1
import json
2
- import math
3
- import os
2
+ import pathlib
4
3
import re
5
4
import string
6
5
from typing import Iterable , cast
@@ -60,31 +59,6 @@ def extract_target_dependencies(lines: Iterable[str]) -> dict[str, list[str]]:
60
59
return tree
61
60
62
61
63
- def compute_target_distances_from_root (tree : dict [str , list [str ]]) -> dict [str , int ]:
64
- levels = {key : math .inf for key in tree }
65
-
66
- # roots have distance of 0
67
- roots = [key for key , value in tree .items () if not value ]
68
- for root in roots :
69
- levels [root ] = 0
70
-
71
- # iterate through undetermined targets and try to compute their distance
72
- # this is an inefficient quadratic loop, but it does not matter here
73
- while math .inf in levels .values ():
74
- changed = False
75
- for key , value in levels .items ():
76
- if value == math .inf :
77
- dist = max (levels [dep ] for dep in tree [key ])
78
- if dist < math .inf :
79
- levels [key ] = dist + 1
80
- changed = True
81
- if not changed :
82
- raise Exception ("Iteration did not make any progress" )
83
-
84
- # math.inf is a float; at this point we know all inf values were replaced by integral distances
85
- return cast (dict [str , int ], levels )
86
-
87
-
88
62
def print_github_actions_matrix (levels : dict [str , int ]) -> list [str ]:
89
63
"""Outputs GitHub matrix definition Json as per
90
64
"""
@@ -103,7 +77,7 @@ def print_github_actions_matrix(levels: dict[str, int]) -> list[str]:
103
77
lines .append (f"level{ level } ={ json .dumps (matrix , separators = ("," , ":" ))} " )
104
78
return lines
105
79
106
- def write_github_workflow_file (tree : dict [str , list [str ]]) -> None :
80
+ def write_github_workflow_file (tree : dict [str , list [str ]], path : pathlib . Path ) -> None :
107
81
jobs = {}
108
82
109
83
# IDs may only contain alphanumeric characters, '_', and '-'. IDs must start with a letter or '_' and must be less than 100 characters.
@@ -143,26 +117,21 @@ def write_github_workflow_file(tree: dict[str, list[str]]) -> None:
143
117
"jobs" : jobs ,
144
118
}
145
119
146
- with open (".github/workflows/build-notebooks.yaml" , "wt" ) as f :
120
+ with open (path , "wt" ) as f :
121
+ print ("# This file is autogenerated by" , __file__ , "DO NOT EDIT BY HAND" , file = f )
147
122
# every json file is a valid yaml file
148
123
json .dump (workflow , f , sort_keys = False , indent = 4 )
149
124
150
125
151
126
def main () -> None :
127
+ cwd = pathlib .Path (__file__ ).parent .absolute ()
128
+
152
129
# https://www.gnu.org/software/make/manual/make.html#Reading-Makefiles
153
130
with open ("Makefile" , "rt" ) as makefile :
154
131
lines = read_makefile_lines (makefile )
155
132
tree = extract_target_dependencies (lines )
156
133
157
- levels = compute_target_distances_from_root (tree )
158
- output = print_github_actions_matrix (levels )
159
-
160
- write_github_workflow_file (tree )
161
-
162
- print (* output , sep = "\n " )
163
- with open (os .environ ["GITHUB_OUTPUT" ], "at" ) as f :
164
- for line in output :
165
- print (line , file = f )
134
+ write_github_workflow_file (tree , cwd / ".github" / "workflows" / "build-notebooks.yaml" )
166
135
167
136
168
137
if __name__ == '__main__' :
0 commit comments