@@ -1513,6 +1513,28 @@ def _generate_targets_fileinfo(target_files, targets_directory,
15131513
15141514
15151515
1516+ def _get_hashes_and_length_if_needed (use_length , use_hashes , full_file_path ,
1517+ storage_backend ):
1518+ """
1519+ Calculate length and hashes only if they are required,
1520+ otherwise, for adopters of tuf with lots of delegations,
1521+ this will cause unnecessary overhead.
1522+ """
1523+
1524+ length = None
1525+ hashes = None
1526+ if use_length :
1527+ length = securesystemslib .util .get_file_length (full_file_path ,
1528+ storage_backend )
1529+
1530+ if use_hashes :
1531+ hashes = securesystemslib .util .get_file_hashes (full_file_path ,
1532+ tuf .settings .FILE_HASH_ALGORITHMS , storage_backend )
1533+
1534+ return length , hashes
1535+
1536+
1537+
15161538def generate_snapshot_metadata (metadata_directory , version , expiration_date ,
15171539 storage_backend , consistent_snapshot = False ,
15181540 repository_name = 'default' , use_length = False , use_hashes = False ):
@@ -1596,12 +1618,8 @@ def generate_snapshot_metadata(metadata_directory, version, expiration_date,
15961618 # Targets, and all delegated roles of the repository.
15971619 fileinfodict = {}
15981620
1599- length , hashes = securesystemslib .util .get_file_details (
1600- os .path .join (metadata_directory , TARGETS_FILENAME ),
1601- tuf .settings .FILE_HASH_ALGORITHMS , storage_backend )
1602-
1603- length = (use_length and length ) or None
1604- hashes = (use_hashes and hashes ) or None
1621+ length , hashes = _get_hashes_and_length_if_needed (use_length , use_hashes ,
1622+ os .path .join (metadata_directory , TARGETS_FILENAME ), storage_backend )
16051623
16061624 targets_role = TARGETS_FILENAME [:- len (METADATA_EXTENSION )]
16071625
@@ -1637,18 +1655,8 @@ def generate_snapshot_metadata(metadata_directory, version, expiration_date,
16371655 if tuf .roledb .role_exists (rolename , repository_name ) and \
16381656 rolename not in tuf .roledb .TOP_LEVEL_ROLES :
16391657
1640- length = None
1641- hashes = None
1642- # We want to make sure we are calculating length and hashes only when
1643- # at least one of them is needed. Otherwise, for adoptors of tuf with
1644- # lots of delegations, this will cause unnecessary overhead.
1645- if use_length or use_hashes :
1646- length , hashes = securesystemslib .util .get_file_details (
1647- os .path .join (metadata_directory , metadata_filename ),
1648- tuf .settings .FILE_HASH_ALGORITHMS )
1649-
1650- length = (use_length and length ) or None
1651- hashes = (use_hashes and hashes ) or None
1658+ length , hashes = _get_hashes_and_length_if_needed (use_length , use_hashes ,
1659+ os .path .join (metadata_directory , metadata_filename ), storage_backend )
16521660
16531661 file_version = get_metadata_versioninfo (rolename ,
16541662 repository_name )
@@ -1744,11 +1752,8 @@ def generate_timestamp_metadata(snapshot_file_path, version, expiration_date,
17441752
17451753 snapshot_fileinfo = {}
17461754
1747- length , hashes = securesystemslib .util .get_file_details (snapshot_file_path ,
1748- tuf .settings .FILE_HASH_ALGORITHMS , storage_backend )
1749-
1750- length = (use_length and length ) or None
1751- hashes = (use_hashes and hashes ) or None
1755+ length , hashes = _get_hashes_and_length_if_needed (use_length , use_hashes ,
1756+ snapshot_file_path , storage_backend )
17521757
17531758 snapshot_filename = os .path .basename (snapshot_file_path )
17541759 # Retrieve the versioninfo of the Snapshot metadata file.
0 commit comments