Skip to content

Commit da09a22

Browse files
committed
Improve get_delegations_filenames performance and readability
Remove unnecessary list keeping track of loaded file names and rewrite outdated comments. Signed-off-by: Teodora Sechkova <[email protected]>
1 parent 73bff87 commit da09a22

File tree

1 file changed

+9
-12
lines changed

1 file changed

+9
-12
lines changed

tuf/repository_lib.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -822,9 +822,13 @@ def get_delegations_filenames(metadata_directory, consistent_snapshot,
822822
"""
823823

824824
filenames = {}
825-
loaded_metadata = []
826825
metadata_files = sorted(storage_backend.list_folder(metadata_directory),
827826
reverse=True)
827+
828+
# Iterate over role metadata files, sorted by their version-number prefix, with
829+
# more recent versions first, and only add the most recent version of any
830+
# (non top-level) metadata to the list of returned filenames. Note that there
831+
# should only be one version of each file, if consistent_snapshot is False.
828832
for metadata_role in metadata_files:
829833
metadata_path = os.path.join(metadata_directory, metadata_role)
830834

@@ -845,20 +849,13 @@ def get_delegations_filenames(metadata_directory, consistent_snapshot,
845849
' extension: ' + repr(metadata_path))
846850
continue
847851

848-
# Skip top-level roles, only interested in delegated roles now that the
849-
# top-level roles have already been loaded.
852+
# Skip top-level roles, only interested in delegated roles.
850853
if metadata_name in ['root', 'snapshot', 'targets', 'timestamp']:
851854
continue
852855

853-
# Keep a store of metadata previously loaded metadata to prevent re-loading
854-
# duplicate versions. Duplicate versions may occur with
855-
# 'consistent_snapshot', where the same metadata may be available in
856-
# multiples files (the different hash is included in each filename).
857-
if metadata_name in loaded_metadata:
858-
continue
859-
860-
filenames[metadata_name] = metadata_path
861-
loaded_metadata.append(metadata_name)
856+
# Prevent reloading duplicate versions if consistent_snapshot is True
857+
if metadata_name not in filenames:
858+
filenames[metadata_name] = metadata_path
862859

863860
return filenames
864861

0 commit comments

Comments
 (0)