Skip to content

Commit ec3bb8e

Browse files
author
Rakshil Modi
committed
Adding new Sync strategy
Updated test case Updated doc comment Added test cases
1 parent 7b9bdfd commit ec3bb8e

File tree

3 files changed

+159
-0
lines changed

3 files changed

+159
-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 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.
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: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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+
"""
26+
Confirms that files are synced when not present at destination.
27+
"""
28+
time_src = datetime.datetime.now()
29+
30+
src_file = FileStat(
31+
src='',
32+
dest='',
33+
compare_key='test.py',
34+
size=10,
35+
last_update=time_src,
36+
src_type='s3',
37+
dest_type='local',
38+
operation_name='download',
39+
)
40+
dest_file = None
41+
should_sync = self.sync_strategy.determine_should_sync(
42+
src_file, dest_file
43+
)
44+
self.assertTrue(should_sync)
45+
46+
def test_file_exists_at_destination_with_different_key(self):
47+
"""
48+
Confirms that files are synced when key differs.
49+
"""
50+
time_src = datetime.datetime.now()
51+
52+
src_file = FileStat(
53+
src='',
54+
dest='',
55+
compare_key='test.py',
56+
size=10,
57+
last_update=time_src,
58+
src_type='s3',
59+
dest_type='s3',
60+
operation_name='copy',
61+
)
62+
dest_file = FileStat(
63+
src='',
64+
dest='',
65+
compare_key='test1.py',
66+
size=100,
67+
last_update=time_src,
68+
src_type='s3',
69+
dest_type='s3',
70+
operation_name='',
71+
)
72+
should_sync = self.sync_strategy.determine_should_sync(
73+
src_file, dest_file
74+
)
75+
self.assertTrue(should_sync)
76+
77+
def test_file_exists_at_destination_with_same_key(self):
78+
"""
79+
Confirm that files with the same key are not synced.
80+
"""
81+
time_src = datetime.datetime.now()
82+
83+
src_file = FileStat(
84+
src='',
85+
dest='',
86+
compare_key='test.py',
87+
size=10,
88+
last_update=time_src,
89+
src_type='local',
90+
dest_type='s3',
91+
operation_name='upload',
92+
)
93+
dest_file = FileStat(
94+
src='',
95+
dest='',
96+
compare_key='test.py',
97+
size=100,
98+
last_update=time_src,
99+
src_type='local',
100+
dest_type='s3',
101+
operation_name='',
102+
)
103+
should_sync = self.sync_strategy.determine_should_sync(
104+
src_file, dest_file
105+
)
106+
self.assertFalse(should_sync)
107+
108+
109+
if __name__ == "__main__":
110+
unittest.main()

0 commit comments

Comments
 (0)