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

Add filename to err msg when we can't stat of file #795

Merged
merged 2 commits into from
May 29, 2014
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
8 changes: 6 additions & 2 deletions awscli/customizations/s3/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,12 @@ def get_file_stat(path):
This is a helper function that given a local path return the size of
the file in bytes and time of last modification.
"""
stats = os.stat(path)
update_time = datetime.fromtimestamp(stats.st_mtime, tzlocal())
try:
stats = os.stat(path)
update_time = datetime.fromtimestamp(stats.st_mtime, tzlocal())
except (ValueError, IOError) as e:
raise ValueError('Could not retrieve file stat of "%s": %s' % (
path, e))
return stats.st_size, update_time


Expand Down
31 changes: 30 additions & 1 deletion tests/unit/customizations/s3/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
from awscli.testutils import unittest
from awscli.testutils import unittest, temporary_file
import os
import tempfile
import shutil
import ntpath
import time
import datetime

import mock
from dateutil.tz import tzlocal

from botocore.hooks import HierarchicalEmitter
from awscli.customizations.s3.utils import find_bucket_key, find_chunksize
Expand All @@ -13,6 +16,7 @@
from awscli.customizations.s3.utils import StablePriorityQueue
from awscli.customizations.s3.utils import BucketLister
from awscli.customizations.s3.utils import ScopedEventHandler
from awscli.customizations.s3.utils import get_file_stat
from awscli.customizations.s3.constants import MAX_SINGLE_UPLOAD_SIZE


Expand Down Expand Up @@ -270,5 +274,30 @@ def test_scoped_session_handler(self):
session.unregister.assert_called_with('eventname', 'handler')


class TestGetFileStat(unittest.TestCase):

def test_get_file_stat(self):
now = datetime.datetime.now(tzlocal())
epoch_now = time.mktime(now.timetuple())
with temporary_file('w') as f:
f.write('foo')
f.flush()
os.utime(f.name, (epoch_now, epoch_now))
size, update_time = get_file_stat(f.name)
self.assertEqual(size, 3)
self.assertEqual(time.mktime(update_time.timetuple()), epoch_now)

def test_get_file_stat_error_message(self):
patch_attribute = 'awscli.customizations.s3.utils.datetime'
with mock.patch(patch_attribute) as f:
with mock.patch('os.stat'):
f.fromtimestamp.side_effect = ValueError(
"timestamp out of range for platform "
"localtime()/gmtime() function")
with self.assertRaisesRegexp(
ValueError, 'myfilename\.txt'):
get_file_stat('myfilename.txt')


if __name__ == "__main__":
unittest.main()