From 3c4d5dd91549062273fde03deef36a93f0a5580b Mon Sep 17 00:00:00 2001 From: Martin Vrachev Date: Wed, 4 Aug 2021 12:44:15 +0300 Subject: [PATCH] Rename targetpath to path and make it mandatory Signed-off-by: Martin Vrachev --- tests/test_api.py | 2 +- tests/test_metadata_serialization.py | 4 ++-- tuf/api/metadata.py | 16 ++++++++-------- tuf/ngclient/updater.py | 7 ++----- 4 files changed, 13 insertions(+), 16 deletions(-) diff --git a/tests/test_api.py b/tests/test_api.py index 8a3045d44e..6dbf942015 100755 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -539,7 +539,7 @@ def test_metadata_targets(self): "sha512": "ef5beafa16041bcdd2937140afebd485296cd54f7348ecd5a4d035c09759608de467a7ac0eb58753d0242df873c305e8bffad2454aa48f44480f15efae1cacd0" } - fileinfo = TargetFile(length=28, hashes=hashes) + fileinfo = TargetFile(length=28, hashes=hashes, path=filename) # Assert that data is not aleady equal self.assertNotEqual( diff --git a/tests/test_metadata_serialization.py b/tests/test_metadata_serialization.py index 9f228d4c8f..1845394ce7 100644 --- a/tests/test_metadata_serialization.py +++ b/tests/test_metadata_serialization.py @@ -282,7 +282,7 @@ def test_delegation_serialization(self, test_case_data: str): def test_invalid_targetfile_serialization(self, test_case_data: Dict[str, str]): case_dict = json.loads(test_case_data) with self.assertRaises(KeyError): - TargetFile.from_dict(copy.deepcopy(case_dict)) + TargetFile.from_dict(copy.deepcopy(case_dict), "file1.txt") valid_targetfiles: DataSet = { @@ -296,7 +296,7 @@ def test_invalid_targetfile_serialization(self, test_case_data: Dict[str, str]): @run_sub_tests_with_dataset(valid_targetfiles) def test_targetfile_serialization(self, test_case_data: str): case_dict = json.loads(test_case_data) - target_file = TargetFile.from_dict(copy.copy(case_dict)) + target_file = TargetFile.from_dict(copy.copy(case_dict), "file1.txt") self.assertDictEqual(case_dict, target_file.to_dict()) diff --git a/tuf/api/metadata.py b/tuf/api/metadata.py index 66bb30247e..c7cbd57511 100644 --- a/tuf/api/metadata.py +++ b/tuf/api/metadata.py @@ -1085,7 +1085,7 @@ class TargetFile(BaseFile): Attributes: length: An integer indicating the length of the target file. hashes: A dictionary of hash algorithm names to hash values. - targetname: An optional string denoting the target file name. + path: A string denoting the target file path. unrecognized_fields: Dictionary of all unrecognized fields. """ @@ -1093,7 +1093,7 @@ def __init__( self, length: int, hashes: Dict[str, str], - targetname: Optional[str] = None, + path: str, unrecognized_fields: Optional[Mapping[str, Any]] = None, ) -> None: @@ -1102,7 +1102,7 @@ def __init__( self.length = length self.hashes = hashes - self.targetname = targetname + self.path = path self.unrecognized_fields = unrecognized_fields or {} @property @@ -1110,15 +1110,13 @@ def custom(self) -> Any: return self.unrecognized_fields.get("custom", None) @classmethod - def from_dict( - cls, target_dict: Dict[str, Any], targetname: Optional[str] = None - ) -> "TargetFile": + def from_dict(cls, target_dict: Dict[str, Any], path: str) -> "TargetFile": """Creates TargetFile object from its dict representation.""" length = target_dict.pop("length") hashes = target_dict.pop("hashes") # All fields left in the target_dict are unrecognized. - return cls(length, hashes, targetname, target_dict) + return cls(length, hashes, path, target_dict) def to_dict(self) -> Dict[str, Any]: """Returns the JSON-serializable dictionary representation of self.""" @@ -1184,7 +1182,9 @@ def from_dict(cls, signed_dict: Dict[str, Any]) -> "Targets": delegations = Delegations.from_dict(delegations_dict) res_targets = {} for target_path, target_info in targets.items(): - res_targets[target_path] = TargetFile.from_dict(target_info) + res_targets[target_path] = TargetFile.from_dict( + target_info, target_path + ) # All fields left in the targets_dict are unrecognized. return cls(*common_args, res_targets, delegations, signed_dict) diff --git a/tuf/ngclient/updater.py b/tuf/ngclient/updater.py index 55b1c13467..536ea33b7f 100644 --- a/tuf/ngclient/updater.py +++ b/tuf/ngclient/updater.py @@ -199,9 +199,7 @@ def updated_targets( # against each hash listed for its fileinfo. Note: join() discards # 'destination_directory' if 'filepath' contains a leading path # separator (i.e., is treated as an absolute path). - target_filepath = os.path.join( - destination_directory, target.targetname - ) + target_filepath = os.path.join(destination_directory, target.path) if target_filepath in updated_targetpaths: continue @@ -248,7 +246,7 @@ def download_target( else: target_base_url = _ensure_trailing_slash(target_base_url) - target_filepath = targetinfo.targetname + target_filepath = targetinfo.path full_url = parse.urljoin(target_base_url, target_filepath) with self._fetcher.download_file( @@ -459,7 +457,6 @@ def _preorder_depth_first_walk(self, target_filepath) -> TargetFile: self.config.max_delegations, ) - target.targetname = target_filepath return target