Skip to content

Commit

Permalink
Rename targetpath to path and make it mandatory
Browse files Browse the repository at this point in the history
Signed-off-by: Martin Vrachev <[email protected]>
  • Loading branch information
MVrachev committed Aug 4, 2021
1 parent bb0dc23 commit 3c4d5dd
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 16 deletions.
2 changes: 1 addition & 1 deletion tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
4 changes: 2 additions & 2 deletions tests/test_metadata_serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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())


Expand Down
16 changes: 8 additions & 8 deletions tuf/api/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -1085,15 +1085,15 @@ 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.
"""

def __init__(
self,
length: int,
hashes: Dict[str, str],
targetname: Optional[str] = None,
path: str,
unrecognized_fields: Optional[Mapping[str, Any]] = None,
) -> None:

Expand All @@ -1102,23 +1102,21 @@ def __init__(

self.length = length
self.hashes = hashes
self.targetname = targetname
self.path = path
self.unrecognized_fields = unrecognized_fields or {}

@property
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."""
Expand Down Expand Up @@ -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)

Expand Down
7 changes: 2 additions & 5 deletions tuf/ngclient/updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -459,7 +457,6 @@ def _preorder_depth_first_walk(self, target_filepath) -> TargetFile:
self.config.max_delegations,
)

target.targetname = target_filepath
return target


Expand Down

0 comments on commit 3c4d5dd

Please sign in to comment.