Skip to content

Commit

Permalink
ngclient: Rename get_one_valid_targetinfo()
Browse files Browse the repository at this point in the history
This is slightly cosmetic but rename get_one_valid_targetinfo to
get_targetinfo:
* The function name is long without any reason: "one" and "valid" are
  always implicit
* shortening makes code (incl. our examples and tests) easier to read
* We're also already changing updater API (compared to legacy) so this
  alone does not break things -- it's also not a difficult "port".

Signed-off-by: Jussi Kukkonen <jkukkonen@vmware.com>
  • Loading branch information
Jussi Kukkonen committed Oct 6, 2021
1 parent 8e24f69 commit 3870e46
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 24 deletions.
16 changes: 8 additions & 8 deletions tests/test_updater_ng.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,8 @@ def consistent_snapshot_modifier(root):
)

# Get targetinfos, assert cache does not contain the files
info1 = updater.get_one_valid_targetinfo("file1.txt")
info3 = updater.get_one_valid_targetinfo("file3.txt")
info1 = updater.get_targetinfo("file1.txt")
info3 = updater.get_targetinfo("file3.txt")
self.assertIsNone(updater.find_cached_target(info1))
self.assertIsNone(updater.find_cached_target(info3))

Expand Down Expand Up @@ -238,11 +238,11 @@ def test_refresh_and_download(self):
self._assert_files(["root", "snapshot", "targets", "timestamp"])

# Get targetinfos, assert that cache does not contain files
info1 = self.updater.get_one_valid_targetinfo("file1.txt")
info1 = self.updater.get_targetinfo("file1.txt")
self._assert_files(["root", "snapshot", "targets", "timestamp"])

# Get targetinfo for 'file3.txt' listed in the delegated role1
info3 = self.updater.get_one_valid_targetinfo("file3.txt")
info3 = self.updater.get_targetinfo("file3.txt")
expected_files = ["role1", "root", "snapshot", "targets", "timestamp"]
self._assert_files(expected_files)
self.assertIsNone(self.updater.find_cached_target(info1))
Expand Down Expand Up @@ -275,7 +275,7 @@ def test_refresh_with_only_local_root(self):
self._assert_files(["root", "snapshot", "targets", "timestamp"])

# Get targetinfo for 'file3.txt' listed in the delegated role1
targetinfo3 = self.updater.get_one_valid_targetinfo("file3.txt")
targetinfo3 = self.updater.get_targetinfo("file3.txt")
expected_files = ["role1", "root", "snapshot", "targets", "timestamp"]
self._assert_files(expected_files)

Expand All @@ -297,13 +297,13 @@ def test_no_target_dir_no_filepath(self):

def test_external_targets_url(self):
self.updater.refresh()
info = self.updater.get_one_valid_targetinfo("file1.txt")
info = self.updater.get_targetinfo("file1.txt")

self.updater.download_target(info, target_base_url=self.targets_url)

def test_length_hash_mismatch(self):
self.updater.refresh()
targetinfo = self.updater.get_one_valid_targetinfo("file1.txt")
targetinfo = self.updater.get_targetinfo("file1.txt")

length = targetinfo.length
with self.assertRaises(exceptions.RepositoryError):
Expand All @@ -325,7 +325,7 @@ def test_missing_targetinfo(self):
self.updater.refresh()

# Get targetinfo for non-existing file
self.assertIsNone(self.updater.get_one_valid_targetinfo("file33.txt"))
self.assertIsNone(self.updater.get_targetinfo("file33.txt"))


if __name__ == "__main__":
Expand Down
2 changes: 1 addition & 1 deletion tests/test_updater_with_simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def test_targets(self):
path = os.path.join(self.targets_dir, local_file)

# target now exists, is not in cache yet
info = updater.get_one_valid_targetinfo(targetpath)
info = updater.get_targetinfo(targetpath)
self.assertIsNotNone(info)
# Test without and with explicit local filepath
self.assertIsNone(updater.find_cached_target(info))
Expand Down
27 changes: 12 additions & 15 deletions tuf/ngclient/updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
snapshot is consistent so multiple targets can be downloaded without
fear of repository content changing. For each target:
* :func:`~tuf.ngclient.updater.Updater.get_one_valid_targetinfo()` is
* :func:`~tuf.ngclient.updater.Updater.get_targetinfo()` is
used to find information about a specific target. This will load new
targets metadata as needed (from local cache or remote repository).
* :func:`~tuf.ngclient.updater.Updater.find_cached_target()` can be used
Expand Down Expand Up @@ -56,7 +56,7 @@
updater.refresh()
# Update metadata, then download target if needed
info = updater.get_one_valid_targetinfo("file.txt")
info = updater.get_targetinfo("file.txt")
path = updater.find_cached_target(info)
if path is None:
path = updater.download_target(info)
Expand Down Expand Up @@ -131,7 +131,7 @@ def refresh(self) -> None:
all the checks required in the TUF client workflow.
The metadata for delegated roles are not refreshed by this method as
that happens on demand during get_one_valid_targetinfo().
that happens on demand during get_targetinfo().
The refresh() method should be called by the client before any other
method calls.
Expand All @@ -147,19 +147,16 @@ def refresh(self) -> None:
self._load_snapshot()
self._load_targets("targets", "root")

def get_one_valid_targetinfo(
self, target_path: str
) -> Optional[TargetFile]:
def get_targetinfo(self, target_path: str) -> Optional[TargetFile]:
"""Returns TargetFile instance with information for 'target_path'.
The return value can be used as an argument to
:func:`download_target()` and :func:`find_cached_target()`.
:func:`refresh()` must be called before calling
`get_one_valid_targetinfo()`. Subsequent calls to
`get_one_valid_targetinfo()` will use the same consistent repository
`get_targetinfo()`. Subsequent calls to
`get_targetinfo()` will use the same consistent repository
state: Changes that happen in the repository between calling
:func:`refresh()` and `get_one_valid_targetinfo()` will not be
:func:`refresh()` and `get_targetinfo()` will not be
seen by the updater.
As a side-effect this method downloads all the additional (delegated
Expand Down Expand Up @@ -194,15 +191,15 @@ def find_cached_target(
Args:
targetinfo: TargetFile from ``get_one_valid_targetinfo()``.
targetinfo: TargetFile from ``get_targetinfo()``.
filepath: Local path to file.
Raises:
ValueError: Incorrect arguments
Returns:
Local file path if the file is an up to date target file, or None
if file is not found or it is not up to date.
Local file path if the file is an up to date target file.
None if file is not found or it is not up to date.
"""
if filepath is not None:
pass
Expand Down Expand Up @@ -234,7 +231,7 @@ def download_target(
Args:
targetinfo: TargetFile with target information from
get_one_valid_targetinfo().
get_targetinfo().
filepath: local path to download into. If the file exists it will
be overwritten.
target_base_url: Base URL used to form the final target
Expand All @@ -246,7 +243,7 @@ def download_target(
TODO: file write errors
Returns:
Path to downloaded file
Local path to downloaded file
"""

if filepath is not None:
Expand Down

0 comments on commit 3870e46

Please sign in to comment.