Commit f1f224c1 by willmcgugan

Fixes for zip opener

parent 40c775ea
......@@ -29,7 +29,7 @@ class FileOpThread(threading.Thread):
except queue.Empty:
continue
try:
if path_type == FSCopy.DIR:
if path_type == FScp.DIR:
self.dest_fs.makedir(path, recursive=True, allow_recreate=True)
else:
self.action(fs, path, self.dest_fs, dest_path, overwrite=True)
......@@ -43,7 +43,7 @@ class FileOpThread(threading.Thread):
except Exception, e:
self.on_error(e)
class FSCopy(Command):
class FScp(Command):
DIR, FILE = 0, 1
......@@ -54,7 +54,7 @@ Copy SOURCE to DESTINATION"""
return copyfile
def get_optparse(self):
optparse = super(FSCopy, self).get_optparse()
optparse = super(FScp, self).get_optparse()
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,
......@@ -100,8 +100,9 @@ Copy SOURCE to DESTINATION"""
if src_fs.isdir(src_path):
self.root_dirs.append((src_fs, src_path))
src_sub_fs = src_fs.opendir(src_path)
for dir_path, file_paths in src_sub_fs.walk():
copy_fs_paths.append((self.DIR, src_sub_fs, dir_path, dir_path))
for dir_path, file_paths in src_sub_fs.walk():
if dir_path not in ('', '/'):
copy_fs_paths.append((self.DIR, src_sub_fs, dir_path, dir_path))
sub_fs = src_sub_fs.opendir(dir_path)
for file_path in file_paths:
copy_fs_paths.append((self.FILE, sub_fs, file_path, pathjoin(dir_path, file_path)))
......@@ -111,8 +112,7 @@ Copy SOURCE to DESTINATION"""
else:
self.error('%s is not a file or directory\n' % src_path)
return 1
if self.options.threads > 1:
copy_fs_dirs = [r for r in copy_fs_paths if r[0] == self.DIR]
copy_fs_paths = [r for r in copy_fs_paths if r[0] == self.FILE]
......@@ -150,8 +150,7 @@ Copy SOURCE to DESTINATION"""
# caught until the queue is finished
#file_queue.join()
except KeyboardInterrupt:
print "!"
except KeyboardInterrupt:
options.progress = False
if self.action_error:
self.error(self.wrap_error(unicode(self.action_error)) + '\n')
......@@ -229,7 +228,7 @@ Copy SOURCE to DESTINATION"""
return bar
def run():
return FSCopy().run()
return FScp().run()
if __name__ == "__main__":
sys.exit(run())
......@@ -213,7 +213,7 @@ class Command(object):
help="make output verbose", metavar="VERBOSE")
return optparse
def run(self):
def run(self):
parser = self.get_optparse()
options, args = parser.parse_args()
args = [unicode(arg, sys.getfilesystemencoding()) for arg in args]
......@@ -227,13 +227,15 @@ class Command(object):
if self.is_terminal():
self.output("\n")
return 0
except ValueError:
pass
except SystemExit:
return 0
#except IOError:
# return 1
#except Exception, e:
# self.error(self.wrap_error('Internal Error - %s\n' % unicode(e)))
# return 1
except IOError:
return 1
except Exception, e:
self.error(self.wrap_error('Internal Error - %s\n' % unicode(e)))
return 1
......
......@@ -157,9 +157,9 @@ class ZipOpener(Opener):
@classmethod
def get_fs(cls, registry, fs_name, fs_name_params, fs_path, writeable, create):
create_zip = fs_name_params == 'new'
create_zip = fs_name_params == 'new'
append_zip = fs_name_params == 'append'
zip_file = None
if fs_path.startswith('['):
......@@ -168,27 +168,33 @@ class ZipOpener(Opener):
raise OpenerError("Not a file")
container_mode = 'r+b'
if create_zip:
container_mode = 'w+'
zip_file = container_fs.open(container_path, mode=container_mode)
container_mode = 'w+b'
elif writeable:
container_mode = 'w+b'
zip_file = container_fs.open(container_path, mode=container_mode)
username, password, fs_path = registry.parse_credentials(fs_path)
from fs.zipfs import ZipFS
if zip_file is None:
zip_file = fs_path
if create_zip:
if append_zip:
mode = 'a'
elif create_zip or create:
mode = 'w'
else:
if writeable:
mode = 'a'
mode = 'w'
else:
mode = 'r'
mode = 'a'
if fs_name == 'zip64':
allow_zip_64 = True
else:
allow_zip_64 = False
allow_zip_64 = False
zipfs = ZipFS(zip_file, mode=mode, allow_zip_64=allow_zip_64)
return zipfs
......@@ -280,7 +286,7 @@ class DebugOpener(Opener):
def get_fs(cls, registry, fs_name, fs_name_params, fs_path, writeable, create):
from fs.wrapfs.debugfs import DebugFS
if fs_path:
fs, path = registry.parse(fs_path)
fs, path = registry.parse(fs_path, writeable=writeable, create=create)
return DebugFS(fs, verbose=False)
if fs_name_params == 'ram':
from fs.memoryfs import MemoryFS
......
......@@ -20,10 +20,12 @@ from fs.filelike import StringIO
def re_raise_faults(func):
"""Decorator to re-raise XML-RPC faults as proper exceptions."""
def wrapper(*args,**kwds):
def wrapper(*args,**kwds):
try:
return func(*args,**kwds)
except xmlrpclib.Fault, f:
import traceback
traceback.print_exc()
# Make sure it's in a form we can handle
bits = f.faultString.split(" ")
if bits[0] not in ["<type","<class"]:
......
......@@ -105,7 +105,7 @@ class ZipFS(FS):
raise ValueError("mode must be 'r', 'w' or 'a'")
self.zip_mode = mode
self.encoding = encoding
self.encoding = encoding
if isinstance(zip_file, basestring):
zip_file = os.path.expanduser(os.path.expandvars(zip_file))
......@@ -171,25 +171,25 @@ class ZipFS(FS):
self.zf = _ExceptionProxy()
@synchronize
def open(self, path, mode="r", **kwargs):
def open(self, path, mode="r", **kwargs):
path = normpath(relpath(path))
if 'r' in mode:
if self.zip_mode not in 'ra':
raise OperationFailedError("open file",
path=path,
msg="Zip file must be opened for reading ('r') or appending ('a')")
msg="1 Zip file must be opened for reading ('r') or appending ('a')")
try:
contents = self.zf.read(path.encode(self.encoding))
except KeyError:
raise ResourceNotFoundError(path)
return StringIO(contents)
if 'w' in mode:
if self.zip_mode not in 'wa':
if 'w' in mode:
if self.zip_mode not in 'wa':
raise OperationFailedError("open file",
path=path,
msg="Zip file must be opened for writing ('w') or appending ('a')")
msg="2 Zip file must be opened for writing ('w') or appending ('a')")
dirname, filename = pathsplit(path)
if dirname:
self.temp_fs.makedir(dirname, recursive=True, allow_recreate=True)
......@@ -211,7 +211,7 @@ class ZipFS(FS):
except KeyError:
raise ResourceNotFoundError(path)
except RuntimeError:
raise OperationFailedError("read file", path=path, msg="Zip file must be oppened with 'r' or 'a' to read")
raise OperationFailedError("read file", path=path, msg="3 Zip file must be opened with 'r' or 'a' to read")
return contents
@synchronize
......@@ -220,8 +220,7 @@ class ZipFS(FS):
self.zf.write(sys_path, filename.encode(self.encoding))
def desc(self, path):
return "%s in zip file %s" % (path, self.zip_path)
return "%s in zip file %s" % (path, self.zip_path)
def isdir(self, path):
return self._path_fs.isdir(path)
......@@ -236,7 +235,7 @@ class ZipFS(FS):
def makedir(self, dirname, recursive=False, allow_recreate=False):
dirname = normpath(dirname)
if self.zip_mode not in "wa":
raise OperationFailedError("create directory", path=dirname, msg="Zip file must be opened for writing ('w') or appending ('a')")
raise OperationFailedError("create directory", path=dirname, msg="4 Zip file must be opened for writing ('w') or appending ('a')")
if not dirname.endswith('/'):
dirname += '/'
self._add_resource(dirname)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment