Skip to content

Commit 546bb78

Browse files
committed
Fix client imports
Fix imports to be vendoring compatible. Signed-off-by: Teodora Sechkova <[email protected]>
1 parent 2f0bbd0 commit 546bb78

File tree

5 files changed

+37
-38
lines changed

5 files changed

+37
-38
lines changed

tuf/client_rework/download.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,10 @@
2828
import timeit
2929
from urllib import parse
3030

31-
import securesystemslib
32-
import securesystemslib.util
31+
from securesystemslib import formats as sslib_formats
3332

3433
import tuf
35-
import tuf.exceptions
36-
import tuf.formats
34+
from tuf import exceptions, formats
3735

3836
# See 'log.py' to learn how logging is handled in TUF.
3937
logger = logging.getLogger(__name__)
@@ -65,7 +63,7 @@ def download_file(url, required_length, fetcher, strict_required_length=True):
6563
A file object is created on disk to store the contents of 'url'.
6664
6765
<Exceptions>
68-
tuf.exceptions.DownloadLengthMismatchError, if there was a
66+
exceptions.DownloadLengthMismatchError, if there was a
6967
mismatch of observed vs expected lengths while downloading the file.
7068
7169
securesystemslib.exceptions.FormatError, if any of the arguments are
@@ -78,8 +76,8 @@ def download_file(url, required_length, fetcher, strict_required_length=True):
7876
"""
7977
# Do all of the arguments have the appropriate format?
8078
# Raise 'securesystemslib.exceptions.FormatError' if there is a mismatch.
81-
securesystemslib.formats.URL_SCHEMA.check_match(url)
82-
tuf.formats.LENGTH_SCHEMA.check_match(required_length)
79+
sslib_formats.URL_SCHEMA.check_match(url)
80+
formats.LENGTH_SCHEMA.check_match(required_length)
8381

8482
# 'url.replace('\\', '/')' is needed for compatibility with Windows-based
8583
# systems, because they might use back-slashes in place of forward-slashes.
@@ -183,7 +181,7 @@ def _check_downloaded_length(
183181
strict_required_length is True and total_downloaded is not equal
184182
required_length.
185183
186-
tuf.exceptions.SlowRetrievalError, if the total downloaded was
184+
exceptions.SlowRetrievalError, if the total downloaded was
187185
done in less than the acceptable download speed (as set in
188186
tuf.settings.py).
189187
@@ -222,15 +220,15 @@ def _check_downloaded_length(
222220
)
223221

224222
if average_download_speed < tuf.settings.MIN_AVERAGE_DOWNLOAD_SPEED:
225-
raise tuf.exceptions.SlowRetrievalError(average_download_speed)
223+
raise exceptions.SlowRetrievalError(average_download_speed)
226224

227225
logger.debug(
228226
"Good average download speed: "
229227
+ repr(average_download_speed)
230228
+ " bytes per second"
231229
)
232230

233-
raise tuf.exceptions.DownloadLengthMismatchError(
231+
raise exceptions.DownloadLengthMismatchError(
234232
required_length, total_downloaded
235233
)
236234

@@ -240,7 +238,7 @@ def _check_downloaded_length(
240238
# signed metadata; so, we must guess a reasonable required_length
241239
# for it.
242240
if average_download_speed < tuf.settings.MIN_AVERAGE_DOWNLOAD_SPEED:
243-
raise tuf.exceptions.SlowRetrievalError(average_download_speed)
241+
raise exceptions.SlowRetrievalError(average_download_speed)
244242

245243
logger.debug(
246244
"Good average download speed: "

tuf/client_rework/metadata_wrapper.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
from securesystemslib.keys import format_metadata_to_key
1111

12-
import tuf.exceptions
12+
from tuf import exceptions, formats
1313
from tuf.api import metadata
1414

1515

@@ -64,7 +64,7 @@ def verify(self, keys, threshold):
6464
verified += 1
6565

6666
if verified < threshold:
67-
raise tuf.exceptions.InsufficientKeysError
67+
raise exceptions.InsufficientKeysError
6868

6969
def persist(self, filename):
7070
"""
@@ -77,13 +77,13 @@ def expires(self, reference_time=None):
7777
TODO
7878
"""
7979
if reference_time is None:
80-
expires_timestamp = tuf.formats.datetime_to_unix_timestamp(
80+
expires_timestamp = formats.datetime_to_unix_timestamp(
8181
self._meta.signed.expires
8282
)
8383
reference_time = int(time.time())
8484

8585
if expires_timestamp < reference_time:
86-
raise tuf.exceptions.ExpiredMetadataError
86+
raise exceptions.ExpiredMetadataError
8787

8888

8989
class RootWrapper(MetadataWrapper):

tuf/client_rework/mirrors.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,12 @@
2626
from typing import BinaryIO, Dict, TextIO
2727
from urllib import parse
2828

29-
import securesystemslib
29+
from securesystemslib import exceptions as sslib_exceptions
30+
from securesystemslib import formats as sslib_formats
31+
from securesystemslib import util as sslib_util
3032

31-
import tuf
32-
import tuf.client_rework.download as download
33-
import tuf.formats
33+
from tuf import exceptions, formats
34+
from tuf.client_rework import download
3435

3536
# The type of file to be downloaded from a repository. The
3637
# 'get_list_of_mirrors' function supports these file types.
@@ -78,13 +79,13 @@ def get_list_of_mirrors(file_type, file_path, mirrors_dict):
7879
"""
7980

8081
# Checking if all the arguments have appropriate format.
81-
tuf.formats.RELPATH_SCHEMA.check_match(file_path)
82-
tuf.formats.MIRRORDICT_SCHEMA.check_match(mirrors_dict)
83-
securesystemslib.formats.NAME_SCHEMA.check_match(file_type)
82+
formats.RELPATH_SCHEMA.check_match(file_path)
83+
formats.MIRRORDICT_SCHEMA.check_match(mirrors_dict)
84+
sslib_formats.NAME_SCHEMA.check_match(file_type)
8485

8586
# Verify 'file_type' is supported.
8687
if file_type not in _SUPPORTED_FILE_TYPES:
87-
raise securesystemslib.exceptions.Error(
88+
raise sslib_exceptions.Error(
8889
"Invalid file_type argument."
8990
" Supported file types: " + repr(_SUPPORTED_FILE_TYPES)
9091
)
@@ -96,7 +97,7 @@ def get_list_of_mirrors(file_type, file_path, mirrors_dict):
9697
# on a repository mirror when fetching target files. This field may be set
9798
# by the client when the repository mirror is added to the
9899
# 'tuf.client.updater.Updater' object.
99-
in_confined_directory = securesystemslib.util.file_in_confined_directories
100+
in_confined_directory = sslib_util.file_in_confined_directories
100101

101102
list_of_mirrors = []
102103
for mirror_info in mirrors_dict.values():
@@ -160,7 +161,7 @@ def mirror_meta_download(
160161

161162
finally:
162163
if file_mirror_errors:
163-
raise tuf.exceptions.NoWorkingMirrorError(file_mirror_errors)
164+
raise exceptions.NoWorkingMirrorError(file_mirror_errors)
164165

165166

166167
def mirror_target_download(
@@ -192,4 +193,4 @@ def mirror_target_download(
192193

193194
finally:
194195
if file_mirror_errors:
195-
raise tuf.exceptions.NoWorkingMirrorError(file_mirror_errors)
196+
raise exceptions.NoWorkingMirrorError(file_mirror_errors)

tuf/client_rework/requests_fetcher.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
import requests
1414
import urllib3.exceptions
1515

16-
import tuf.exceptions
17-
import tuf.settings
16+
import tuf
17+
from tuf import exceptions, settings
1818
from tuf.client_rework.fetcher import FetcherInterface
1919

2020
# Globals
@@ -58,9 +58,9 @@ def fetch(self, url, required_length):
5858
bytes.
5959
6060
Raises:
61-
tuf.exceptions.SlowRetrievalError: A timeout occurs while receiving
61+
exceptions.SlowRetrievalError: A timeout occurs while receiving
6262
data.
63-
tuf.exceptions.FetcherHTTPError: An HTTP error code is received.
63+
exceptions.FetcherHTTPError: An HTTP error code is received.
6464
6565
Returns:
6666
A bytes iterator
@@ -76,15 +76,15 @@ def fetch(self, url, required_length):
7676
# - connect timeout (max delay before first byte is received)
7777
# - read (gap) timeout (max delay between bytes received)
7878
response = session.get(
79-
url, stream=True, timeout=tuf.settings.SOCKET_TIMEOUT
79+
url, stream=True, timeout=settings.SOCKET_TIMEOUT
8080
)
8181
# Check response status.
8282
try:
8383
response.raise_for_status()
8484
except requests.HTTPError as e:
8585
response.close()
8686
status = e.response.status_code
87-
raise tuf.exceptions.FetcherHTTPError(str(e), status)
87+
raise exceptions.FetcherHTTPError(str(e), status)
8888

8989
# Define a generator function to be returned by fetch. This way the
9090
# caller of fetch can differentiate between connection and actual data
@@ -99,11 +99,11 @@ def chunks():
9999
# large file in one shot. Before beginning the round, sleep
100100
# (if set) for a short amount of time so that the CPU is not
101101
# hogged in the while loop.
102-
if tuf.settings.SLEEP_BEFORE_ROUND:
103-
time.sleep(tuf.settings.SLEEP_BEFORE_ROUND)
102+
if settings.SLEEP_BEFORE_ROUND:
103+
time.sleep(settings.SLEEP_BEFORE_ROUND)
104104

105105
read_amount = min(
106-
tuf.settings.CHUNK_SIZE,
106+
settings.CHUNK_SIZE,
107107
required_length - bytes_received,
108108
)
109109

@@ -134,7 +134,7 @@ def chunks():
134134
break
135135

136136
except urllib3.exceptions.ReadTimeoutError as e:
137-
raise tuf.exceptions.SlowRetrievalError(str(e))
137+
raise exceptions.SlowRetrievalError(str(e))
138138

139139
finally:
140140
response.close()
@@ -150,7 +150,7 @@ def _get_session(self, url):
150150
parsed_url = parse.urlparse(url)
151151

152152
if not parsed_url.scheme or not parsed_url.hostname:
153-
raise tuf.exceptions.URLParsingError(
153+
raise exceptions.URLParsingError(
154154
"Could not get scheme and hostname from URL: " + url
155155
)
156156

tuf/client_rework/updater_rework.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
from tuf import exceptions, settings
2020
from tuf.client.fetcher import FetcherInterface
21-
from tuf.client_rework import download, mirrors, requests_fetcher
21+
from tuf.client_rework import mirrors, requests_fetcher
2222

2323
from .metadata_wrapper import (
2424
RootWrapper,

0 commit comments

Comments
 (0)