Skip to content

Commit 17098c9

Browse files
author
Rakshil Modi
committed
Adding new Sync strategy
Updated test case Updated doc comment Added test cases Updating Sync Strategy Updated comments Updated comments Override sync strategy Overridding sync strategy
1 parent b8d8987 commit 17098c9

File tree

4 files changed

+88
-6
lines changed

4 files changed

+88
-6
lines changed

awscli/customizations/s3/subcommands.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1314,7 +1314,6 @@ def choose_sync_strategies(self):
13141314
sync_type = override_sync_strategy.sync_type
13151315
sync_type += '_sync_strategy'
13161316
sync_strategies[sync_type] = override_sync_strategy
1317-
13181317
return sync_strategies
13191318

13201319
def run(self):
@@ -1830,11 +1829,6 @@ def _validate_streaming_no_overwrite_for_download_parameter(self):
18301829
"""
18311830
Validates that no-overwrite parameter is not used with streaming downloads.
18321831
1833-
When downloading from S3 to stdout (streaming download), the no-overwrite
1834-
parameter doesn't make sense since stdout is not a file that can be checked
1835-
for existence. This method checks for this invalid combination and raises
1836-
an appropriate error.
1837-
18381832
Raises:
18391833
ParamValidationError: If no-overwrite is specified with a streaming download.
18401834
"""
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License"). You
4+
# may not use this file except in compliance with the License. A copy of
5+
# the License is located at
6+
#
7+
# http://aws.amazon.com/apache2.0/
8+
#
9+
# or in the "license" file accompanying this file. This file is
10+
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
11+
# ANY KIND, either express or implied. See the License for the specific
12+
# language governing permissions and limitations under the License.
13+
import logging
14+
15+
from awscli.customizations.s3.subcommands import NO_OVERWRITE
16+
from awscli.customizations.s3.syncstrategy.base import BaseSync
17+
18+
LOG = logging.getLogger(__name__)
19+
20+
21+
class NoOverwriteSync(BaseSync):
22+
"""Sync strategy that prevents overwriting of existing files at the destination.
23+
This strategy is used only for files that exist at both source and destination
24+
(file_at_src_and_dest_sync_strategy). It always returns False to prevent any
25+
overwriting of existing files, regardless of size or modification time differences.
26+
"""
27+
28+
ARGUMENT = NO_OVERWRITE
29+
30+
def determine_should_sync(self, src_file, dest_file):
31+
LOG.debug(
32+
f"warning: skipping {src_file.src} -> {src_file.dest}, file exists at destination"
33+
)
34+
return False

awscli/customizations/s3/syncstrategy/register.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from awscli.customizations.s3.syncstrategy.exacttimestamps import (
1515
ExactTimestampsSync,
1616
)
17+
from awscli.customizations.s3.syncstrategy.nooverwrite import NoOverwriteSync
1718
from awscli.customizations.s3.syncstrategy.sizeonly import SizeOnlySync
1819

1920

@@ -48,4 +49,7 @@ def register_sync_strategies(command_table, session, **kwargs):
4849
# Register the delete sync strategy.
4950
register_sync_strategy(session, DeleteSync, 'file_not_at_src')
5051

52+
# Register the noOverwrite sync strategy
53+
register_sync_strategy(session, NoOverwriteSync, 'file_at_src_and_dest')
54+
5155
# Register additional sync strategies here...
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License"). You
4+
# may not use this file except in compliance with the License. A copy of
5+
# the License is located at
6+
#
7+
# http://aws.amazon.com/apache2.0/
8+
#
9+
# or in the "license" file accompanying this file. This file is
10+
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
11+
# ANY KIND, either express or implied. See the License for the specific
12+
# language governing permissions and limitations under the License.
13+
import datetime
14+
15+
import pytest
16+
17+
from awscli.customizations.s3.filegenerator import FileStat
18+
from awscli.customizations.s3.syncstrategy.nooverwrite import NoOverwriteSync
19+
20+
21+
@pytest.fixture
22+
def sync_strategy():
23+
return NoOverwriteSync('file_at_src_and_dest')
24+
25+
26+
def test_file_exists_at_destination_with_same_key(sync_strategy):
27+
time_src = datetime.datetime.now()
28+
29+
src_file = FileStat(
30+
src='',
31+
dest='',
32+
compare_key='test.py',
33+
size=10,
34+
last_update=time_src,
35+
src_type='local',
36+
dest_type='s3',
37+
operation_name='upload',
38+
)
39+
dest_file = FileStat(
40+
src='',
41+
dest='',
42+
compare_key='test.py',
43+
size=100,
44+
last_update=time_src,
45+
src_type='local',
46+
dest_type='s3',
47+
operation_name='',
48+
)
49+
should_sync = sync_strategy.determine_should_sync(src_file, dest_file)
50+
assert not should_sync

0 commit comments

Comments
 (0)