-
Notifications
You must be signed in to change notification settings - Fork 275
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Some refactoring of the updater class.
In order to make the process of downloading metadata files more flexible, finding mirrors and the actual download were moved to a so-called handler class. With that change, it is possible to load a file from a specific commit of a git repository for example Signed-off-by: Renata <vrenata8@gmail.com>
- Loading branch information
Showing
2 changed files
with
169 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
import tuf | ||
import logging | ||
import tuf.exceptions | ||
|
||
logger = logging.getLogger('tuf.client.updater') | ||
|
||
|
||
class MetadataUpdater(object): | ||
""" | ||
<Purpose> | ||
Provide a way to redefine certain parts of the process of updating metadata. | ||
To be more specific, this class should enable redefinition of how metadata | ||
is downloaded. | ||
<Arguments> | ||
mirrors: | ||
A dictionary holding repository mirror information, conformant to | ||
'tuf.formats.MIRRORDICT_SCHEMA'. | ||
repository_directory: | ||
Client's repository directory. Specified via tuf.settings.repositories_directory. | ||
<Exceptions> | ||
None. | ||
<Side Effects> | ||
None. | ||
<Returns> | ||
None. | ||
""" | ||
def __init__(self, mirrors, repository_directory): | ||
self.mirrors = mirrors | ||
self.repository_directory = repository_directory | ||
|
||
class RemoteMetadataUpdater(MetadataUpdater): | ||
""" | ||
Subclass of 'MetadataUpdater' which handles the case of | ||
downloading metadata files from remote mirrors. | ||
""" | ||
|
||
|
||
def get_mirrors(self, remote_filename): | ||
""" | ||
<Purpose> | ||
Finds mirrors from which the specified file can be downloaded. | ||
<Arguments> | ||
remote_filename: | ||
The relative file path (on the remote repository) of a metadata role. | ||
<Exceptions> | ||
None. | ||
Side Effects> | ||
None. | ||
<Returns> | ||
A list of mirrors from which the specified file can be downloaded. | ||
""" | ||
return tuf.mirrors.get_list_of_mirrors('meta', remote_filename, | ||
self.mirrors) | ||
|
||
|
||
def get_metadata_file(self, file_mirror, _filename, _upperbound_filelength): | ||
""" | ||
<Purpose> | ||
Downloads the metadata file from the provided mirror. Calls 'unsafe_download', which, | ||
given the 'url' and 'required_length' of the desired file downloads the file and | ||
returns its contents. | ||
<Arguments> | ||
file_mirror: | ||
Mirror from which the file should be downloaded. | ||
_filename: | ||
The relative file path (on the remote repository) of a metadata role. | ||
_upperbound_filelength: | ||
An integer value representing the upper limit of the length of the file. | ||
<Exceptions> | ||
tuf.ssl_commons.exceptions.DownloadLengthMismatchError, if there was a | ||
mismatch of observed vs expected lengths while downloading the file. | ||
securesystemslib.exceptions.FormatError, if any of the arguments are | ||
improperly formatted. | ||
Any other unforeseen runtime exception. | ||
Side Effects> | ||
A 'securesystemslib.util.TempFile' object is created on disk to store the | ||
contents of 'url'. | ||
<Returns> | ||
A 'securesystemslib.util.TempFile' file-like object that points to the | ||
contents of 'url'. | ||
""" | ||
return tuf.download.unsafe_download(file_mirror, | ||
_upperbound_filelength) | ||
|
||
|
||
def on_successful_update(self, filename, mirror): | ||
""" | ||
<Purpose> | ||
React to successful update of a metadata file 'filename'. Called | ||
after file 'filename' is downloaded from 'mirror' and all | ||
validation checks pass. In this case, nothing needs to be done, | ||
so the method is empty. | ||
<Arguments> | ||
filename: | ||
The relative file path (on the remote repository) of a metadata role. | ||
mirror: | ||
The mirror from whih th file was successfully downloaded. | ||
<Exceptions> | ||
None. | ||
Side Effects> | ||
None. | ||
<Returns> | ||
None. | ||
""" | ||
|
||
|
||
|
||
def on_unsuccessful_update(self, filename): | ||
""" | ||
<Purpose> | ||
React to unsuccessful update of a metadata file 'filename'. Called | ||
after all attempts to download file 'filename' fail. | ||
In this case, nothing needs to be done, so the method is empty. | ||
<Arguments> | ||
filename: | ||
The relative file path (on the remote repository) of a metadata role. | ||
<Exceptions> | ||
None. | ||
Side Effects> | ||
None. | ||
<Returns> | ||
None | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters