From 882df8e26d64d12a4e157f789fcf0ba2124de371 Mon Sep 17 00:00:00 2001 From: Teodora Sechkova Date: Wed, 8 Apr 2020 18:50:03 +0300 Subject: [PATCH] Remove unnecessary variables Remove unnecessary copying of paths to another list in add_targets() and add_paths() methods. Fix incorrect docstring text. Signed-off-by: Teodora Sechkova --- tuf/repository_tool.py | 28 +++++++++++----------------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/tuf/repository_tool.py b/tuf/repository_tool.py index 70480ed90c..a5b24d517e 100755 --- a/tuf/repository_tool.py +++ b/tuf/repository_tool.py @@ -1854,7 +1854,8 @@ def add_paths(self, paths, child_rolename): securesystemslib.exceptions.Error, if 'child_rolename' has not been delegated yet. - tuf.exceptions.InvalidNameError, if 'pathname' does not match pattern. + tuf.exceptions.InvalidNameError, if any path in 'paths' does not match + pattern. Modifies this Targets' delegations field. @@ -1870,10 +1871,6 @@ def add_paths(self, paths, child_rolename): securesystemslib.formats.PATHS_SCHEMA.check_match(paths) tuf.formats.ROLENAME_SCHEMA.check_match(child_rolename) - # A list of relative and verified paths or glob patterns to be added to the - # child role's entry in the parent's delegations field. - relative_paths = [] - # Ensure that 'child_rolename' exists, otherwise it will not have an entry # in the parent role's delegations field. if not tuf.roledb.role_exists(child_rolename, self._repository_name): @@ -1886,7 +1883,6 @@ def add_paths(self, paths, child_rolename): # on the file system is not verified. If the path is incorrect, # the targetfile won't be matched successfully during a client update. self._check_path(path) - relative_paths.append(path) # Get the current role's roleinfo, so that its delegations field can be # updated. @@ -1895,7 +1891,7 @@ def add_paths(self, paths, child_rolename): # Update the delegated paths of 'child_rolename' to add relative paths. for role in roleinfo['delegations']['roles']: if role['name'] == child_rolename: - for relative_path in relative_paths: + for relative_path in paths: if relative_path not in role['paths']: role['paths'].append(relative_path) @@ -1944,7 +1940,7 @@ def add_target(self, filepath, custom=None, fileinfo=None): securesystemslib.exceptions.FormatError, if 'filepath' is improperly formatted. - tuf.exceptions.InvalidNameError, if 'pathname' does not match pattern. + tuf.exceptions.InvalidNameError, if 'filepath' does not match pattern. Adds 'filepath' to this role's list of targets. This role's @@ -2024,7 +2020,8 @@ def add_targets(self, list_of_targets): securesystemslib.exceptions.FormatError, if the arguments are improperly formatted. - tuf.exceptions.InvalidNameError, if 'pathname' does not match pattern. + tuf.exceptions.InvalidNameError, if any target in 'list_of_targets' + does not match pattern. This Targets' roleinfo is updated with the paths in 'list_of_targets'. @@ -2039,9 +2036,6 @@ def add_targets(self, list_of_targets): # Raise 'securesystemslib.exceptions.FormatError' if there is a mismatch. tuf.formats.RELPATHS_SCHEMA.check_match(list_of_targets) - # Update the tuf.roledb entry. - relative_list_of_targets = [] - # Ensure the paths in 'list_of_targets' are relative and use forward slash # as a separator or raise an exception. The paths of 'list_of_targets' # will be verified as existing and allowed paths according to this Targets @@ -2050,14 +2044,12 @@ def add_targets(self, list_of_targets): # in any order and minimize the number of times these checks are performed. for target in list_of_targets: self._check_path(target) - relative_list_of_targets.append(target) # Update this Targets 'tuf.roledb.py' entry. roleinfo = tuf.roledb.get_roleinfo(self._rolename, self._repository_name) - for relative_target in relative_list_of_targets: + for relative_target in list_of_targets: if relative_target not in roleinfo['paths']: logger.debug('Adding new target: ' + repr(relative_target)) - else: logger.debug('Replacing target: ' + repr(relative_target)) roleinfo['paths'].update({relative_target: {}}) @@ -2285,7 +2277,8 @@ def delegate(self, rolename, public_keys, paths, threshold=1, securesystemslib.exceptions.Error, if the delegated role already exists. - tuf.exceptions.InvalidNameError, if 'pathname' does not match pattern. + tuf.exceptions.InvalidNameError, if any path in 'paths' or target in + 'list_of_targets' does not match pattern. A new Target object is created for 'rolename' that is accessible to the @@ -2490,7 +2483,8 @@ def delegate_hashed_bins(self, list_of_targets, keys_of_hashed_bins, 2, or one of the targets in 'list_of_targets' is not relative to the repository's targets directory. - tuf.exceptions.InvalidNameError, if 'pathname' does not match pattern. + tuf.exceptions.InvalidNameError, if any target in 'list_of_targets' + does not match pattern. Delegates multiple target roles from the current parent role.