Skip to content

Commit 2a32648

Browse files
author
Rakshil Modi
committed
Override sync strategy
1 parent fdba42b commit 2a32648

File tree

4 files changed

+18
-78
lines changed

4 files changed

+18
-78
lines changed

awscli/customizations/s3/subcommands.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
NeverSync,
4141
SizeAndLastModifiedSync,
4242
)
43+
from awscli.customizations.s3.syncstrategy.nooverwrite import NoOverwriteSync
4344
from awscli.customizations.s3.utils import (
4445
AppendFilter,
4546
RequestParamsMapper,
@@ -1296,10 +1297,15 @@ def choose_sync_strategies(self):
12961297
of its self when the event is emitted.
12971298
"""
12981299
sync_strategies = {}
1299-
# Set the default strategies.
1300-
sync_strategies['file_at_src_and_dest_sync_strategy'] = (
1301-
SizeAndLastModifiedSync()
1302-
)
1300+
if self.parameters.get('no_overwrite'):
1301+
sync_strategies['file_at_src_and_dest_sync_strategy'] = (
1302+
NoOverwriteSync()
1303+
)
1304+
else:
1305+
# Set the default strategies.
1306+
sync_strategies['file_at_src_and_dest_sync_strategy'] = (
1307+
SizeAndLastModifiedSync()
1308+
)
13031309
sync_strategies['file_not_at_dest_sync_strategy'] = MissingFileSync()
13041310
sync_strategies['file_not_at_src_sync_strategy'] = NeverSync()
13051311

@@ -1841,11 +1847,6 @@ def _validate_no_overwrite_for_download_streaming(self):
18411847
"""
18421848
Validates that no-overwrite parameter is not used with streaming downloads.
18431849
1844-
When downloading from S3 to stdout (streaming download), the no-overwrite
1845-
parameter doesn't make sense since stdout is not a file that can be checked
1846-
for existence. This method checks for this invalid combination and raises
1847-
an appropriate error.
1848-
18491850
Raises:
18501851
ParamValidationError: If no-overwrite is specified with a streaming download.
18511852
"""

awscli/customizations/s3/syncstrategy/nooverwrite.py

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,34 +12,20 @@
1212
# language governing permissions and limitations under the License.
1313
import logging
1414

15-
from awscli.customizations.s3.subcommands import NO_OVERWRITE
1615
from awscli.customizations.s3.syncstrategy.base import BaseSync
1716

1817
LOG = logging.getLogger(__name__)
1918

2019

2120
class NoOverwriteSync(BaseSync):
2221
"""Sync strategy that prevents overwriting of existing files at the destination.
23-
This strategy will only sync files that don't exist at the destination.
24-
If a file exists at both source and destination, it will be skipped regardless
25-
of size or modification time differences.
22+
This strategy is used only for files that exist at both source and destination
23+
(file_at_src_and_dest_sync_strategy). It always returns False to prevent any
24+
overwriting of existing files, regardless of size or modification time differences.
2625
"""
2726

28-
ARGUMENT = NO_OVERWRITE
29-
30-
def __init__(self, sync_type='file_not_at_dest'):
31-
super().__init__(sync_type)
32-
3327
def determine_should_sync(self, src_file, dest_file):
34-
if self.sync_type == 'file_not_at_dest':
35-
LOG.debug(
36-
f"syncing: {src_file.src} -> {src_file.dest}, file does not exist at destination"
37-
)
38-
return True
39-
elif self.sync_type == 'file_at_src_and_dest':
40-
# Files exist at both locations must not be synced
41-
LOG.debug(
42-
f"warning: skipping {src_file.src} -> {src_file.dest}, file exists at destination"
43-
)
44-
return False
28+
LOG.debug(
29+
f"warning: skipping {src_file.src} -> {src_file.dest}, file exists at destination"
30+
)
4531
return False

awscli/customizations/s3/syncstrategy/register.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ def register_sync_strategies(command_table, session, **kwargs):
5050
register_sync_strategy(session, DeleteSync, 'file_not_at_src')
5151

5252
# Register the noOverwrite sync strategy
53-
register_sync_strategy(session, NoOverwriteSync, 'file_not_at_dest')
5453
register_sync_strategy(session, NoOverwriteSync, 'file_at_src_and_dest')
5554

5655
# Register additional sync strategies here...

tests/unit/customizations/s3/syncstrategy/test_nooverwrite.py

Lines changed: 2 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -20,56 +20,10 @@
2020

2121
@pytest.fixture
2222
def sync_strategy():
23-
return NoOverwriteSync()
23+
return NoOverwriteSync('file_at_src_and_dest')
2424

2525

26-
def test_file_does_not_exist_at_destination(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='s3',
36-
dest_type='local',
37-
operation_name='download',
38-
)
39-
dest_file = None
40-
should_sync = sync_strategy.determine_should_sync(src_file, dest_file)
41-
assert should_sync
42-
43-
44-
def test_file_exists_at_destination_with_different_key(sync_strategy):
45-
time_src = datetime.datetime.now()
46-
47-
src_file = FileStat(
48-
src='',
49-
dest='',
50-
compare_key='test.py',
51-
size=10,
52-
last_update=time_src,
53-
src_type='s3',
54-
dest_type='s3',
55-
operation_name='copy',
56-
)
57-
dest_file = FileStat(
58-
src='',
59-
dest='',
60-
compare_key='test1.py',
61-
size=100,
62-
last_update=time_src,
63-
src_type='s3',
64-
dest_type='s3',
65-
operation_name='',
66-
)
67-
should_sync = sync_strategy.determine_should_sync(src_file, dest_file)
68-
assert should_sync
69-
70-
71-
def test_file_exists_at_destination_with_same_key():
72-
sync_strategy = NoOverwriteSync('file_at_src_and_dest')
26+
def test_file_exists_at_destination_with_same_key(sync_strategy):
7327
time_src = datetime.datetime.now()
7428

7529
src_file = FileStat(

0 commit comments

Comments
 (0)