Skip to content

Commit

Permalink
Merge pull request #468 from grapefruit623/issue311
Browse files Browse the repository at this point in the history
Sync mtime with updated time
  • Loading branch information
tseaver committed Jan 8, 2015
2 parents f577b30 + c25a6e7 commit 549c175
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
9 changes: 9 additions & 0 deletions gcloud/storage/key.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import copy
import mimetypes
import os
import time
import datetime
from StringIO import StringIO
import urllib

Expand Down Expand Up @@ -254,6 +256,13 @@ def download_to_filename(self, filename):
with open(filename, 'wb') as file_obj:
self.download_to_file(file_obj)

mtime = time.mktime(
datetime.datetime.strptime(
self.properties['updated'],
'%Y-%m-%dT%H:%M:%S.%fz').timetuple()
)
os.utime(file_obj.name, (mtime, mtime))

# NOTE: Alias for boto-like API.
get_contents_to_filename = download_to_filename

Expand Down
13 changes: 12 additions & 1 deletion gcloud/storage/test_key.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,9 @@ def test_download_to_file(self):

def test_download_to_filename(self):
import httplib
import os
import time
import datetime
from tempfile import NamedTemporaryFile
KEY = 'key'
chunk1_response = {'status': httplib.PARTIAL_CONTENT,
Expand All @@ -227,15 +230,23 @@ def test_download_to_filename(self):
)
bucket = _Bucket(connection)
MEDIA_LINK = 'http://example.com/media/'
properties = {'mediaLink': MEDIA_LINK}
properties = {'mediaLink': MEDIA_LINK,
'updated': '2014-12-06T13:13:50.690Z'}
key = self._makeOne(bucket, KEY, properties)
key.CHUNK_SIZE = 3
with NamedTemporaryFile() as f:
key.download_to_filename(f.name)
f.flush()
with open(f.name) as g:
wrote = g.read()
mtime = os.path.getmtime(f.name)
updatedTime = time.mktime(
datetime.datetime.strptime(
key.properties['updated'],
'%Y-%m-%dT%H:%M:%S.%fz').timetuple()
)
self.assertEqual(wrote, 'abcdef')
self.assertEqual(mtime, updatedTime)

def test_download_as_string(self):
import httplib
Expand Down

0 comments on commit 549c175

Please sign in to comment.