Skip to content

Commit

Permalink
fscp --update command
Browse files Browse the repository at this point in the history
  • Loading branch information
icio committed May 6, 2014
1 parent cb47406 commit d147d6f
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions fs/commands/fscp.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python
from functools import partial
from fs.errors import DestinationExistsError
from fs.errors import DestinationExistsError, DestinationNotOlderError
from fs.utils import copyfile, copyfile_non_atomic
from fs.path import pathjoin, iswildcard
from fs.commands.runner import Command
Expand Down Expand Up @@ -33,7 +33,7 @@ def run(self):
self.dest_fs.makedir(path, recursive=True, allow_recreate=True)
else:
self.action(fs, path, self.dest_fs, dest_path)
except DestinationExistsError, e:
except (DestinationExistsError, DestinationNotOlderError), e:
self.queue.task_done()
self.on_done(path_type, fs, path, self.dest_fs, dest_path, e)
except Exception, e:
Expand All @@ -54,10 +54,12 @@ class FScp(Command):
Copy SOURCE to DESTINATION"""

def get_action(self):
opt = {'update': self.options.update,
'overwrite': self.options.overwrite}
if self.options.threads > 1:
return partial(copyfile_non_atomic, overwrite=self.options.overwrite)
return partial(copyfile_non_atomic, **opt)
else:
return partial(copyfile, overwrite=self.options.overwrite)
return partial(copyfile, **opt)

def get_verb(self):
return 'copying...'
Expand All @@ -66,12 +68,20 @@ def get_optparse(self):
optparse = super(FScp, self).get_optparse()
optparse.add_option('-n', '--no-clobber', dest='overwrite', action="store_false", default=True,
help="do not overwrite an existing file")
optparse.add_option('-u', '--update', dest='update', action="store_true", default=False,
help="copy only when the SOURCE file is newer "
"than the destination file or when the "
"destination file is missing")
optparse.add_option('-p', '--progress', dest='progress', action="store_true", default=False,
help="show progress", metavar="PROGRESS")
optparse.add_option('-t', '--threads', dest='threads', action="store", default=1,
help="number of threads to use", type="int", metavar="THREAD_COUNT")
return optparse


def check_args(self, parser, options):
if options.update and not options.overwrite:
parser.error("--update and --no-clobber are mutually exclusive")

def do_run(self, options, args):

self.options = options
Expand Down Expand Up @@ -212,7 +222,7 @@ def on_done(self, path_type, src_fs, src_path, dst_fs, dst_path, error=None):
if self.options.verbose:
if path_type == self.DIR:
print "mkdir %s" % dst_fs.desc(dst_path)
elif not isinstance(error, DestinationExistsError):
elif not isinstance(error, (DestinationNotOlderError, DestinationExistsError)):
print "%s -> %s" % (src_fs.desc(src_path), dst_fs.desc(dst_path))
elif self.options.progress:
self.done_files += 1
Expand Down

2 comments on commit d147d6f

@icio
Copy link
Contributor Author

@icio icio commented on d147d6f May 6, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$ mkdir a b
$ touch a/{a..g}
$ ls a
a  b  c  d  e  f  g
$ fscp -vu a b
/Users/icio/src/google-code/pyfilesystem/a!a -> /Users/icio/src/google-code/pyfilesystem/b!/a
/Users/icio/src/google-code/pyfilesystem/a!b -> /Users/icio/src/google-code/pyfilesystem/b!/b
/Users/icio/src/google-code/pyfilesystem/a!c -> /Users/icio/src/google-code/pyfilesystem/b!/c
/Users/icio/src/google-code/pyfilesystem/a!d -> /Users/icio/src/google-code/pyfilesystem/b!/d
/Users/icio/src/google-code/pyfilesystem/a!e -> /Users/icio/src/google-code/pyfilesystem/b!/e
/Users/icio/src/google-code/pyfilesystem/a!f -> /Users/icio/src/google-code/pyfilesystem/b!/f
/Users/icio/src/google-code/pyfilesystem/a!g -> /Users/icio/src/google-code/pyfilesystem/b!/g
$ fscp -vu a b
$ touch a/{a..d}
$ fscp -vu a b
/Users/icio/src/google-code/pyfilesystem/a!a -> /Users/icio/src/google-code/pyfilesystem/b!/a
/Users/icio/src/google-code/pyfilesystem/a!b -> /Users/icio/src/google-code/pyfilesystem/b!/b
/Users/icio/src/google-code/pyfilesystem/a!c -> /Users/icio/src/google-code/pyfilesystem/b!/c
/Users/icio/src/google-code/pyfilesystem/a!d -> /Users/icio/src/google-code/pyfilesystem/b!/d
$ fscp -vu a b

@icio
Copy link
Contributor Author

@icio icio commented on d147d6f May 6, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$ fscp -nu a b
Usage: fscp [OPTION]... [SOURCE]... [DESTINATION]
Copy SOURCE to DESTINATION

fscp: error: --update and --no-clobber are mutually exclusive
$ echo $?
2

Please sign in to comment.