commit:     691285b64caf4497d693462751d81a74934b36a1
Author:     Brian Dolbec <dolsen <AT> gentoo <DOT> org>
AuthorDate: Mon Jul  2 21:52:17 2018 +0000
Commit:     Brian Dolbec <dolsen <AT> gentoo <DOT> org>
CommitDate: Sat Jul  7 05:22:11 2018 +0000
URL:        https://gitweb.gentoo.org/proj/gentoo-keys.git/commit/?id=691285b6

gkeys/fetch.py: New Fetch class

Add sig_path assignment, makes the sig_path available to class instance holders 
like _verify()
Use the lack of a returned timestamp to abort the process
If the main file is not downloaded, then don't bother looking for a signature.
This allows a verify action to exit smoothly.
There is a message about the reason for not downloading, so is not hidden.

Signed-off-by: Brian Dolbec <dolsen <AT> gentoo.org>

 gkeys/gkeys/fetch.py | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 63 insertions(+)

diff --git a/gkeys/gkeys/fetch.py b/gkeys/gkeys/fetch.py
new file mode 100644
index 0000000..365124a
--- /dev/null
+++ b/gkeys/gkeys/fetch.py
@@ -0,0 +1,63 @@
+
+from sslfetch.connections import Connector, get_timestamp
+from gkeys import _unicode
+
+EXTENSIONS = ['.sig', '.asc', '.gpg','.gpgsig']
+
+
+class Fetch(object):
+
+    def __init__(self, logger):
+        self.logger = logger
+        connector_output = {
+             'info': self.logger.info,
+             'debug': self.logger.debug,
+             'error': self.logger.error,
+             'exception': self.logger.exception,
+             # we want any warnings to be printed to the terminal
+             # so assign it to logging.error
+             'warning': self.logger.error,
+             'kwargs-info': {},
+             'kwargs-debug': {},
+             'kwargs-error': {},
+             'kwargs-exception': {},
+             'kwargs-warning': {},
+        }
+        self.fetcher = Connector(connector_output, None, "Gentoo Keys")
+        self.sig_path = None
+
+    def fetch_url(self, url, filepath, signature=True, timestamp=None, 
timestamp_path=None, climit=60):
+        if not timestamp_path:
+            timestamp_path = filepath + ".timestamp"
+        messages = []
+        self.logger.debug(
+            _unicode("FETCH: fetching %s signed file ") % filepath)
+        self.logger.debug(
+            _unicode("FETCH: timestamp path: %s") % timestamp_path)
+        success, signedfile, timestamp = self.fetcher.fetch_file(
+            url, filepath, timestamp_path, climit=climit, timestamp=timestamp)
+        if timestamp is '':
+            self.logger.debug("Fetch.fetch_url; file not downloaded")
+            return (False, messages)
+        elif not success:
+            messages.append(_unicode("File %s cannot be retrieved.") % 
filepath)
+        elif '.' + url.rsplit('.', 1)[1] not in EXTENSIONS:
+            self.logger.debug("File %s successfully retrieved.", filepath)
+            if signature:
+                success_fetch = False
+                for ext in EXTENSIONS:
+                    sig_path = filepath + ext
+                    signature = url + ext
+                    self.logger.debug(
+                        _unicode("FETCH: fetching %s signature ")
+                        % signature)
+                    success_fetch, sig, timestamp = 
self.fetcher.fetch_file(signature, sig_path)
+                    if success_fetch:
+                        self.sig_path = sig_path
+                        break
+                    else:
+                        signature = None
+        return (success, messages)
+
+    def verify_cycle(self, tpath, climit=60):
+        return self.fetcher.verify_cycle(tpath, climit=60)

Reply via email to