@@ -303,89 +303,76 @@ def _load_targets(self, role: str, parent_role: str) -> None:
303303 self ._trusted_set .update_delegated_targets (data , role , parent_role )
304304 self ._persist_metadata (role , data )
305305
306- def _preorder_depth_first_walk (self , target_filepath ) -> Dict :
306+ def _preorder_depth_first_walk (self , target_filepath : str ) -> Dict :
307307 """
308308 Interrogates the tree of target delegations in order of appearance
309309 (which implicitly order trustworthiness), and returns the matching
310310 target found in the most trusted role.
311311 """
312312
313- target = None
314313 role_names = [("targets" , "root" )]
315314 visited_role_names = set ()
316315 number_of_delegations = self .config .max_delegations
317316
318317 # Preorder depth-first traversal of the graph of target delegations.
319- while (
320- target is None and number_of_delegations > 0 and len (role_names ) > 0
321- ):
318+ while number_of_delegations > 0 and len (role_names ) > 0 :
322319
323320 # Pop the role name from the top of the stack.
324321 role_name , parent_role = role_names .pop (- 1 )
325- self . _load_targets ( role_name , parent_role )
322+
326323 # Skip any visited current role to prevent cycles.
327324 if (role_name , parent_role ) in visited_role_names :
328325 logger .debug ("Skipping visited current role %s" , role_name )
329326 continue
330327
331328 # The metadata for 'role_name' must be downloaded/updated before
332329 # its targets, delegations, and child roles can be inspected.
330+ self ._load_targets (role_name , parent_role )
333331
334332 role_metadata = self ._trusted_set [role_name ].signed
335333 target = role_metadata .targets .get (target_filepath )
336334
335+ if target is not None :
336+ logger .debug ("Found target in current role %s" , role_name )
337+ return {"filepath" : target_filepath , "fileinfo" : target }
338+
337339 # After preorder check, add current role to set of visited roles.
338340 visited_role_names .add ((role_name , parent_role ))
339341
340342 # And also decrement number of visited roles.
341343 number_of_delegations -= 1
342- child_roles = []
343- if role_metadata .delegations is not None :
344- child_roles = role_metadata .delegations .roles
345344
346- if target is None :
345+ if role_metadata . delegations is not None :
347346 child_roles_to_visit = []
348347 # NOTE: This may be a slow operation if there are many
349348 # delegated roles.
350- for child_role in child_roles :
349+ for child_role in role_metadata . delegations . roles :
351350 if child_role .is_in_trusted_paths (target_filepath ):
352-
353351 logger .debug ("Adding child role %s" , child_role .name )
354352
355353 child_roles_to_visit .append (
356354 (child_role .name , role_name )
357355 )
358-
359356 if child_role .terminating :
360357 logger .debug ("Not backtracking to other roles." )
361358 role_names = []
362359 break
363-
364- else :
365- logger .debug ("Skipping child role %s" , child_role .name )
366-
367360 # Push 'child_roles_to_visit' in reverse order of appearance
368361 # onto 'role_names'. Roles are popped from the end of
369362 # the 'role_names' list.
370363 child_roles_to_visit .reverse ()
371364 role_names .extend (child_roles_to_visit )
372365
373- else :
374- logger .debug ("Found target in current role %s" , role_name )
375-
376- if (
377- target is None
378- and number_of_delegations == 0
379- and len (role_names ) > 0
380- ):
366+ if number_of_delegations == 0 and len (role_names ) > 0 :
381367 logger .debug (
382368 "%d roles left to visit, but allowed to "
383369 "visit at most %d delegations." ,
384370 len (role_names ),
385371 self .config .max_delegations ,
386372 )
387373
388- return {"filepath" : target_filepath , "fileinfo" : target }
374+ # If this point is reached then target is not found, return None
375+ return None
389376
390377
391378def _ensure_trailing_slash (url : str ):
0 commit comments