diff --git a/awscli/customizations/s3/syncstrategy/ignoreexisting.py b/awscli/customizations/s3/syncstrategy/ignoreexisting.py new file mode 100644 index 000000000000..807778836737 --- /dev/null +++ b/awscli/customizations/s3/syncstrategy/ignoreexisting.py @@ -0,0 +1,34 @@ +# Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"). You +# may not use this file except in compliance with the License. A copy of +# the License is located at +# +# http://aws.amazon.com/apache2.0/ +# +# or in the "license" file accompanying this file. This file is +# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF +# ANY KIND, either express or implied. See the License for the specific +# language governing permissions and limitations under the License. +import logging + +from awscli.customizations.s3.syncstrategy.base import BaseSync + + +LOG = logging.getLogger(__name__) + + +IGNORE_EXISTING = { + 'name': 'ignore_existing', + 'action': 'store_true', + 'help_text': ('If the file exists, do not sync.') +} + + +class IgnoreExistingSync(BaseSync): + + ARGUMENT = IGNORE_EXISTING + + def determine_should_sync(self, src_file, dest_file): + LOG.debug("ignoring existing file %s", src_file) + return False diff --git a/awscli/customizations/s3/syncstrategy/register.py b/awscli/customizations/s3/syncstrategy/register.py index b75674dcb99b..f31d39a56732 100644 --- a/awscli/customizations/s3/syncstrategy/register.py +++ b/awscli/customizations/s3/syncstrategy/register.py @@ -14,6 +14,8 @@ from awscli.customizations.s3.syncstrategy.exacttimestamps import \ ExactTimestampsSync from awscli.customizations.s3.syncstrategy.delete import DeleteSync +from awscli.customizations.s3.syncstrategy.ignoreexisting import \ + IgnoreExistingSync def register_sync_strategy(session, strategy_cls, @@ -46,4 +48,7 @@ def register_sync_strategies(command_table, session, **kwargs): # Register the delete sync strategy. register_sync_strategy(session, DeleteSync, 'file_not_at_src') + # Register the sync once strategy. + register_sync_strategy(session, IgnoreExistingSync) + # Register additional sync strategies here... diff --git a/tests/unit/customizations/s3/syncstrategy/test_ignoreexisting.py b/tests/unit/customizations/s3/syncstrategy/test_ignoreexisting.py new file mode 100644 index 000000000000..b8a4c2a2d9f7 --- /dev/null +++ b/tests/unit/customizations/s3/syncstrategy/test_ignoreexisting.py @@ -0,0 +1,70 @@ +# Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"). You +# may not use this file except in compliance with the License. A copy of +# the License is located at +# +# http://aws.amazon.com/apache2.0/ +# +# or in the "license" file accompanying this file. This file is +# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF +# ANY KIND, either express or implied. See the License for the specific +# language governing permissions and limitations under the License. +import datetime + +from awscli.customizations.s3.filegenerator import FileStat +from awscli.customizations.s3.syncstrategy.ignoreexisting import \ + IgnoreExistingSync + +from awscli.testutils import unittest + + +class TestIgnoreExistingSync(unittest.TestCase): + def setUp(self): + self.sync_strategy = IgnoreExistingSync() + + def test_same_name_download(self): + """ + Confirm that files with the same name are not synced. + """ + time_src = datetime.datetime.now() + time_dst = time_src + datetime.timedelta(days=1) + + src_file = FileStat(src='', dest='', + compare_key='test.py', size=10, + last_update=time_src, src_type='local', + dest_type='s3', operation_name='upload') + + dst_file = FileStat(src='', dest='', + compare_key='test.py', size=11, + last_update=time_dst, src_type='s3', + dest_type='local', operation_name='') + + should_sync = self.sync_strategy.determine_should_sync( + src_file, dst_file) + self.assertFalse(should_sync) + + def test_same_name_upload(self): + """ + Confirm that files with the same name are not synced. + """ + time_src = datetime.datetime.now() + time_dst = time_src + datetime.timedelta(days=1) + + src_file = FileStat(src='', dest='', + compare_key='test.py', size=10, + last_update=time_src, src_type='s3', + dest_type='local', operation_name='download') + + dst_file = FileStat(src='', dest='', + compare_key='test.py', size=11, + last_update=time_dst, src_type='local', + dest_type='s3', operation_name='') + + should_sync = self.sync_strategy.determine_should_sync( + src_file, dst_file) + self.assertFalse(should_sync) + + +if __name__ == "__main__": + unittest.main()