1- #!/usr/bin/env python
2-
31# Copyright 2012 - 2017, New York University and the TUF contributors
42# SPDX-License-Identifier: MIT OR Apache-2.0
53
4+ """Handles the download of URL contents to a file"
65"""
7- <Program Name>
8- download.py
9-
10- <Started>
11- February 21, 2012. Based on previous version by Geremy Condra.
12-
13- <Author>
14- Konstantin Andrianov
15- Vladimir Diaz <vladimir.v.diaz@gmail.com>
16-
17- <Copyright>
18- See LICENSE-MIT OR LICENSE for licensing information.
196
20- <Purpose>
21- Download metadata and target files and check their validity. The hash and
22- length of a downloaded file has to match the hash and length supplied by the
23- metadata of that file.
24- """
257import logging
268import tempfile
279from contextlib import contextmanager
10+ from typing import IO , Iterator
2811from urllib import parse
2912
3013from tuf import exceptions
3114
3215logger = logging .getLogger (__name__ )
3316
34- @contextmanager
35- def download_file (url , required_length , fetcher ):
36- """
37- <Purpose>
38- Given the url and length of the desired file, this function opens a
39- connection to 'url' and downloads the file up to 'required_length'.
4017
41- <Arguments>
42- url:
43- A URL string that represents the location of the file.
44-
45- required_length:
46- An integer value representing the length of the file or an
47- upper boundary.
48-
49- <Side Effects>
50- A file object is created on disk to store the contents of 'url'.
51-
52- <Exceptions>
53- exceptions.DownloadLengthMismatchError, if there was a
54- mismatch of observed vs expected lengths while downloading the file.
55-
56- Any other unforeseen runtime exception.
57-
58- <Returns>
18+ @contextmanager
19+ def download_file (
20+ url : str , required_length : int , fetcher : "FetcherInterface"
21+ ) -> Iterator [IO ]:
22+ """Opens a connection to 'url' and downloads the content
23+ up to 'required_length'.
24+
25+ Args:
26+ url: a URL string that represents the location of the file.
27+ required_length: an integer value representing the length of
28+ the file or an upper boundary.
29+
30+ Raises:
31+ DownloadLengthMismatchError: a mismatch of observed vs expected
32+ lengths while downloading the file.
33+
34+ Returns:
5935 A file object that points to the contents of 'url'.
6036 """
6137 # 'url.replace('\\', '/')' is needed for compatibility with Windows-based
@@ -82,7 +58,9 @@ def download_file(url, required_length, fetcher):
8258 yield temp_file
8359
8460
85- def download_bytes (url , required_length , fetcher ):
61+ def download_bytes (
62+ url : str , required_length : int , fetcher : "FetcherInterface"
63+ ) -> bytes :
8664 """Download bytes from given url
8765
8866 Returns the downloaded bytes, otherwise like download_file()
0 commit comments