[py-svn] r7072 - py/dist/py/path/local
hpk at codespeak.net
hpk at codespeak.net
Wed Oct 20 16:39:47 CEST 2004
Author: hpk
Date: Wed Oct 20 16:39:47 2004
New Revision: 7072
Modified:
py/dist/py/path/local/local.py
Log:
- cleanup of the invocation of the exception-conversion method
(you get a py.path.NotFound/Denied/NoDirectory/Invalid etc.pp
exception instead of funny OS or IOErrors.) Further cleanup
is probably needed especially with respect to the other
fs-implementations. It will be slightly tricky though
for the svn-command-based paths because we will have to
parse the cmdline-output.
Modified: py/dist/py/path/local/local.py
==============================================================================
--- py/dist/py/path/local/local.py (original)
+++ py/dist/py/path/local/local.py Wed Oct 20 16:39:47 2004
@@ -73,9 +73,6 @@
def __hash__(self):
return hash(self.strpath)
- def _except(self, excinfo):
- return error.error_enhance(excinfo)
-
def new(self, **kw):
""" create a modified version of this path.
the following keyword arguments modify various path parts:
@@ -156,8 +153,8 @@
def join(self, *args, **kwargs):
""" return a new path by appending all 'args' as path
- components. if abs=1 is used start from root if any if the args
- is an absolute path.
+ components. if abs=1 is used restart from root if any
+ of the args is an absolute path.
"""
if not args:
return self
@@ -183,10 +180,7 @@
def open(self, mode='r'):
""" return an opened file with the given mode. """
- try:
- return open(self.strpath, mode)
- except:
- self._except(sys.exc_info())
+ return callex(open, self.strpath, mode)
def listdir(self, fil=None, sort=None):
""" list directory contents, possibly filter by the given fil func
@@ -195,13 +189,10 @@
if isinstance(fil, str):
fil = common.fnmatch(fil)
res = []
- try:
- for name in os.listdir(self.strpath):
- childurl = self.join(name)
- if fil is None or fil(childurl):
- res.append(childurl)
- except:
- self._except(sys.exc_info())
+ for name in callex(os.listdir, self.strpath):
+ childurl = self.join(name)
+ if fil is None or fil(childurl):
+ res.append(childurl)
if callable(sort):
res.sort(sort)
elif sort:
@@ -227,72 +218,51 @@
def remove(self, rec=1):
""" remove a file or directory (or a directory tree if rec=1). """
- try:
- if self.check(dir=1, link=0):
- if rec:
- import shutil
- #def call(*args):
- # print args
-
- shutil.rmtree(self.strpath)
- #, onerror=call)
- else:
- os.rmdir(self.strpath)
+ if self.check(dir=1, link=0):
+ if rec:
+ callex(py.std.shutil.rmtree, self.strpath)
else:
- os.remove(self.strpath)
- except:
- self._except(sys.exc_info())
+ callex(os.rmdir, self.strpath)
+ else:
+ callex(os.remove, self.strpath)
def copy(self, target, archive=False):
- try:
- assert not archive, "XXX archive-mode not supported"
- if self.check(file=1):
- if target.check(dir=1):
- target = target.join(self.basename)
- assert self!=target
- copychunked(self, target)
- else:
- target.ensure(dir=1)
- def rec(p):
- return p.check(link=0)
- for x in self.visit(rec=rec):
- relpath = x.relto(self)
- newx = target.join(relpath)
- if x.check(link=1):
- newx.mksymlinkto(x.readlink())
- elif x.check(file=1):
- copychunked(x, newx)
- elif x.check(dir=1):
- newx.ensure(dir=1)
- except:
- self._except(sys.exc_info())
+ assert not archive, "XXX archive-mode not supported"
+ if self.check(file=1):
+ if target.check(dir=1):
+ target = target.join(self.basename)
+ assert self!=target
+ copychunked(self, target)
+ else:
+ target.ensure(dir=1)
+ def rec(p):
+ return p.check(link=0)
+ for x in self.visit(rec=rec):
+ relpath = x.relto(self)
+ newx = target.join(relpath)
+ if x.check(link=1):
+ newx.mksymlinkto(x.readlink())
+ elif x.check(file=1):
+ copychunked(x, newx)
+ elif x.check(dir=1):
+ newx.ensure(dir=1)
def rename(self, target):
- try:
- os.rename(str(self), str(target))
- except:
- self._except(sys.exc_info())
+ return callex(os.rename, str(self), str(target))
def dumpobj(self, obj):
""" pickle object into path location"""
+ f = self.open('wb')
try:
- f = self.open('wb')
- try:
- from cPickle import dump
- dump(obj, f)
- finally:
- f.close()
- except:
- self._except(sys.exc_info())
+ callex(py.std.cPickle.dump, obj, f)
+ finally:
+ f.close()
def mkdir(self, *args):
""" create & return the directory joined with args. """
p = self.join(*args)
- try:
- os.mkdir(str(p))
- except:
- self._except(sys.exc_info())
- return p
+ callex(os.mkdir, str(p))
+ return p
def write(self, content):
""" write string content into path. """
@@ -317,30 +287,21 @@
a file). if you specify a keyword argument 'dir=True'
then the path is forced to be a directory path.
"""
- try:
- p = self.join(*args)
- if kwargs.get('dir', 0):
- return p._ensuredirs()
- parent = p.dirpath()
- parent._ensuredirs()
+ p = self.join(*args)
+ if kwargs.get('dir', 0):
+ return p._ensuredirs()
+ else:
+ p.dirpath()._ensuredirs()
p.write("")
return p
- except:
- self._except(sys.exc_info())
def stat(self):
""" Return an os.stat() tuple. """
- try:
- return os.stat(self.strpath)
- except:
- self._except(sys.exc_info())
+ return callex(os.stat, self.strpath)
def lstat(self):
""" Return an os.lstat() tuple. """
- try:
- return os.lstat(self.strpath)
- except:
- self._except(sys.exc_info())
+ return callex(os.lstat, self.strpath)
# xlocal implementation
def setmtime(self, mtime=None):
@@ -350,19 +311,19 @@
Note that the resolution for 'mtime' is platform dependent.
"""
if mtime is None:
- return os.utime(self.strpath, mtime)
+ return callex(os.utime, self.strpath, mtime)
try:
return os.utime(self.strpath, (-1, mtime))
except OSError, e:
if e.errno != 22:
- self._except(sys.exc_info())
+ error.error_enhance(sys.exc_info())
raise
- return os.utime(self.strpath, (self.atime(), mtime))
+ return callex(os.utime, self.strpath, (self.atime(), mtime))
def chdir(self):
""" change directory to self and return old current directory """
old = self.__class__()
- os.chdir(self.strpath)
+ callex(os.chdir, self.strpath)
return old
def realpath(self):
@@ -542,3 +503,12 @@
fdest.close()
finally:
fsrc.close()
+
+def callex(func, *args, **kwargs):
+ try:
+ return func(*args, **kwargs)
+ except (KeyboardInterrupt, SystemExit):
+ raise
+ except:
+ error.error_enhance(sys.exc_info() )
+
More information about the pytest-commit
mailing list