Commit f1f224c1 by willmcgugan

Fixes for zip opener

parent 40c775ea
...@@ -29,7 +29,7 @@ class FileOpThread(threading.Thread): ...@@ -29,7 +29,7 @@ class FileOpThread(threading.Thread):
except queue.Empty: except queue.Empty:
continue continue
try: try:
if path_type == FSCopy.DIR: if path_type == FScp.DIR:
self.dest_fs.makedir(path, recursive=True, allow_recreate=True) self.dest_fs.makedir(path, recursive=True, allow_recreate=True)
else: else:
self.action(fs, path, self.dest_fs, dest_path, overwrite=True) self.action(fs, path, self.dest_fs, dest_path, overwrite=True)
...@@ -43,7 +43,7 @@ class FileOpThread(threading.Thread): ...@@ -43,7 +43,7 @@ class FileOpThread(threading.Thread):
except Exception, e: except Exception, e:
self.on_error(e) self.on_error(e)
class FSCopy(Command): class FScp(Command):
DIR, FILE = 0, 1 DIR, FILE = 0, 1
...@@ -54,7 +54,7 @@ Copy SOURCE to DESTINATION""" ...@@ -54,7 +54,7 @@ Copy SOURCE to DESTINATION"""
return copyfile return copyfile
def get_optparse(self): 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, optparse.add_option('-p', '--progress', dest='progress', action="store_true", default=False,
help="show progress", metavar="PROGRESS") help="show progress", metavar="PROGRESS")
optparse.add_option('-t', '--threads', dest='threads', action="store", default=1, optparse.add_option('-t', '--threads', dest='threads', action="store", default=1,
...@@ -101,6 +101,7 @@ Copy SOURCE to DESTINATION""" ...@@ -101,6 +101,7 @@ Copy SOURCE to DESTINATION"""
self.root_dirs.append((src_fs, src_path)) self.root_dirs.append((src_fs, src_path))
src_sub_fs = src_fs.opendir(src_path) src_sub_fs = src_fs.opendir(src_path)
for dir_path, file_paths in src_sub_fs.walk(): 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)) copy_fs_paths.append((self.DIR, src_sub_fs, dir_path, dir_path))
sub_fs = src_sub_fs.opendir(dir_path) sub_fs = src_sub_fs.opendir(dir_path)
for file_path in file_paths: for file_path in file_paths:
...@@ -112,7 +113,6 @@ Copy SOURCE to DESTINATION""" ...@@ -112,7 +113,6 @@ Copy SOURCE to DESTINATION"""
self.error('%s is not a file or directory\n' % src_path) self.error('%s is not a file or directory\n' % src_path)
return 1 return 1
if self.options.threads > 1: if self.options.threads > 1:
copy_fs_dirs = [r for r in copy_fs_paths if r[0] == self.DIR] 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] copy_fs_paths = [r for r in copy_fs_paths if r[0] == self.FILE]
...@@ -151,7 +151,6 @@ Copy SOURCE to DESTINATION""" ...@@ -151,7 +151,6 @@ Copy SOURCE to DESTINATION"""
#file_queue.join() #file_queue.join()
except KeyboardInterrupt: except KeyboardInterrupt:
print "!"
options.progress = False options.progress = False
if self.action_error: if self.action_error:
self.error(self.wrap_error(unicode(self.action_error)) + '\n') self.error(self.wrap_error(unicode(self.action_error)) + '\n')
...@@ -229,7 +228,7 @@ Copy SOURCE to DESTINATION""" ...@@ -229,7 +228,7 @@ Copy SOURCE to DESTINATION"""
return bar return bar
def run(): def run():
return FSCopy().run() return FScp().run()
if __name__ == "__main__": if __name__ == "__main__":
sys.exit(run()) sys.exit(run())
...@@ -227,13 +227,15 @@ class Command(object): ...@@ -227,13 +227,15 @@ class Command(object):
if self.is_terminal(): if self.is_terminal():
self.output("\n") self.output("\n")
return 0 return 0
except ValueError:
pass
except SystemExit: except SystemExit:
return 0 return 0
#except IOError: except IOError:
# return 1 return 1
#except Exception, e: except Exception, e:
# self.error(self.wrap_error('Internal Error - %s\n' % unicode(e))) self.error(self.wrap_error('Internal Error - %s\n' % unicode(e)))
# return 1 return 1
......
...@@ -158,8 +158,8 @@ class ZipOpener(Opener): ...@@ -158,8 +158,8 @@ class ZipOpener(Opener):
@classmethod @classmethod
def get_fs(cls, registry, fs_name, fs_name_params, fs_path, writeable, create): 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 zip_file = None
if fs_path.startswith('['): if fs_path.startswith('['):
...@@ -168,7 +168,10 @@ class ZipOpener(Opener): ...@@ -168,7 +168,10 @@ class ZipOpener(Opener):
raise OpenerError("Not a file") raise OpenerError("Not a file")
container_mode = 'r+b' container_mode = 'r+b'
if create_zip: if create_zip:
container_mode = 'w+' container_mode = 'w+b'
elif writeable:
container_mode = 'w+b'
zip_file = container_fs.open(container_path, mode=container_mode) zip_file = container_fs.open(container_path, mode=container_mode)
username, password, fs_path = registry.parse_credentials(fs_path) username, password, fs_path = registry.parse_credentials(fs_path)
...@@ -177,18 +180,21 @@ class ZipOpener(Opener): ...@@ -177,18 +180,21 @@ class ZipOpener(Opener):
if zip_file is None: if zip_file is None:
zip_file = fs_path zip_file = fs_path
if create_zip: if append_zip:
mode = 'a'
elif create_zip or create:
mode = 'w' mode = 'w'
else: else:
if writeable: if writeable:
mode = 'a' mode = 'w'
else: else:
mode = 'r' mode = 'a'
if fs_name == 'zip64': if fs_name == 'zip64':
allow_zip_64 = True allow_zip_64 = True
else: else:
allow_zip_64 = False allow_zip_64 = False
zipfs = ZipFS(zip_file, mode=mode, allow_zip_64=allow_zip_64) zipfs = ZipFS(zip_file, mode=mode, allow_zip_64=allow_zip_64)
return zipfs return zipfs
...@@ -280,7 +286,7 @@ class DebugOpener(Opener): ...@@ -280,7 +286,7 @@ class DebugOpener(Opener):
def get_fs(cls, registry, fs_name, fs_name_params, fs_path, writeable, create): def get_fs(cls, registry, fs_name, fs_name_params, fs_path, writeable, create):
from fs.wrapfs.debugfs import DebugFS from fs.wrapfs.debugfs import DebugFS
if fs_path: if fs_path:
fs, path = registry.parse(fs_path) fs, path = registry.parse(fs_path, writeable=writeable, create=create)
return DebugFS(fs, verbose=False) return DebugFS(fs, verbose=False)
if fs_name_params == 'ram': if fs_name_params == 'ram':
from fs.memoryfs import MemoryFS from fs.memoryfs import MemoryFS
......
...@@ -24,6 +24,8 @@ def re_raise_faults(func): ...@@ -24,6 +24,8 @@ def re_raise_faults(func):
try: try:
return func(*args,**kwds) return func(*args,**kwds)
except xmlrpclib.Fault, f: except xmlrpclib.Fault, f:
import traceback
traceback.print_exc()
# Make sure it's in a form we can handle # Make sure it's in a form we can handle
bits = f.faultString.split(" ") bits = f.faultString.split(" ")
if bits[0] not in ["<type","<class"]: if bits[0] not in ["<type","<class"]:
......
...@@ -178,7 +178,7 @@ class ZipFS(FS): ...@@ -178,7 +178,7 @@ class ZipFS(FS):
if self.zip_mode not in 'ra': if self.zip_mode not in 'ra':
raise OperationFailedError("open file", raise OperationFailedError("open file",
path=path, 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: try:
contents = self.zf.read(path.encode(self.encoding)) contents = self.zf.read(path.encode(self.encoding))
except KeyError: except KeyError:
...@@ -189,7 +189,7 @@ class ZipFS(FS): ...@@ -189,7 +189,7 @@ class ZipFS(FS):
if self.zip_mode not in 'wa': if self.zip_mode not in 'wa':
raise OperationFailedError("open file", raise OperationFailedError("open file",
path=path, 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) dirname, filename = pathsplit(path)
if dirname: if dirname:
self.temp_fs.makedir(dirname, recursive=True, allow_recreate=True) self.temp_fs.makedir(dirname, recursive=True, allow_recreate=True)
...@@ -211,7 +211,7 @@ class ZipFS(FS): ...@@ -211,7 +211,7 @@ class ZipFS(FS):
except KeyError: except KeyError:
raise ResourceNotFoundError(path) raise ResourceNotFoundError(path)
except RuntimeError: 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 return contents
@synchronize @synchronize
...@@ -222,7 +222,6 @@ class ZipFS(FS): ...@@ -222,7 +222,6 @@ class ZipFS(FS):
def desc(self, path): 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): def isdir(self, path):
return self._path_fs.isdir(path) return self._path_fs.isdir(path)
...@@ -236,7 +235,7 @@ class ZipFS(FS): ...@@ -236,7 +235,7 @@ class ZipFS(FS):
def makedir(self, dirname, recursive=False, allow_recreate=False): def makedir(self, dirname, recursive=False, allow_recreate=False):
dirname = normpath(dirname) dirname = normpath(dirname)
if self.zip_mode not in "wa": 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('/'): if not dirname.endswith('/'):
dirname += '/' dirname += '/'
self._add_resource(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