32
32
from __future__ import unicode_literals
33
33
34
34
import logging
35
+ import six
35
36
import timeit
36
37
import tempfile
37
38
38
- import securesystemslib
39
- import securesystemslib .util
40
- import six
39
+ import securesystemslib # pylint: disable=unused-import
40
+ from securesystemslib import formats as sslib_formats
41
41
42
- import tuf
43
- import tuf . exceptions
44
- import tuf . formats
42
+ from tuf import exceptions
43
+ from tuf import formats
44
+ from tuf import settings
45
45
46
46
# See 'log.py' to learn how logging is handled in TUF.
47
47
logger = logging .getLogger (__name__ )
@@ -53,7 +53,7 @@ def safe_download(url, required_length, fetcher):
53
53
Given the 'url' and 'required_length' of the desired file, open a connection
54
54
to 'url', download it, and return the contents of the file. Also ensure
55
55
the length of the downloaded file matches 'required_length' exactly.
56
- tuf. download.unsafe_download() may be called if an upper download limit is
56
+ download.unsafe_download() may be called if an upper download limit is
57
57
preferred.
58
58
59
59
<Arguments>
@@ -86,8 +86,8 @@ def safe_download(url, required_length, fetcher):
86
86
87
87
# Do all of the arguments have the appropriate format?
88
88
# Raise 'securesystemslib.exceptions.FormatError' if there is a mismatch.
89
- securesystemslib . formats .URL_SCHEMA .check_match (url )
90
- tuf . formats .LENGTH_SCHEMA .check_match (required_length )
89
+ sslib_formats .URL_SCHEMA .check_match (url )
90
+ formats .LENGTH_SCHEMA .check_match (required_length )
91
91
92
92
return _download_file (url , required_length , fetcher , STRICT_REQUIRED_LENGTH = True )
93
93
@@ -101,7 +101,7 @@ def unsafe_download(url, required_length, fetcher):
101
101
Given the 'url' and 'required_length' of the desired file, open a connection
102
102
to 'url', download it, and return the contents of the file. Also ensure
103
103
the length of the downloaded file is up to 'required_length', and no larger.
104
- tuf. download.safe_download() may be called if an exact download limit is
104
+ download.safe_download() may be called if an exact download limit is
105
105
preferred.
106
106
107
107
<Arguments>
@@ -134,8 +134,8 @@ def unsafe_download(url, required_length, fetcher):
134
134
135
135
# Do all of the arguments have the appropriate format?
136
136
# Raise 'securesystemslib.exceptions.FormatError' if there is a mismatch.
137
- securesystemslib . formats .URL_SCHEMA .check_match (url )
138
- tuf . formats .LENGTH_SCHEMA .check_match (required_length )
137
+ sslib_formats .URL_SCHEMA .check_match (url )
138
+ formats .LENGTH_SCHEMA .check_match (required_length )
139
139
140
140
return _download_file (url , required_length , fetcher , STRICT_REQUIRED_LENGTH = False )
141
141
@@ -208,15 +208,14 @@ def _download_file(url, required_length, fetcher, STRICT_REQUIRED_LENGTH=True):
208
208
seconds_spent_receiving = stop_time - start_time
209
209
average_download_speed = number_of_bytes_received / seconds_spent_receiving
210
210
211
- if average_download_speed < tuf . settings .MIN_AVERAGE_DOWNLOAD_SPEED :
211
+ if average_download_speed < settings .MIN_AVERAGE_DOWNLOAD_SPEED :
212
212
logger .debug ('The average download speed dropped below the minimum'
213
- ' average download speed set in tuf.settings.py. Stopping the'
214
- ' download!' )
213
+ ' average download speed set in settings. Stopping the download!.' )
215
214
break
216
215
217
216
else :
218
217
logger .debug ('The average download speed has not dipped below the'
219
- ' minimum average download speed set in tuf. settings.py .' )
218
+ ' minimum average download speed set in settings.' )
220
219
221
220
# Does the total number of downloaded bytes match the required length?
222
221
_check_downloaded_length (number_of_bytes_received , required_length ,
@@ -273,7 +272,7 @@ def _check_downloaded_length(total_downloaded, required_length,
273
272
274
273
tuf.exceptions.SlowRetrievalError, if the total downloaded was
275
274
done in less than the acceptable download speed (as set in
276
- tuf.settings.py ).
275
+ tuf.settings).
277
276
278
277
<Returns>
279
278
None.
@@ -296,24 +295,24 @@ def _check_downloaded_length(total_downloaded, required_length,
296
295
# If the average download speed is below a certain threshold, we flag
297
296
# this as a possible slow-retrieval attack.
298
297
logger .debug ('Average download speed: ' + repr (average_download_speed ))
299
- logger .debug ('Minimum average download speed: ' + repr (tuf . settings .MIN_AVERAGE_DOWNLOAD_SPEED ))
298
+ logger .debug ('Minimum average download speed: ' + repr (settings .MIN_AVERAGE_DOWNLOAD_SPEED ))
300
299
301
- if average_download_speed < tuf . settings .MIN_AVERAGE_DOWNLOAD_SPEED :
302
- raise tuf . exceptions .SlowRetrievalError (average_download_speed )
300
+ if average_download_speed < settings .MIN_AVERAGE_DOWNLOAD_SPEED :
301
+ raise exceptions .SlowRetrievalError (average_download_speed )
303
302
304
303
else :
305
304
logger .debug ('Good average download speed: ' +
306
305
repr (average_download_speed ) + ' bytes per second' )
307
306
308
- raise tuf . exceptions .DownloadLengthMismatchError (required_length , total_downloaded )
307
+ raise exceptions .DownloadLengthMismatchError (required_length , total_downloaded )
309
308
310
309
else :
311
310
# We specifically disabled strict checking of required length, but we
312
311
# will log a warning anyway. This is useful when we wish to download the
313
312
# Timestamp or Root metadata, for which we have no signed metadata; so,
314
313
# we must guess a reasonable required_length for it.
315
- if average_download_speed < tuf . settings .MIN_AVERAGE_DOWNLOAD_SPEED :
316
- raise tuf . exceptions .SlowRetrievalError (average_download_speed )
314
+ if average_download_speed < settings .MIN_AVERAGE_DOWNLOAD_SPEED :
315
+ raise exceptions .SlowRetrievalError (average_download_speed )
317
316
318
317
else :
319
318
logger .debug ('Good average download speed: ' +
0 commit comments