Skip to content

Commit b72151b

Browse files
write build results to files instead of printing
1 parent ff6f35e commit b72151b

2 files changed

Lines changed: 114 additions & 4 deletions

File tree

orion/graph_pipeline.py

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,6 @@ def build_graph(self, graph_spec: GraphSpec):
108108
return False
109109

110110
if build_status == Metadata.STABLE:
111-
self.build_results[graph_id] = {'release_version': release_version}
112111
logger.info(f'Graph {graph_id} release_version {release_version} was already built.')
113112
else:
114113
# If we get here we need to build the graph
@@ -147,7 +146,6 @@ def build_graph(self, graph_spec: GraphSpec):
147146
graph_metadata.set_build_info(merge_metadata, current_time)
148147
graph_metadata.set_build_status(Metadata.STABLE)
149148
logger.info(f'Building graph {graph_id} complete!')
150-
self.build_results[graph_id] = {'release_version': release_version}
151149

152150
kgx_bundle = KGXBundle(graph_output_dir)
153151

@@ -304,6 +302,7 @@ def build_graph(self, graph_spec: GraphSpec):
304302
kgx_bundle.edges_path.replace(KGXBundle.EDGES_FILENAME, COLLAPSED_QUALIFIERS_FILENAME))
305303
for jsonl_path in jsonl_files_to_compress:
306304
kgx_bundle.compress_jsonl(jsonl_path)
305+
self._record_build_result(graph_spec, graph_metadata, release_version, graph_output_dir)
307306
return True
308307

309308
# Determine the release_version (semver) for a graph, deriving its deterministic build_version
@@ -943,6 +942,34 @@ def get_graph_metadata(self, graph_id: str, release_version: str):
943942
# load existing or create new metadata file
944943
return GraphMetadata(graph_id, graph_output_dir)
945944

945+
def _record_build_result(self,
946+
graph_spec: GraphSpec,
947+
graph_metadata: GraphMetadata,
948+
release_version: str,
949+
graph_output_dir: str):
950+
self.build_results[graph_spec.graph_id] = {
951+
'graph_id': graph_spec.graph_id,
952+
'release_version': release_version,
953+
'build_version': graph_spec.build_version,
954+
'graph_dir': graph_output_dir,
955+
'build_status': graph_metadata.get_build_status(),
956+
'build_time': graph_metadata.get_build_time(),
957+
}
958+
959+
# Write the build results from this invocation to graphs_dir/.build_results/<timestamp>.json.
960+
# Subsequent deployment stages read the most-recent file to discover what just built.
961+
# Returns the file path written, or None if nothing was built.
962+
def write_build_results(self) -> str | None:
963+
if not self.build_results:
964+
return None
965+
results_dir = os.path.join(self.graphs_dir, '.build_results')
966+
os.makedirs(results_dir, exist_ok=True)
967+
timestamp = datetime.datetime.now().strftime('%Y-%m-%dT%H%M%S')
968+
results_path = os.path.join(results_dir, f'{timestamp}.json')
969+
with open(results_path, 'w') as f:
970+
json.dump(list(self.build_results.values()), f, indent=2)
971+
return results_path
972+
946973

947974
def _generate_inline_graph_spec(graph_id: str, sources_arg: str, output_format: str) -> dict:
948975
source_ids = [s.strip() for s in sources_arg.split(',') if s.strip()]
@@ -997,8 +1024,11 @@ def main():
9971024
graph_builder.build_graph(graph_spec)
9981025
else:
9991026
print(f'Invalid graph spec requested: {graph_id_arg}')
1000-
for results_graph_id, results in graph_builder.build_results.items():
1001-
print(f'{results_graph_id}\t{results["version"]}')
1027+
results_path = graph_builder.write_build_results()
1028+
if results_path:
1029+
print(f'Build results written to {results_path}')
1030+
else:
1031+
print('No graphs were built.')
10021032

10031033

10041034
if __name__ == '__main__':

tests/test_build_results.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import json
2+
import os
3+
4+
import pytest
5+
6+
from orion.graph_pipeline import GraphBuilder
7+
8+
9+
@pytest.fixture
10+
def test_graph_spec_dir():
11+
return os.path.join(os.path.dirname(os.path.abspath(__file__)), 'graph_specs')
12+
13+
14+
def _make_builder(test_graph_spec_dir, tmp_path):
15+
return GraphBuilder(graph_specs_dir=test_graph_spec_dir,
16+
graph_output_dir=str(tmp_path))
17+
18+
19+
def test_write_build_results_no_builds_returns_none(test_graph_spec_dir, tmp_path):
20+
builder = _make_builder(test_graph_spec_dir, tmp_path)
21+
assert builder.write_build_results() is None
22+
assert not (tmp_path / '.build_results').exists()
23+
24+
25+
def test_write_build_results_writes_records(test_graph_spec_dir, tmp_path):
26+
builder = _make_builder(test_graph_spec_dir, tmp_path)
27+
builder.build_results['Graph_A'] = {
28+
'graph_id': 'Graph_A',
29+
'release_version': '1.0.0',
30+
'build_version': 'abcdef',
31+
'graph_dir': str(tmp_path / 'Graph_A' / '1.0.0'),
32+
'build_status': 'stable',
33+
'build_time': '2026-05-18T14:30:22',
34+
}
35+
builder.build_results['Graph_B'] = {
36+
'graph_id': 'Graph_B',
37+
'release_version': '2.1.0',
38+
'build_version': '123456',
39+
'graph_dir': str(tmp_path / 'Graph_B' / '2.1.0'),
40+
'build_status': 'stable',
41+
'build_time': '2026-05-18T14:31:05',
42+
}
43+
44+
results_path = builder.write_build_results()
45+
46+
assert results_path is not None
47+
assert os.path.isfile(results_path)
48+
assert os.path.dirname(results_path) == str(tmp_path / '.build_results')
49+
assert results_path.endswith('.json')
50+
51+
with open(results_path) as f:
52+
records = json.load(f)
53+
54+
assert isinstance(records, list)
55+
assert len(records) == 2
56+
records_by_id = {r['graph_id']: r for r in records}
57+
assert records_by_id['Graph_A']['release_version'] == '1.0.0'
58+
assert records_by_id['Graph_A']['build_version'] == 'abcdef'
59+
assert records_by_id['Graph_B']['build_status'] == 'stable'
60+
assert records_by_id['Graph_B']['graph_dir'].endswith('Graph_B/2.1.0')
61+
for record in records:
62+
assert set(record.keys()) == {
63+
'graph_id', 'release_version', 'build_version',
64+
'graph_dir', 'build_status', 'build_time',
65+
}
66+
67+
68+
def test_write_build_results_creates_results_dir(test_graph_spec_dir, tmp_path):
69+
builder = _make_builder(test_graph_spec_dir, tmp_path)
70+
builder.build_results['Graph_A'] = {
71+
'graph_id': 'Graph_A',
72+
'release_version': '1.0.0',
73+
'build_version': 'abcdef',
74+
'graph_dir': str(tmp_path / 'Graph_A' / '1.0.0'),
75+
'build_status': 'stable',
76+
'build_time': '2026-05-18T14:30:22',
77+
}
78+
assert not (tmp_path / '.build_results').exists()
79+
builder.write_build_results()
80+
assert (tmp_path / '.build_results').is_dir()

0 commit comments

Comments
 (0)