Skip to content

Commit b0e8acb

Browse files
committed
Add fetcher as parameter to Updater class
Initialize Updater with an external implementation of FetcherInterface. If not provided, tuf.fetcher.RequestsFetcher is used. Signed-off-by: Teodora Sechkova <[email protected]>
1 parent 2022a1c commit b0e8acb

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

tuf/client/updater.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@
131131

132132
import tuf
133133
import tuf.download
134+
import tuf.fetcher
134135
import tuf.formats
135136
import tuf.settings
136137
import tuf.keydb
@@ -619,7 +620,7 @@ class Updater(object):
619620
http://www.python.org/dev/peps/pep-0008/#method-names-and-instance-variables
620621
"""
621622

622-
def __init__(self, repository_name, repository_mirrors):
623+
def __init__(self, repository_name, repository_mirrors, fetcher=None):
623624
"""
624625
<Purpose>
625626
Constructor. Instantiating an updater object causes all the metadata
@@ -659,6 +660,11 @@ def __init__(self, repository_name, repository_mirrors):
659660
'targets_path': 'targets',
660661
'confined_target_dirs': ['']}}
661662
663+
fetcher:
664+
A concrete 'FetcherInterface' implementation. Performs the network
665+
related download operations. If an external implementation is not
666+
provided, tuf.fetcher.RequestsFetcher is used.
667+
662668
<Exceptions>
663669
securesystemslib.exceptions.FormatError:
664670
If the arguments are improperly formatted.
@@ -688,6 +694,13 @@ def __init__(self, repository_name, repository_mirrors):
688694
self.repository_name = repository_name
689695
self.mirrors = repository_mirrors
690696

697+
# Initialize Updater with an externally provided 'fetcher' implementing
698+
# the network download. By default tuf.fetcher.RequestsFetcher is used.
699+
if fetcher is None:
700+
self.fetcher = tuf.fetcher.RequestsFetcher()
701+
else:
702+
self.fetcher = fetcher
703+
691704
# Store the trusted metadata read from disk.
692705
self.metadata = {}
693706

@@ -1311,7 +1324,8 @@ def _get_target_file(self, target_filepath, file_length, file_hashes,
13111324

13121325
for file_mirror in file_mirrors:
13131326
try:
1314-
file_object = tuf.download.safe_download(file_mirror, file_length)
1327+
file_object = tuf.download.safe_download(file_mirror,
1328+
file_length, self.fetcher)
13151329

13161330
# Verify 'file_object' against the expected length and hashes.
13171331
self._check_file_length(file_object, file_length)
@@ -1509,7 +1523,7 @@ def _get_metadata_file(self, metadata_role, remote_filename,
15091523
for file_mirror in file_mirrors:
15101524
try:
15111525
file_object = tuf.download.unsafe_download(file_mirror,
1512-
upperbound_filelength)
1526+
upperbound_filelength, self.fetcher)
15131527
file_object.seek(0)
15141528

15151529
# Verify 'file_object' according to the callable function.

0 commit comments

Comments
 (0)