From b5e85dd4e879f88eab3c9d7ba15b6d216b8bfde8 Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Thu, 20 Feb 2014 13:42:19 -0800 Subject: [PATCH] server: Use the Last-Modified header to set last-modified time (mtime) This also sets the access time to the same value, but we're only calling _get_file if we're about to serve the file to a client, which will clobber any value of atime set here. --- package_cache/server.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/package_cache/server.py b/package_cache/server.py index 1da14d6..21213ae 100644 --- a/package_cache/server.py +++ b/package_cache/server.py @@ -1,5 +1,6 @@ # Copyright +import calendar as _calendar import email.utils as _email_utils import mimetypes as _mimetypes import os as _os @@ -73,6 +74,7 @@ class Server (object): def _get_file(self, url, path): 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 +83,9 @@ 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)) def _serve_file(self, path, environ, start_response): headers = { -- 2.26.2