Skip to content

Commit f2b0e6d

Browse files
committed
[pipeline/kexts] Simplify kext sorting logic
Prevents orphaning of dependents with explicit bounds checks and wider indexing to prevent collisions on insert/upsert.
1 parent f48496a commit f2b0e6d

File tree

1 file changed

+35
-15
lines changed

1 file changed

+35
-15
lines changed

ocebuild/pipeline/kexts.py

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -177,28 +177,48 @@ def num_dependents(kext: dict) -> int:
177177
for k in kext.get('dependencies', {}))
178178
is_bundled_kext = kext['__path'].count('.kext') > 1
179179
is_standalone_kext = not num_dependents(kext) and not is_bundled_kext
180+
181+
node_added = False
180182
# Has no dependencies on any nodes' dependents
181183
if is_standalone_kext and not has_resolved_dependencies:
182184
nodes.append([kext])
183185
offset = idx + 1
186+
node_added = True
184187
# Has dependents in the current node
185-
elif num_dependents(kext) and not (is_bundled_kext, offset == idx):
186-
offset = idx + 1
187-
nodes.append(sorted_dependencies[offset:idx])
188-
# Is bundled or has dependencies in the current node
189-
elif not (has_resolved_dependencies or is_bundled_kext):
190-
nodes.append(sorted_dependencies[offset:idx])
188+
elif num_dependents(kext) and not (is_bundled_kext or offset == idx):
189+
# Append previous node group if it exists
190+
prev_node = sorted_dependencies[offset:idx]
191+
if prev_node:
192+
nodes.append(prev_node)
193+
node_added = True # Mark that a node was potentially added
194+
# Start new offset for the current kext (which might start a new node later)
191195
offset = idx
192-
# Ensures the last node is added
196+
# Note: Current kext is not added here, handled in subsequent iterations or final step
197+
# Is bundled or has dependencies in the current node (group with previous)
198+
elif not (has_resolved_dependencies or is_bundled_kext):
199+
# Append node including current kext, update offset for next group
200+
nodes.append(sorted_dependencies[offset : idx + 1])
201+
offset = idx + 1
202+
node_added = True
203+
# Ensures the last node is added (includes all remaining kexts)
193204
elif idx == len(sorted_dependencies) - 1:
194-
nodes.append(sorted_dependencies[offset:idx])
195-
else: continue
196-
197-
# Sort the inserted node's standalone dependents alphabetically
198-
ordered, unordered = [], []
199-
for k in nodes[-1]:
200-
(ordered if num_dependents(k) else unordered).append(k)
201-
nodes[-1] = ordered + sorted(unordered, key=lambda k: k['name'])
205+
# Append the final node including the last kext
206+
final_node = sorted_dependencies[offset : idx + 1]
207+
if final_node:
208+
nodes.append(final_node)
209+
node_added = True
210+
else:
211+
# Otherwise, continue processing without creating a new node yet
212+
continue
213+
214+
# Sort the inserted node's standalone dependents alphabetically, only if a node was added
215+
if node_added and nodes and nodes[-1]: # Check if nodes is not empty and last node is not empty
216+
last_node = nodes[-1]
217+
ordered, unordered = [], []
218+
for k in last_node:
219+
(ordered if num_dependents(k) else unordered).append(k)
220+
# Only replace if the sorting actually changes something or if it's necessary
221+
nodes[-1] = ordered + sorted(unordered, key=lambda k: k['name'])
202222

203223
# Handle colliding cfbundleidentifiers (or unmapped kexts)
204224
for kext in sorted_dependencies:

0 commit comments

Comments
 (0)