server: Log source-requests and errors
[package-cache.git] / package_cache / server.py
index 1da14d645b55335e61e141b010dba0f88b8f98e8..2dfbc1e613c5675ef752b4f0d24a16013120bd8f 100644 (file)
@@ -1,6 +1,23 @@
-# Copyright
-
+# Copyright (C) 2014 W. Trevor King <wking@tremily.us>
+#
+# This file is part of package-cache.
+#
+# package-cache is free software: you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by the Free
+# Software Foundation, either version 3 of the License, or (at your option) any
+# later version.
+#
+# package-cache is distributed in the hope that it will be useful, but WITHOUT
+# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
+# details.
+#
+# You should have received a copy of the GNU General Public License along with
+# package-cache.  If not, see <http://www.gnu.org/licenses/>.
+
+import calendar as _calendar
 import email.utils as _email_utils
+import logging as _logging
 import mimetypes as _mimetypes
 import os as _os
 import urllib.error as _urllib_error
@@ -9,6 +26,9 @@ import urllib.request as _urllib_request
 from . import __version__
 
 
+LOG = _logging.getLogger(__name__)
+
+
 class InvalidFile (ValueError):
     def __init__(self, url):
         super(InvalidFile, self).__init__('invalid file {!r}'.format(url))
@@ -65,14 +85,18 @@ class Server (object):
             source_url = source.rstrip('/') + url
             try:
                 self._get_file(url=source_url, path=path)
-            except _urllib_error.HTTPError:
+            except _urllib_error.HTTPError as e:
+                LOG.warn('error getting {}: {} {}'.format(
+                    source_url, e.code, e.reason))
                 if i == len(self.sources) - 1:
                     raise
             else:
                 return
 
     def _get_file(self, url, path):
+        LOG.info('GET {}'.format(url))
         with self.opener.open(url) as response:
+            last_modified = response.getheader('Last-Modified', None)
             content_length = int(response.getheader('Content-Length'))
             with open(path, 'wb') as f:
                 block_size = 8192
@@ -81,6 +105,10 @@ class Server (object):
                     f.write(data)
                     if len(data) < block_size:
                         break
+        if last_modified:
+            mtime = _calendar.timegm(_email_utils.parsedate(last_modified))
+            _os.utime(path=path, times=(mtime, mtime))
+        LOG.info('got {}'.format(url))
 
     def _serve_file(self, path, environ, start_response):
         headers = {