Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sync mtime with updated time #468

Merged
merged 1 commit into from
Jan 8, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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