From fa596b8a639c3a04dacaffb1b1432f8895dec178 Mon Sep 17 00:00:00 2001 From: rfkelly0 Date: Thu, 7 May 2009 12:44:52 +0000 Subject: got unittests passing on windows git-svn-id: http://pyfilesystem.googlecode.com/svn/branches/rfk-ideas@143 67cdc799-7952-0410-af00-57a81ceafa0f --- fs/base.py | 24 +++++++++++++++--------- fs/errors.py | 3 ++- fs/osfs.py | 7 +++++-- fs/tempfs.py | 4 ++-- fs/tests.py | 6 ++++-- fs/wrappers/xattr.py | 11 ++++++----- 6 files changed, 34 insertions(+), 21 deletions(-) diff --git a/fs/base.py b/fs/base.py index 33def75..e99fde5 100644 --- a/fs/base.py +++ b/fs/base.py @@ -10,6 +10,7 @@ start by sublcassing the base FS class. """ +import os, os.path import shutil import fnmatch import datetime @@ -583,17 +584,22 @@ class FS(object): src_syspath = self.getsyspath(src, allow_none=True) dst_syspath = self.getsyspath(dst, allow_none=True) + # Try to do an os-level rename if possible. + # Otherwise, fall back to copy-and-remove. if src_syspath is not None and dst_syspath is not None: - if not self.isfile(src): - if self.isdir(src): + if not os.path.isfile(src_syspath): + if os.path.isdir(src_syspath): raise ResourceInvalidError(src,msg="Source is not a file: %(path)s") raise FileNotFoundError(src) - if not overwrite and self.exists(dst): + if not overwrite and os.path.exists(dst): raise DestinationExistsError(dst) - shutil.move(src_syspath, dst_syspath) - else: - self.copy(src, dst, overwrite=overwrite, chunk_size=chunk_size) - self.remove(src) + try: + os.rename(src_syspath,dst_syspath) + return + except OSError: + pass + self.copy(src, dst, overwrite=overwrite, chunk_size=chunk_size) + self.remove(src) def movedir(self, src, dst, overwrite=False, ignore_errors=False, chunk_size=16384): @@ -616,9 +622,9 @@ class FS(object): if src_syspath is not None and dst_syspath is not None: try: - shutil.move(src_syspath, dst_syspath) + os.rename(src_syspath,dst_syspath) return - except WindowsError: + except OSError: pass def movefile_noerrors(src, dst, overwrite): diff --git a/fs/errors.py b/fs/errors.py index e3eb618..daa026b 100644 --- a/fs/errors.py +++ b/fs/errors.py @@ -36,11 +36,12 @@ class PathError(FSError): class OperationFailedError(FSError): """Base exception class for errors associated with a specific operation.""" - default_message = "Unable to %(opname)s: unspecified error" + default_message = "Unable to %(opname)s: unspecified error [%(errno)s - %(details)s]" def __init__(self,opname,path=None,**kwds): self.opname = opname self.path = path + self.errno = getattr(kwds.get("details",None),"errno",None) super(OperationFailedError,self).__init__(**kwds) diff --git a/fs/osfs.py b/fs/osfs.py index 4dc5edd..fae9a01 100644 --- a/fs/osfs.py +++ b/fs/osfs.py @@ -22,7 +22,7 @@ class OSFS(FS): def __init__(self, root_path, dir_mode=0700, thread_synchronize=True): FS.__init__(self, thread_synchronize=thread_synchronize) - expanded_path = makeabsolute(normpath(os.path.expanduser(os.path.expandvars(root_path)))) + expanded_path = normpath(os.path.abspath(os.path.expanduser(os.path.expandvars(root_path)))) if not os.path.exists(expanded_path): raise DirectoryNotFoundError(expanded_path, msg="Root directory does not exist: %(path)s") if not os.path.isdir(expanded_path): @@ -39,6 +39,7 @@ class OSFS(FS): return sys_path def open(self, path, mode="r", **kwargs): + mode = filter(lambda c: c in "rwabt+",mode) try: f = open(self.getsyspath(path), mode, kwargs.get("buffering", -1)) except IOError, e: @@ -66,7 +67,7 @@ class OSFS(FS): except (OSError, IOError), e: if e.errno == 2: raise ResourceNotFoundError(path) - if e.errno == 20: + if e.errno in (20,22,): raise ResourceInvalidError(path,msg="Can't list directory contents of a file: %(path)s") raise OperationFailedError("list directory", path=path, details=e, msg="Unable to get directory listing: %(path)s - (%(details)s)") return self._listdir_helper(path, paths, wildcard, full, absolute, dirs_only, files_only) @@ -86,6 +87,8 @@ class OSFS(FS): raise DestinationExistsError(path,msg="Can not create a directory that already exists (try allow_recreate=True): %(path)s") elif e.errno == 2: raise ParentDirectoryMissingError(path) + elif e.errno == 22: + raise ResourceInvalidError(path) else: raise OperationFailedError("make directory",path=path,details=e) diff --git a/fs/tempfs.py b/fs/tempfs.py index 5aa66e6..f886ada 100644 --- a/fs/tempfs.py +++ b/fs/tempfs.py @@ -9,14 +9,14 @@ class TempFS(OSFS): """Create a Filesystem in a tempory directory (with tempfile.mkdtemp), and removes it when the TempFS object is cleaned up.""" - def __init__(self, identifier=None, dir_mode=0700, thread_synchronize=True): + def __init__(self, identifier=None, temp_dir=None, dir_mode=0700, thread_synchronize=True): """Creates a temporary Filesystem identifier -- A string that is included in the name of the temporary directory, default uses "TempFS" """ - self._temp_dir = tempfile.mkdtemp(identifier or "TempFS") + self._temp_dir = tempfile.mkdtemp(identifier or "TempFS",dir=temp_dir) self._cleaned = False OSFS.__init__(self, self._temp_dir, dir_mode=dir_mode, thread_synchronize=thread_synchronize) diff --git a/fs/tests.py b/fs/tests.py index 28a5bbf..d47c07e 100644 --- a/fs/tests.py +++ b/fs/tests.py @@ -256,9 +256,11 @@ class FSTestCases: self.assert_(not check("a")) self.fs.makedir("a") + print self.fs.listdir("a") self.assertRaises(fs.DestinationExistsError,self.fs.movedir,"copy of a","a") self.fs.movedir("copy of a","a",overwrite=True) self.assert_(not check("copy of a")) + print self.fs.listdir("a") self.assert_(check("a/1.txt")) self.assert_(check("a/2.txt")) self.assert_(check("a/3.txt")) @@ -729,8 +731,8 @@ class TestS3FS(unittest.TestCase,FSTestCases): bucket = "test-s3fs.rfk.id.au" def setUp(self): - #import nose - #raise nose.SkipTest + import nose + raise nose.SkipTest self.fs = s3fs.S3FS(self.bucket,"/unittest/files") self._clear() diff --git a/fs/wrappers/xattr.py b/fs/wrappers/xattr.py index 3eb5820..e63108d 100644 --- a/fs/wrappers/xattr.py +++ b/fs/wrappers/xattr.py @@ -23,15 +23,16 @@ class SimulateXAttr(FSWrapper): def _get_attr_path(self, path): """Get the path of the file containing xattrs for the given path.""" if self.wrapped_fs.isdir(path): - return pathjoin(path, '.xattrs.') + attr_path = pathjoin(path, '.xattrs') else: dir_path, file_name = pathsplit(path) - return pathjoin(dir_path, '.xattrs.'+file_name) + attr_path = pathjoin(dir_path, '.xattrs.'+file_name) + return attr_path def _is_attr_path(self, path): """Check whether the given path references an xattrs file.""" _,name = pathsplit(path) - if name.startswith(".xattrs."): + if name.startswith(".xattrs"): return True return False @@ -46,7 +47,7 @@ class SimulateXAttr(FSWrapper): def _set_attr_dict(self, path, attrs): """Store the xattr dictionary for the given path.""" attr_path = self._get_attr_path(path) - self.wrapped_fs.setcontents(self._get_attr_path(path), pickle.dumps(attrs)) + self.wrapped_fs.setcontents(attr_path, pickle.dumps(attrs)) def setxattr(self, path, key, value): """Set an extended attribute on the given path.""" @@ -82,7 +83,7 @@ class SimulateXAttr(FSWrapper): def _encode(self,path): """Prevent requests for operations on .xattr files.""" if self._is_attr_path(path): - raise PathError(path,msg="Paths cannot contain '.xattrs.': %(path)s") + raise PathError(path,msg="Paths cannot contain '.xattrs': %(path)s") return path def _decode(self,path): -- cgit v1.2.1