Skip to content

Commit 936de83

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 e58d42b commit 936de83

File tree

1 file changed

+19
-36
lines changed

1 file changed

+19
-36
lines changed

tuf/ngclient/updater.py

Lines changed: 19 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -312,92 +312,75 @@ def _load_targets(self, role: str, parent_role: str) -> None:
312312
self._trusted_set.update_delegated_targets(data, role, parent_role)
313313
self._persist_metadata(role, data)
314314

315-
def _preorder_depth_first_walk(self, target_filepath) -> Dict:
315+
def _preorder_depth_first_walk(self, target_filepath: str) -> Dict:
316316
"""
317317
Interrogates the tree of target delegations in order of appearance
318318
(which implicitly order trustworthiness), and returns the matching
319319
target found in the most trusted role.
320320
"""
321321

322-
target = None
323322
role_names = [("targets", "root")]
324323
visited_role_names = set()
325324
number_of_delegations = self.config.max_delegations
326325

327326
# Preorder depth-first traversal of the graph of target delegations.
328-
while (
329-
target is None and number_of_delegations > 0 and len(role_names) > 0
330-
):
327+
while number_of_delegations > 0 and len(role_names) > 0:
331328

332329
# Pop the role name from the top of the stack.
333330
role_name, parent_role = role_names.pop(-1)
334-
self._load_targets(role_name, parent_role)
331+
335332
# Skip any visited current role to prevent cycles.
336333
if (role_name, parent_role) in visited_role_names:
337-
msg = f"Skipping visited current role {role_name}"
338-
logger.debug(msg)
334+
logger.debug("Skipping visited current role %s", role_name)
339335
continue
340336

341337
# The metadata for 'role_name' must be downloaded/updated before
342338
# its targets, delegations, and child roles can be inspected.
339+
self._load_targets(role_name, parent_role)
343340

344341
role_metadata = self._trusted_set[role_name].signed
345342
target = role_metadata.targets.get(target_filepath)
346343

344+
if target is not None:
345+
logger.debug("Found target in current role %s", role_name)
346+
return {"filepath": target_filepath, "fileinfo": target}
347+
347348
# After preorder check, add current role to set of visited roles.
348349
visited_role_names.add((role_name, parent_role))
349350

350351
# And also decrement number of visited roles.
351352
number_of_delegations -= 1
352-
child_roles = []
353-
if role_metadata.delegations is not None:
354-
child_roles = role_metadata.delegations.roles
355353

356-
if target is None:
354+
if role_metadata.delegations is not None:
357355
child_roles_to_visit = []
358356
# NOTE: This may be a slow operation if there are many
359357
# delegated roles.
360-
for child_role in child_roles:
358+
for child_role in role_metadata.delegations.roles:
361359
if child_role.is_in_trusted_paths(target_filepath):
362-
363-
msg = f"Adding child role {child_role.name}"
364-
logger.debug(msg)
360+
logger.debug("Adding child role %s", child_role.name)
365361

366362
child_roles_to_visit.append(
367363
(child_role.name, role_name)
368364
)
369-
370365
if child_role.terminating:
371366
logger.debug("Not backtracking to other roles.")
372367
role_names = []
373368
break
374-
375-
else:
376-
msg = f"Skipping child role {child_role.name}"
377-
logger.debug(msg)
378-
379369
# Push 'child_roles_to_visit' in reverse order of appearance
380370
# onto 'role_names'. Roles are popped from the end of
381371
# the 'role_names' list.
382372
child_roles_to_visit.reverse()
383373
role_names.extend(child_roles_to_visit)
384374

385-
else:
386-
msg = f"Found target in current role {role_name}"
387-
logger.debug(msg)
388-
389-
if (
390-
target is None
391-
and number_of_delegations == 0
392-
and len(role_names) > 0
393-
):
394-
msg = (
395-
f"{len(role_names)} roles left to visit, but allowed to ",
396-
f"visit at most {self.config.max_delegations} delegations.",
375+
if number_of_delegations == 0 and len(role_names) > 0:
376+
logger.debug(
377+
"%d roles left to visit, but allowed to visit at most %d.",
378+
len(role_names),
379+
self.config.max_delegations,
397380
)
398-
logger.debug(msg)
399381

400-
return {"filepath": target_filepath, "fileinfo": target}
382+
# If this point is reached then target is not found, return None
383+
return None
401384

402385

403386
def _ensure_trailing_slash(url: str):

0 commit comments

Comments
 (0)