Skip to content

Commit 818c2cc

Browse files
author
Rakshil Modi
committed
Adding new Sync strategy
Updated test case
1 parent 7b9bdfd commit 818c2cc

File tree

3 files changed

+150
-0
lines changed

3 files changed

+150
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Copyright 2014 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 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.
26+
"""
27+
28+
ARGUMENT = NO_OVERWRITE
29+
30+
def determine_should_sync(self, src_file, dest_file):
31+
if not dest_file:
32+
# Sync if file does not exist at destination
33+
return True
34+
elif src_file.compare_key != dest_file.compare_key:
35+
# Sync if file exists at both source and destination but the
36+
# source file's key is different than the destination file's key
37+
return True
38+
else:
39+
# Skip if file exists at both source and destination
40+
LOG.debug(
41+
"Warning: Skipping file %s as it already exists on %s",
42+
src_file.src,
43+
src_file.dest,
44+
)
45+
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: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# Copyright 2014 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+
from awscli.customizations.s3.filegenerator import FileStat
16+
from awscli.customizations.s3.syncstrategy.nooverwrite import NoOverwriteSync
17+
from awscli.testutils import unittest
18+
19+
20+
class TestNoOverwriteSync(unittest.TestCase):
21+
def setUp(self):
22+
self.sync_strategy = NoOverwriteSync()
23+
24+
def test_file_does_not_exist_at_destination(self):
25+
time_src = datetime.datetime.now()
26+
27+
src_file = FileStat(
28+
src='',
29+
dest='',
30+
compare_key='test.py',
31+
size=10,
32+
last_update=time_src,
33+
src_type='s3',
34+
dest_type='local',
35+
operation_name='download',
36+
)
37+
dest_file = None
38+
should_sync = self.sync_strategy.determine_should_sync(
39+
src_file, dest_file
40+
)
41+
self.assertTrue(should_sync)
42+
43+
def test_file_exists_at_destination_with_different_key(self):
44+
time_src = datetime.datetime.now()
45+
46+
src_file = FileStat(
47+
src='',
48+
dest='',
49+
compare_key='test.py',
50+
size=10,
51+
last_update=time_src,
52+
src_type='s3',
53+
dest_type='s3',
54+
operation_name='copy',
55+
)
56+
dest_file = FileStat(
57+
src='',
58+
dest='',
59+
compare_key='test1.py',
60+
size=100,
61+
last_update=time_src,
62+
src_type='s3',
63+
dest_type='s3',
64+
operation_name='',
65+
)
66+
should_sync = self.sync_strategy.determine_should_sync(
67+
src_file, dest_file
68+
)
69+
self.assertTrue(should_sync)
70+
71+
def test_file_exists_at_destination_with_same_key(self):
72+
time_src = datetime.datetime.now()
73+
74+
src_file = FileStat(
75+
src='',
76+
dest='',
77+
compare_key='test.py',
78+
size=10,
79+
last_update=time_src,
80+
src_type='local',
81+
dest_type='s3',
82+
operation_name='upload',
83+
)
84+
dest_file = FileStat(
85+
src='',
86+
dest='',
87+
compare_key='test.py',
88+
size=100,
89+
last_update=time_src,
90+
src_type='local',
91+
dest_type='s3',
92+
operation_name='',
93+
)
94+
should_sync = self.sync_strategy.determine_should_sync(
95+
src_file, dest_file
96+
)
97+
self.assertFalse(should_sync)
98+
99+
100+
if __name__ == "__main__":
101+
unittest.main()

0 commit comments

Comments
 (0)