Skip to content

Commit

Permalink
Merge pull request #76 from datreant/rsync-fix
Browse files Browse the repository at this point in the history
Added overwrite option
  • Loading branch information
dotsdl committed Jul 11, 2016
2 parents c1fd665 + 25c569c commit 61af38c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
9 changes: 8 additions & 1 deletion src/datreant/core/rsync.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@


def rsync(source, dest, compress=True, backup=False, dry=False, checksum=True,
include=None, exclude=None, rsync_path='/usr/bin/rsync'):
include=None, exclude=None, overwrite=False,
rsync_path='/usr/bin/rsync'):
"""Wrapper function for rsync. There are some minor differences with the
standard rsync behaviour:
Expand Down Expand Up @@ -33,11 +34,17 @@ def rsync(source, dest, compress=True, backup=False, dry=False, checksum=True,
every other path is excluded
exclude: str or list
Paths to be excluded from the copy
overwrite: bool
If False, files in `dest` that are newer than files in `source`
will not be overwritten
rsync_path: str
Path where to find the rsync executable
"""
opts = ['-r'] # Recursive

if not overwrite:
opts.append('--ignore-existing')

if compress:
opts.append('-z')

Expand Down
19 changes: 19 additions & 0 deletions src/datreant/core/tests/test_rsync.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,25 @@ def _create_tree(name, files=[]):

return tree

def test_overwrite(tmpdir):
with tmpdir.as_cwd():
sequoia = _create_tree('sequoia', ['hello.txt', 'data/hello.txt',
'data/world.dat', 'world.dat'])
sequoia2 = dtr.Tree("sequoia2").makedirs()

# Upload contents
sequoia.sync(sequoia2, mode='upload')

# Change contents
open(sequoia2['hello.txt'].abspath, 'w').write('newcontent')

# Upload contents again
sequoia.sync(sequoia2, mode='upload')

# Verify that the hello.txt is not overwritten
assert sequoia2['hello.txt'].read() == 'newcontent'



def test_excludes(tmpdir):
with tmpdir.as_cwd():
Expand Down
5 changes: 3 additions & 2 deletions src/datreant/core/trees.py
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ def make(self):

def sync(self, other, mode='upload', compress=True, checksum=True,
backup=False, dry=False, include=None, exclude=None,
rsync_path='/usr/bin/rsync'):
overwrite=False, rsync_path='/usr/bin/rsync'):
"""Synchronize directories using rsync.
Parameters
Expand Down Expand Up @@ -501,4 +501,5 @@ def sync(self, other, mode='upload', compress=True, checksum=True,
# Here we do some massaging before passing to the rsync function
return rsync(source, dest, compress=compress, backup=backup,
dry=dry, include=include, checksum=checksum,
exclude=exclude, rsync_path=rsync_path)
overwrite=overwrite, exclude=exclude,
rsync_path=rsync_path)

0 comments on commit 61af38c

Please sign in to comment.