Skip to content

Commit

Permalink
Improve get_delegations_filenames performance and readability
Browse files Browse the repository at this point in the history
Remove unnecessary list keeping track of loaded file names and
rewrite outdated comments.

Signed-off-by: Teodora Sechkova <tsechkova@vmware.com>
  • Loading branch information
sechkova committed Jun 24, 2020
1 parent 73bff87 commit da09a22
Showing 1 changed file with 9 additions and 12 deletions.
21 changes: 9 additions & 12 deletions tuf/repository_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -822,9 +822,13 @@ def get_delegations_filenames(metadata_directory, consistent_snapshot,
"""

filenames = {}
loaded_metadata = []
metadata_files = sorted(storage_backend.list_folder(metadata_directory),
reverse=True)

# Iterate over role metadata files, sorted by their version-number prefix, with
# more recent versions first, and only add the most recent version of any
# (non top-level) metadata to the list of returned filenames. Note that there
# should only be one version of each file, if consistent_snapshot is False.
for metadata_role in metadata_files:
metadata_path = os.path.join(metadata_directory, metadata_role)

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

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

# Keep a store of metadata previously loaded metadata to prevent re-loading
# duplicate versions. Duplicate versions may occur with
# 'consistent_snapshot', where the same metadata may be available in
# multiples files (the different hash is included in each filename).
if metadata_name in loaded_metadata:
continue

filenames[metadata_name] = metadata_path
loaded_metadata.append(metadata_name)
# Prevent reloading duplicate versions if consistent_snapshot is True
if metadata_name not in filenames:
filenames[metadata_name] = metadata_path

return filenames

Expand Down

0 comments on commit da09a22

Please sign in to comment.