-# 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
from . import __version__
+LOG = _logging.getLogger(__name__)
+
+
class InvalidFile (ValueError):
def __init__(self, url):
super(InvalidFile, self).__init__('invalid file {!r}'.format(url))
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
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 = {