Skip to content

Commit 4b61b5e

Browse files
committed
Simplify _preorder_depth_first_walk
Reduce redundant logging and simplify code further. Signed-off-by: Teodora Sechkova <tsechkova@vmware.com>
1 parent 5a1d42b commit 4b61b5e

File tree

1 file changed

+13
-26
lines changed

1 file changed

+13
-26
lines changed

tuf/ngclient/updater.py

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -366,89 +366,76 @@ def _load_targets(self, role: str, parent_role: str) -> None:
366366
self._trusted_set.update_delegated_targets(data, role, parent_role)
367367
self._persist_metadata(role, data)
368368

369-
def _preorder_depth_first_walk(self, target_filepath) -> Dict:
369+
def _preorder_depth_first_walk(self, target_filepath: str) -> Dict:
370370
"""
371371
Interrogates the tree of target delegations in order of appearance
372372
(which implicitly order trustworthiness), and returns the matching
373373
target found in the most trusted role.
374374
"""
375375

376-
target = None
377376
role_names = [("targets", "root")]
378377
visited_role_names = set()
379378
number_of_delegations = self.config.max_delegations
380379

381380
# Preorder depth-first traversal of the graph of target delegations.
382-
while (
383-
target is None and number_of_delegations > 0 and len(role_names) > 0
384-
):
381+
while number_of_delegations > 0 and len(role_names) > 0:
385382

386383
# Pop the role name from the top of the stack.
387384
role_name, parent_role = role_names.pop(-1)
388-
self._load_targets(role_name, parent_role)
385+
389386
# Skip any visited current role to prevent cycles.
390387
if (role_name, parent_role) in visited_role_names:
391388
logger.debug("Skipping visited current role %s", role_name)
392389
continue
393390

394391
# The metadata for 'role_name' must be downloaded/updated before
395392
# its targets, delegations, and child roles can be inspected.
393+
self._load_targets(role_name, parent_role)
396394

397395
role_metadata = self._trusted_set[role_name].signed
398396
target = role_metadata.targets.get(target_filepath)
399397

398+
if target is not None:
399+
logger.debug("Found target in current role %s", role_name)
400+
return {"filepath": target_filepath, "fileinfo": target}
401+
400402
# After preorder check, add current role to set of visited roles.
401403
visited_role_names.add((role_name, parent_role))
402404

403405
# And also decrement number of visited roles.
404406
number_of_delegations -= 1
405-
child_roles = []
406-
if role_metadata.delegations is not None:
407-
child_roles = role_metadata.delegations.roles
408407

409-
if target is None:
408+
if role_metadata.delegations is not None:
410409
child_roles_to_visit = []
411410
# NOTE: This may be a slow operation if there are many
412411
# delegated roles.
413-
for child_role in child_roles:
412+
for child_role in role_metadata.delegations.roles:
414413
if child_role.is_in_trusted_paths(target_filepath):
415-
416414
logger.debug("Adding child role %s", child_role.name)
417415

418416
child_roles_to_visit.append(
419417
(child_role.name, role_name)
420418
)
421-
422419
if child_role.terminating:
423420
logger.debug("Not backtracking to other roles.")
424421
role_names = []
425422
break
426-
427-
else:
428-
logger.debug("Skipping child role %s", child_role.name)
429-
430423
# Push 'child_roles_to_visit' in reverse order of appearance
431424
# onto 'role_names'. Roles are popped from the end of
432425
# the 'role_names' list.
433426
child_roles_to_visit.reverse()
434427
role_names.extend(child_roles_to_visit)
435428

436-
else:
437-
logger.debug("Found target in current role %s", role_name)
438-
439-
if (
440-
target is None
441-
and number_of_delegations == 0
442-
and len(role_names) > 0
443-
):
429+
if number_of_delegations == 0 and len(role_names) > 0:
444430
logger.debug(
445431
"%d roles left to visit, but allowed to "
446432
"visit at most %d delegations.",
447433
len(role_names),
448434
self.config.max_delegations,
449435
)
450436

451-
return {"filepath": target_filepath, "fileinfo": target}
437+
# If this point is reached then target is not found, return None
438+
return None
452439

453440

454441
def _ensure_trailing_slash(url: str):

0 commit comments

Comments
 (0)