From 30afc7b8654923d006d3086ee4c69e248270c41e Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 16 Aug 2007 18:44:47 +0000 Subject: [PATCH] Add -f option to cp --- src/libprs500/devices/errors.py | 3 +++ src/libprs500/devices/prs500/cli/main.py | 12 +++++++++++- src/libprs500/devices/prs500/driver.py | 2 +- 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/libprs500/devices/errors.py b/src/libprs500/devices/errors.py index 35f040cbf8..5e61277f77 100644 --- a/src/libprs500/devices/errors.py +++ b/src/libprs500/devices/errors.py @@ -58,6 +58,9 @@ class ArgumentError(ProtocolError): class PathError(ArgumentError): """ When a user supplies an incorrect/invalid path """ + def __init__(self, msg, path=None): + ArgumentError.__init__(self, msg) + self.path = path class ControlError(ProtocolError): """ Errors in Command/Response pairs while communicating with the device """ diff --git a/src/libprs500/devices/prs500/cli/main.py b/src/libprs500/devices/prs500/cli/main.py index d48fd378ad..84a6477214 100755 --- a/src/libprs500/devices/prs500/cli/main.py +++ b/src/libprs500/devices/prs500/cli/main.py @@ -22,6 +22,7 @@ import StringIO, sys, time, os from optparse import OptionParser from libprs500 import __version__ as VERSION +from libprs500.devices.errors import PathError from libprs500.devices.prs500.driver import PRS500 from libprs500.devices.prs500.cli.terminfo import TerminalController from libprs500.devices.errors import ArgumentError, DeviceError, DeviceLocked @@ -252,6 +253,8 @@ def main(): "source must point to a file for which you have read permissions\n"+\ "destination must point to a file or directory for which you have write permissions" parser = OptionParser(usage=usage) + parser.add_option('-f', '--force', dest='force', action='store_true', default=False, + help='Overwrite the destination file if it exists already.') options, args = parser.parse_args(args) if len(args) != 2: parser.print_help() @@ -277,7 +280,14 @@ def main(): print >> sys.stderr, e parser.print_help() return 1 - dev.put_file(infile, args[1][7:]) + try: + dev.put_file(infile, args[1][7:]) + except PathError, err: + if options.force and 'exists' in str(err): + dev.del_file(err.path, False) + dev.put_file(infile, args[1][7:]) + else: + raise infile.close() else: parser.print_help() diff --git a/src/libprs500/devices/prs500/driver.py b/src/libprs500/devices/prs500/driver.py index c78e490486..e819f1dcf9 100755 --- a/src/libprs500/devices/prs500/driver.py +++ b/src/libprs500/devices/prs500/driver.py @@ -679,7 +679,7 @@ class PRS500(Device): else: if not replace_file: raise PathError("Cannot write to " + \ - path + " as it already exists") + path + " as it already exists", path=path) _file = self.path_properties(path, end_session=False) if _file.file_size > bytes: self.del_file(path, end_session=False)