Skip to content

Commit 46f0c0d

Browse files
authored
Merge pull request #244 from desultory/dev
fix import order not accounting for changing index, improve comments, fix logging typo
2 parents e301485 + b671b0b commit 46f0c0d

File tree

2 files changed

+9
-8
lines changed

2 files changed

+9
-8
lines changed

src/ugrd/initramfs_generator.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -159,14 +159,14 @@ def sort_hook_functions(self, hook: str) -> None:
159159
return self.logger.debug("No import order specified for hook: %s" % hook)
160160

161161
def iter_order(order, direction):
162-
# Iterate over all before/after functions
163-
# If the function is not in the correct position, move it
164-
# Use the index of the function to determine the position
165-
# Move the function in the imports list as well
162+
""" Iterate over all functions in an import order list,
163+
using this information to move the order of function names in the import list.
164+
165+
Returns True if any changes were made, False otherwise."""
166166
changed = False
167167

168168
for func_name, other_funcs in order.items():
169-
func_index = func_names.index(func_name)
169+
func_index = func_names.index(func_name) # Get the index based on the current position
170170
assert func_index >= 0, "Function not found in import list: %s" % func_name
171171
for other_func in other_funcs:
172172
try:
@@ -177,7 +177,6 @@ def iter_order(order, direction):
177177

178178
def reorder_func(direction):
179179
"""Reorders the function based on the direction."""
180-
self.logger.debug("Moving %s %s %s" % (func_name, direction, other_func))
181180
if direction == "before": # Move the function before the other function
182181
self.logger.debug("[%s] Moving function before: %s" % (func_name, other_func))
183182
func_names.insert(other_index, func_names.pop(func_index))
@@ -204,18 +203,20 @@ def reorder_func(direction):
204203
self.logger.log(5, "Function %s already after: %s" % (func_name, other_func))
205204
else:
206205
raise ValueError("Invalid direction: %s" % direction)
206+
func_index = func_names.index(func_name) # Update the index after moving
207207
return changed
208208

209209
max_iterations = len(func_names) * (len(before) + 1) * (len(after) + 1) # Prevent infinite loops
210210
iterations = max_iterations
211211
while iterations:
212212
iterations -= 1
213+
# Update the order based on the before and after lists
213214
if not any([iter_order(before, "before"), iter_order(after, "after")]):
214215
self.logger.debug(
215216
"[%s] Import order converged after %s iterations" % (hook, max_iterations - iterations)
216217
)
217218
break # Keep going until no changes are made
218-
else:
219+
else: # If the loop completes without breaking, raise an error
219220
self.logger.error("Import list: %s" % func_names)
220221
self.logger.error("Before: %s" % before)
221222
self.logger.error("After: %s" % after)

src/ugrd/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ def main():
183183
logger.critical(e)
184184
exit(1)
185185
except Exception as e:
186-
logger.criical("An unhandled exception occurred while building:")
186+
logger.critical("An unhandled exception occurred while building:")
187187
print(generator.config_dict)
188188
logger.critical(e, exc_info=True)
189189
exit(1)

0 commit comments

Comments
 (0)