Skip to content

Commit c876dd3

Browse files
author
Tjaz Erzen
committed
Refactor: _ensure_previous_frid_commits_exist
1 parent a614131 commit c876dd3

3 files changed

Lines changed: 103 additions & 50 deletions

File tree

git_utils.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -259,9 +259,7 @@ def _get_commit(repo: Repo, frid: Optional[str]) -> str:
259259
return initial_commit
260260

261261

262-
def _get_commit_with_frid(
263-
repo: Repo, frid: str, module_name: Optional[str] = None
264-
) -> str:
262+
def _get_commit_with_frid(repo: Repo, frid: str, module_name: Optional[str] = None) -> str:
265263
"""
266264
Finds commit with given frid mentioned in the commit message.
267265
@@ -281,9 +279,7 @@ def _get_commit_with_frid(
281279
return _get_commit_with_message(repo, commit_message_pattern)
282280

283281
# Use multiple grep patterns with --all-match for AND condition
284-
escaped_frid_message = commit_message_pattern.replace("[", "\\[").replace(
285-
"]", "\\]"
286-
)
282+
escaped_frid_message = commit_message_pattern.replace("[", "\\[").replace("]", "\\]")
287283
module_name_pattern = MODULE_NAME_MESSAGE.format(module_name)
288284
escaped_module_message = module_name_pattern.replace("[", "\\[").replace("]", "\\]")
289285

module_renderer.py

Lines changed: 81 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -42,40 +42,22 @@ def __init__(
4242
self.run_state = run_state
4343
self.event_bus = event_bus
4444

45-
def _validate_previous_frid_commits_exist(
46-
self, module_name: str, plain_source: dict, render_range: list[str]
47-
) -> None:
45+
def _ensure_module_folders_exist(self, module_name: str, first_render_frid: str) -> tuple[str, str]:
4846
"""
49-
Validate that all FRID commits before the render_range exist.
47+
Ensure that build and conformance test folders exist for the module.
5048
5149
Args:
5250
module_name: Name of the module being rendered
53-
plain_source: The plain source tree
54-
render_range: List of FRIDs to render
51+
first_render_frid: The first FRID in the render range
52+
53+
Returns:
54+
tuple[str, str]: (build_folder_path, conformance_tests_path)
5555
5656
Raises:
57-
MissingPreviousFridCommitsError: If any previous FRID commits are missing
57+
MissingPreviousFridCommitsError: If any required folders are missing
5858
"""
59-
# Get all FRIDs from the plain source
60-
all_frids = list(plain_spec.get_frids(plain_source))
61-
first_render_frid = render_range[0]
62-
63-
# Find all FRIDs before the first FRID in render_range
64-
previous_frids = []
65-
for frid in all_frids:
66-
if frid == first_render_frid:
67-
break
68-
previous_frids.append(frid)
69-
70-
# If there are no previous FRIDs, nothing to validate
71-
if not previous_frids:
72-
return
73-
74-
# Check if commits exist for all previous FRIDs
7559
build_folder_path = os.path.join(self.args.build_folder, module_name)
76-
conformance_tests_path = os.path.join(
77-
self.args.conformance_tests_folder, module_name
78-
)
60+
conformance_tests_path = os.path.join(self.args.conformance_tests_folder, module_name)
7961

8062
if not os.path.exists(build_folder_path):
8163
raise MissingPreviousFridCommitsError(
@@ -91,27 +73,82 @@ def _validate_previous_frid_commits_exist(
9173
f"Please render all previous FRIDs for module {module_name} first."
9274
)
9375

94-
for prev_frid in previous_frids:
95-
# Check in build folder
96-
if not git_utils.has_commit_for_frid(
97-
build_folder_path, prev_frid, module_name
98-
):
76+
return build_folder_path, conformance_tests_path
77+
78+
def _ensure_frid_commit_exists(
79+
self,
80+
frid: str,
81+
module_name: str,
82+
build_folder_path: str,
83+
conformance_tests_path: str,
84+
first_render_frid: str,
85+
) -> None:
86+
"""
87+
Ensure commit exists for a single FRID in both repositories.
88+
89+
Args:
90+
frid: The FRID to check
91+
module_name: Name of the module
92+
build_folder_path: Path to the build folder
93+
conformance_tests_path: Path to the conformance tests folder
94+
first_render_frid: The first FRID in the render range (for error messages)
95+
96+
Raises:
97+
MissingPreviousFridCommitsError: If the commit is missing
98+
"""
99+
# Check in build folder
100+
if not git_utils.has_commit_for_frid(build_folder_path, frid, module_name):
101+
raise MissingPreviousFridCommitsError(
102+
f"Cannot render from FRID {first_render_frid}: "
103+
f"Missing commit for previous FRID {frid} in {build_folder_path}. "
104+
f"Please render all previous FRIDs first."
105+
)
106+
107+
# Check in conformance tests folder (only if conformance tests are enabled)
108+
if self.args.render_conformance_tests:
109+
if not git_utils.has_commit_for_frid(conformance_tests_path, frid, module_name):
99110
raise MissingPreviousFridCommitsError(
100111
f"Cannot render from FRID {first_render_frid}: "
101-
f"Missing commit for previous FRID {prev_frid} in {build_folder_path}. "
112+
f"Missing commit for previous FRID {frid} in {conformance_tests_path}. "
102113
f"Please render all previous FRIDs first."
103114
)
104115

105-
# Check in conformance tests folder (only if conformance tests are enabled)
106-
if self.args.render_conformance_tests:
107-
if not git_utils.has_commit_for_frid(
108-
conformance_tests_path, prev_frid, module_name
109-
):
110-
raise MissingPreviousFridCommitsError(
111-
f"Cannot render from FRID {first_render_frid}: "
112-
f"Missing commit for previous FRID {prev_frid} in {conformance_tests_path}. "
113-
f"Please render all previous FRIDs first."
114-
)
116+
def _ensure_previous_frid_commits_exist(
117+
self, module_name: str, plain_source: dict, render_range: list[str]
118+
) -> None:
119+
"""
120+
Ensure that all FRID commits before the render_range exist.
121+
122+
This is a precondition check that must pass before rendering can proceed.
123+
Raises an exception if any previous FRID commits are missing.
124+
125+
Args:
126+
module_name: Name of the module being rendered
127+
plain_source: The plain source tree
128+
render_range: List of FRIDs to render
129+
130+
Raises:
131+
MissingPreviousFridCommitsError: If any previous FRID commits are missing
132+
"""
133+
first_render_frid = render_range[0]
134+
135+
# Get all FRIDs that should have been rendered before this one
136+
previous_frids = plain_spec.get_frids_before(plain_source, first_render_frid)
137+
if not previous_frids:
138+
return
139+
140+
# Ensure the module folders exist
141+
build_folder_path, conformance_tests_path = self._ensure_module_folders_exist(module_name, first_render_frid)
142+
143+
# Verify commits exist for all previous FRIDs
144+
for prev_frid in previous_frids:
145+
self._ensure_frid_commit_exists(
146+
prev_frid,
147+
module_name,
148+
build_folder_path,
149+
conformance_tests_path,
150+
first_render_frid,
151+
)
115152

116153
def _build_render_context_for_module(
117154
self,
@@ -159,9 +196,9 @@ def _render_module(
159196
resources_list = []
160197
plain_spec.collect_linked_resources(plain_source, resources_list, None, True)
161198

162-
# Validate that all previous FRID commits exist before proceeding with render_range
199+
# Ensure that all previous FRID commits exist before proceeding with render_range
163200
if render_range is not None:
164-
self._validate_previous_frid_commits_exist(module_name, plain_source, render_range)
201+
self._ensure_previous_frid_commits_exist(module_name, plain_source, render_range)
165202

166203
required_modules = []
167204
has_any_required_module_changed = False

plain_spec.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,26 @@ def get_previous_frid(plain_source_tree, frid):
178178
raise Exception(f"Functional requirement {frid} does not exist.")
179179

180180

181+
def get_frids_before(plain_source_tree, target_frid: str) -> list[str]:
182+
"""
183+
Get all FRIDs that appear before the target FRID in the specification.
184+
185+
Args:
186+
plain_source_tree: The plain source tree
187+
target_frid: The FRID to find predecessors for
188+
189+
Returns:
190+
List of FRIDs that appear before target_frid, in order
191+
"""
192+
previous_frids = []
193+
for frid in get_frids(plain_source_tree):
194+
if frid == target_frid:
195+
break
196+
previous_frids.append(frid)
197+
198+
return previous_frids
199+
200+
181201
def get_specification_item_markdown(specification_item, code_variables, replace_code_variables):
182202
markdown = specification_item["markdown"]
183203
if "code_variables" in specification_item:

0 commit comments

Comments
 (0)