class LocalPath(FSPathBase, PosixMixin):
Local path implementation offering access/modification
methods similar to os.path.
class attributes and properties:
basename: <property object (dynamically calculated value)>
ext: <property object (dynamically calculated value)>
purebasename: <property object (dynamically calculated value)>
sep: /
methods:
def atime(self):
return last access time of the path.
arguments:
return value:
<UNKNOWN>
def chdir(self):
change directory to self and return old current directory
source: path/local/local.py
|
def chdir(self): |
""" change directory to self and return old current directory """ |
old = self.__class__() |
self._callex(os.chdir, self.strpath) |
return old | |
def check(self, **kw):
check a path for existence, or query its properties
without arguments, this returns True if the path exists (on the
filesystem), False if not
with (keyword only) arguments, the object compares the value
of the argument with the value of a property with the same name
(if it has one, else it raises a TypeError)
when for example the keyword argument 'ext' is '.py', this will
return True if self.ext == '.py', False otherwise
arguments:
return value:
AnyOf(<None>, <Boolean>)
source: path/common.py
96 |
97 |
98 |
99 |
100 |
101 |
102 |
103 |
104 |
105 |
106 |
107 |
108 |
109 |
110 |
111 |
112 |
113 |
114 |
115 | |
def check(self, **kw): |
""" check a path for existence, or query its properties |
|
without arguments, this returns True if the path exists (on the |
filesystem), False if not |
|
with (keyword only) arguments, the object compares the value |
of the argument with the value of a property with the same name |
(if it has one, else it raises a TypeError) |
|
when for example the keyword argument 'ext' is '.py', this will |
return True if self.ext == '.py', False otherwise |
""" |
if kw: |
kw = kw.copy() |
if not checktype(self, kw): |
return False |
else: |
kw = {'exists' : 1} |
return self.Checkers(self)._evaluate(kw) | |
def chmod(self, mode, rec=0):
change permissions to the given mode. If mode is an
integer it directly encodes the os-specific modes.
if rec is True perform recursively.
(xxx if mode is a string then it specifies access rights
in '/bin/chmod' style, e.g. a+r).
arguments:
- self: <Instance of Class LocalPath>
- mode: <Int>
- rec: AnyOf(<Function>, <Int>)
return value:
<None>
source: path/local/posix.py
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 | |
def chmod(self, mode, rec=0): |
""" change permissions to the given mode. If mode is an |
integer it directly encodes the os-specific modes. |
if rec is True perform recursively. |
|
(xxx if mode is a string then it specifies access rights |
in '/bin/chmod' style, e.g. a+r). |
""" |
if not isinstance(mode, int): |
raise TypeError("mode %r must be an integer" % (mode,)) |
if rec: |
for x in self.visit(rec=rec): |
self._callex(os.chmod, str(x), mode) |
self._callex(os.chmod, str(self), mode) | |
def chown(self, user, group, rec=0):
change ownership to the given user and group.
user and group may be specified by a number or
by a name. if rec is True change ownership
recursively.
arguments:
- self: <Instance of Class LocalPath>
- user: <String>
- group: <String>
- rec: <Int>
return value:
<None>
source: path/local/posix.py
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 | |
def chown(self, user, group, rec=0): |
""" change ownership to the given user and group. |
user and group may be specified by a number or |
by a name. if rec is True change ownership |
recursively. |
""" |
uid = getuserid(user) |
gid = getgroupid(group) |
if rec: |
for x in self.visit(rec=lambda x: x.check(link=0)): |
if x.check(link=0): |
self._callex(os.chown, str(x), uid, gid) |
self._callex(os.chown, str(self), uid, gid) | |
def common(self, other):
return the common part shared with the other path
or None if there is no common part.
source: path/common.py
166 |
167 |
168 |
169 |
170 |
171 |
172 |
173 |
174 |
175 | |
def common(self, other): |
""" return the common part shared with the other path |
or None if there is no common part. |
""" |
last = None |
for x, y in zip(self.parts(), other.parts()): |
if x != y: |
return last |
last = x |
return last | |
def computehash(self, hashtype='md5', chunksize=524288):
return hexdigest of hashvalue for this file.
arguments:
return value:
AnyOf(<None>, <String>)
source: path/local/local.py
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 | |
def computehash(self, hashtype="md5", chunksize=524288): |
""" return hexdigest of hashvalue for this file. """ |
hash = self._gethashinstance(hashtype) |
f = self.open('rb') |
try: |
while 1: |
buf = f.read(chunksize) |
if not buf: |
return hash.hexdigest() |
hash.update(buf) |
finally: |
f.close() | |
def copy(self, target, archive=False):
copy path to target.
arguments:
return value:
<None>
source: path/local/local.py
234 |
235 |
236 |
237 |
238 |
239 |
240 |
241 |
242 |
243 |
244 |
245 |
246 |
247 |
248 |
249 |
250 |
251 |
252 |
253 |
254 | |
def copy(self, target, archive=False): |
""" copy path to target.""" |
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 dirpath(self, *args, **kwargs):
return the directory Path of the current Path joined
with any given path arguments.
source: path/common.py
|
def dirpath(self, *args, **kwargs): |
""" return the directory Path of the current Path joined |
with any given path arguments. |
""" |
return self.new(basename='').join(*args, **kwargs) | |
def dump(self, obj, bin=1):
pickle object into path location
arguments:
return value:
<None>
source: path/local/local.py
260 |
261 |
262 |
263 |
264 |
265 |
266 | |
def dump(self, obj, bin=1): |
""" pickle object into path location""" |
f = self.open('wb') |
try: |
self._callex(py.std.cPickle.dump, obj, f, bin) |
finally: |
f.close() | |
def ensure(self, *args, **kwargs):
ensure that an args-joined path exists (by default as
a file). if you specify a keyword argument 'dir=True'
then the path is forced to be a directory path.
source: path/local/local.py
299 |
300 |
301 |
302 |
303 |
304 |
305 |
306 |
307 |
308 |
309 |
310 |
311 | |
def ensure(self, *args, **kwargs): |
""" ensure that an args-joined path exists (by default as |
a file). if you specify a keyword argument 'dir=True' |
then the path is forced to be a directory path. |
""" |
p = self.join(*args) |
if kwargs.get('dir', 0): |
return p._ensuredirs() |
else: |
p.dirpath()._ensuredirs() |
if not p.check(file=1): |
p.write("") |
return p | |
def get_temproot(cls):
return the system's temporary directory
(where tempfiles are usually created in)
source: path/local/local.py
|
def get_temproot(cls): |
""" return the system's temporary directory |
(where tempfiles are usually created in) |
""" |
return py.path.local(py.std.tempfile.gettempdir()) | |
def group(self):
return group name of file.
arguments:
return value:
<String>
def join(self, *args, **kwargs):
return a new path by appending all 'args' as path
components. if abs=1 is used restart from root if any
of the args is an absolute path.
source: path/local/local.py
167 |
168 |
169 |
170 |
171 |
172 |
173 |
174 |
175 |
176 |
177 |
178 |
179 |
180 |
181 |
182 |
183 |
184 |
185 |
186 |
187 |
188 |
189 |
190 |
191 |
192 |
193 |
194 |
195 | |
def join(self, *args, **kwargs): |
""" return a new path by appending all 'args' as path |
components. if abs=1 is used restart from root if any |
of the args is an absolute path. |
""" |
if not args: |
return self |
strpath = self.strpath |
sep = self.sep |
strargs = [str(x) for x in args] |
if kwargs.get('abs', 0): |
for i in range(len(strargs)-1, -1, -1): |
if os.path.isabs(strargs[i]): |
strpath = strargs[i] |
strargs = strargs[i+1:] |
break |
for arg in strargs: |
arg = arg.strip(sep) |
if py.std.sys.platform == 'win32': |
|
arg = arg.strip('/') |
arg = arg.replace('/', sep) |
if arg: |
if not strpath.endswith(sep): |
strpath += sep |
strpath += arg |
obj = self.new() |
obj.strpath = os.path.normpath(strpath) |
return obj | |
def listdir(self, fil=None, sort=None):
list directory contents, possibly filter by the given fil func
and possibly sorted.
arguments:
- self: <Instance of Class LocalPath>
- fil: AnyOf(<None>, <String>, <Instance of Class checker>)
- sort: AnyOf(<Boolean>, <None>)
return value:
AnyOf(<None>, <List>)
source: path/local/local.py
209 |
210 |
211 |
212 |
213 |
214 |
215 |
216 |
217 |
218 |
219 |
220 |
221 |
222 |
223 |
224 | |
def listdir(self, fil=None, sort=None): |
""" list directory contents, possibly filter by the given fil func |
and possibly sorted. |
""" |
if isinstance(fil, str): |
fil = common.fnmatch(fil) |
res = [] |
for name in self._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: |
res.sort() |
return res | |
def load(self):
return object unpickled from self.read()
arguments:
return value:
<Dict>
source: path/common.py
355 |
356 |
357 |
358 |
359 |
360 |
361 |
362 | |
def load(self): |
""" return object unpickled from self.read() """ |
f = self.open('rb') |
try: |
from cPickle import load |
return self._callex(load, f) |
finally: |
f.close() | |
def lstat(self):
Return an os.lstat() tuple.
arguments:
return value:
AnyOf(<None>, <Instance of Class PosixStat>)
source: path/local/local.py
|
def lstat(self): |
""" Return an os.lstat() tuple. """ |
return self._makestat(self._callex(os.lstat, self.strpath)) | |
def make_numbered_dir(cls, prefix='session-', rootdir=None, keep=3, lock_timeout=172800):
return unique directory with a number greater than the current
maximum one. The number is assumed to start directly after prefix.
if keep is true directories with a number less than (maxnum-keep)
will be removed.
source: path/local/local.py
583 |
584 |
585 |
586 |
587 |
588 |
589 |
590 |
591 |
592 |
593 |
594 |
595 |
596 |
597 |
598 |
599 |
600 |
601 |
602 |
603 |
604 |
605 |
606 |
607 |
608 |
609 |
610 |
611 |
612 |
613 |
614 |
615 |
616 |
617 |
618 |
619 |
620 |
621 |
622 |
623 |
624 |
625 |
626 |
627 |
628 |
629 |
630 |
631 |
632 |
633 |
634 |
635 |
636 |
637 |
638 |
639 |
640 |
641 |
642 |
643 |
644 |
645 |
646 |
647 |
648 |
649 |
650 |
651 |
652 |
653 |
654 |
655 |
656 |
657 |
658 |
659 |
660 |
661 |
662 |
663 |
664 |
665 |
666 |
667 |
668 |
669 |
670 |
671 |
672 |
673 |
674 |
675 |
676 |
677 |
678 |
679 |
680 |
681 |
682 |
683 |
684 | |
def make_numbered_dir(cls, prefix='session-', rootdir=None, keep=3, |
lock_timeout = 172800): |
""" return unique directory with a number greater than the current |
maximum one. The number is assumed to start directly after prefix. |
if keep is true directories with a number less than (maxnum-keep) |
will be removed. |
""" |
if rootdir is None: |
rootdir = cls.get_temproot() |
|
def parse_num(path): |
""" parse the number out of a path (if it matches the prefix) """ |
bn = path.basename |
if bn.startswith(prefix): |
try: |
return int(bn[len(prefix):]) |
except ValueError: |
pass |
|
|
|
lastmax = None |
while True: |
maxnum = -1 |
for path in rootdir.listdir(): |
num = parse_num(path) |
if num is not None: |
maxnum = max(maxnum, num) |
|
|
try: |
udir = rootdir.mkdir(prefix + str(maxnum+1)) |
except py.error.EEXIST: |
|
|
if lastmax == maxnum: |
raise |
lastmax = maxnum |
continue |
break |
|
|
|
lockfile = udir.join('.lock') |
mypid = os.getpid() |
if hasattr(lockfile, 'mksymlinkto'): |
lockfile.mksymlinkto(str(mypid)) |
else: |
lockfile.write(str(mypid)) |
def try_remove_lockfile(): |
|
|
|
|
|
if os.getpid() != mypid: |
return |
try: |
lockfile.remove() |
except py.error.Error: |
pass |
atexit.register(try_remove_lockfile) |
|
|
if keep: |
for path in rootdir.listdir(): |
num = parse_num(path) |
if num is not None and num <= (maxnum - keep): |
lf = path.join('.lock') |
try: |
t1 = lf.lstat().mtime |
t2 = lockfile.lstat().mtime |
if abs(t2-t1) < lock_timeout: |
continue |
except py.error.Error: |
pass |
try: |
path.remove(rec=1) |
except py.error.Error: |
pass |
|
|
try: |
username = os.environ['USER'] |
except KeyError: |
try: |
username = os.environ['USERNAME'] |
except KeyError: |
username = 'current' |
|
src = str(udir) |
dest = src[:src.rfind('-')] + '-' + username |
try: |
os.unlink(dest) |
except OSError: |
pass |
try: |
os.symlink(src, dest) |
except (OSError, AttributeError): |
pass |
|
return udir | |
def mkdir(self, *args):
create & return the directory joined with args.
source: path/local/local.py
|
def mkdir(self, *args): |
""" create & return the directory joined with args. """ |
p = self.join(*args) |
self._callex(os.mkdir, str(p)) |
return p | |
def mkdtemp(cls):
return a Path object pointing to a fresh new temporary directory
(which we created ourself).
source: path/local/local.py
566 |
567 |
568 |
569 |
570 |
571 |
572 |
573 |
574 |
575 |
576 |
577 |
578 |
579 |
580 | |
def mkdtemp(cls): |
""" return a Path object pointing to a fresh new temporary directory |
(which we created ourself). |
""" |
import tempfile |
tries = 10 |
for i in range(tries): |
dname = tempfile.mktemp() |
dpath = cls(tempfile.mktemp()) |
try: |
dpath.mkdir() |
except (py.error.EEXIST, py.error.EPERM, py.error.EACCES): |
continue |
return dpath |
raise py.error.ENOENT(dpath, "could not create tempdir, %d tries" % tries) | |
def mklinkto(self, oldname):
posix style hard link to another name.
arguments:
return value:
<None>
source: path/local/posix.py
|
def mklinkto(self, oldname): |
""" posix style hard link to another name. """ |
self._callex(os.link, str(oldname), str(self)) | |
def mksymlinkto(self, value, absolute=1):
create a symbolic link with the given value (pointing to another name).
arguments:
return value:
<None>
source: path/local/posix.py
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 |
101 | |
def mksymlinkto(self, value, absolute=1): |
""" create a symbolic link with the given value (pointing to another name). """ |
if absolute: |
self._callex(os.symlink, str(value), self.strpath) |
else: |
base = self.common(value) |
|
relsource = self.__class__(value).relto(base) |
reldest = self.relto(base) |
n = reldest.count(self.sep) |
target = self.sep.join(('..', )*n + (relsource, )) |
self._callex(os.symlink, target, self.strpath) | |
def mode(self):
return permission mode of the path object
arguments:
return value:
<Int>
source: path/local/posix.py
|
def mode(self): |
""" return permission mode of the path object """ |
self._deprecated('mode') |
return self.stat().mode | |
def move(self, target):
move this path to target.
arguments:
return value:
<None>
source: path/common.py
364 |
365 |
366 |
367 |
368 |
369 |
370 |
371 |
372 | |
def move(self, target): |
""" move this path to target. """ |
if target.relto(self): |
raise py.error.EINVAL(target, "cannot move path into a subdirectory of itself") |
try: |
self.rename(target) |
except py.error.EXDEV: |
self.copy(target) |
self.remove() | |
def mtime(self):
return last modification time of the path.
arguments:
return value:
AnyOf(<None>, <Float>)
def new(self, **kw):
create a modified version of this path.
the following keyword arguments modify various path parts:
a:/some/path/to/a/file.ext
|| drive
|-------------| dirname
|------| basename
|--| purebasename
|--| ext
source: path/local/local.py
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 |
101 |
102 |
103 |
104 |
105 |
106 |
107 |
108 |
109 |
110 |
111 |
112 |
113 |
114 |
115 |
116 |
117 |
118 |
119 |
120 |
121 |
122 |
123 |
124 |
125 |
126 | |
def new(self, **kw): |
""" create a modified version of this path. |
the following keyword arguments modify various path parts: |
|
a:/some/path/to/a/file.ext |
|| drive |
|-------------| dirname |
|------| basename |
|--| purebasename |
|--| ext |
""" |
obj = object.__new__(self.__class__) |
drive, dirname, basename, purebasename,ext = self._getbyspec( |
"drive,dirname,basename,purebasename,ext") |
if 'basename' in kw: |
if 'purebasename' in kw or 'ext' in kw: |
raise ValueError("invalid specification %r" % kw) |
else: |
pb = kw.setdefault('purebasename', purebasename) |
try: |
ext = kw['ext'] |
except KeyError: |
pass |
else: |
if ext and not ext.startswith('.'): |
ext = '.' + ext |
kw['basename'] = pb + ext |
|
kw.setdefault('drive', drive) |
kw.setdefault('dirname', dirname) |
kw.setdefault('sep', self.sep) |
obj.strpath = os.path.normpath( |
"%(drive)s%(dirname)s%(sep)s%(basename)s" % kw) |
return obj | |
def open(self, mode='r'):
return an opened file with the given mode.
arguments:
return value:
AnyOf(<None>, <Instance of Class file>)
source: path/local/local.py
|
def open(self, mode='r'): |
""" return an opened file with the given mode. """ |
return self._callex(open, self.strpath, mode) | |
def owner(self):
return owner name of file.
arguments:
return value:
<String>
def parts(self, reverse=False):
return a root-first list of all ancestor directories
plus the path itself.
arguments:
return value:
<List>
source: path/common.py
150 |
151 |
152 |
153 |
154 |
155 |
156 |
157 |
158 |
159 |
160 |
161 |
162 |
163 |
164 | |
def parts(self, reverse=False): |
""" return a root-first list of all ancestor directories |
plus the path itself. |
""" |
current = self |
l = [self] |
while 1: |
last = current |
current = current.dirpath() |
if last == current: |
break |
l.insert(0, current) |
if reverse: |
l.reverse() |
return l | |
def pyimport(self, modname=None, ensuresyspath=True):
return path as an imported python module.
if modname is None, look for the containing package
and construct an according module name.
The module will be put/looked up in sys.modules.
arguments:
- self: <Instance of Class LocalPath>
- modname: AnyOf(<String>, <None>)
- ensuresyspath: <Boolean>
return value:
AnyOf(<None>, <Module>)
source: path/local/local.py
383 |
384 |
385 |
386 |
387 |
388 |
389 |
390 |
391 |
392 |
393 |
394 |
395 |
396 |
397 |
398 |
399 |
400 |
401 |
402 |
403 |
404 |
405 |
406 |
407 |
408 |
409 |
410 |
411 |
412 |
413 |
414 |
415 |
416 |
417 |
418 |
419 |
420 |
421 |
422 |
423 |
424 |
425 |
426 |
427 |
428 |
429 |
430 |
431 |
432 |
433 |
434 | |
def pyimport(self, modname=None, ensuresyspath=True): |
""" return path as an imported python module. |
if modname is None, look for the containing package |
and construct an according module name. |
The module will be put/looked up in sys.modules. |
""" |
if not self.check(): |
raise py.error.ENOENT(self) |
|
pkgpath = None |
if modname is None: |
|
|
|
|
pkgpath = self.pypkgpath() |
if pkgpath is not None: |
if ensuresyspath: |
self._prependsyspath(pkgpath.dirpath()) |
pkg = __import__(pkgpath.basename, None, None, []) |
|
if hasattr(pkg, '__pkg__'): |
modname = pkg.__pkg__.getimportname(self) |
assert modname is not None, "package %s doesn't know %s" % ( |
pkg.__name__, self) |
|
else: |
names = self.new(ext='').relto(pkgpath.dirpath()) |
names = names.split(self.sep) |
modname = ".".join(names) |
else: |
|
if ensuresyspath: |
self._prependsyspath(self.dirpath()) |
modname = self.purebasename |
mod = __import__(modname, None, None, ['__doc__']) |
|
return mod |
else: |
try: |
return sys.modules[modname] |
except KeyError: |
|
mod = py.std.new.module(modname) |
mod.__file__ = str(self) |
sys.modules[modname] = mod |
try: |
execfile(str(self), mod.__dict__) |
except: |
del sys.modules[modname] |
raise |
return mod | |
def pypkgpath(self, pkgname=None):
return the path's package path by looking for the given
pkgname. If pkgname is None then look for the last
directory upwards which still contains an __init__.py.
Return None if a pkgpath can not be determined.
source: path/local/local.py
357 |
358 |
359 |
360 |
361 |
362 |
363 |
364 |
365 |
366 |
367 |
368 |
369 |
370 |
371 |
372 |
373 |
374 |
375 | |
def pypkgpath(self, pkgname=None): |
""" return the path's package path by looking for the given |
pkgname. If pkgname is None then look for the last |
directory upwards which still contains an __init__.py. |
Return None if a pkgpath can not be determined. |
""" |
pkgpath = None |
for parent in self.parts(reverse=True): |
if pkgname is None: |
if parent.check(file=1): |
continue |
if parent.join('__init__.py').check(): |
pkgpath = parent |
continue |
return pkgpath |
else: |
if parent.basename == pkgname: |
return parent |
return pkgpath | |
def read(self, mode='rb'):
read and return a bytestring from reading the path.
arguments:
return value:
AnyOf(<None>, <String>)
source: path/common.py
330 |
331 |
332 |
333 |
334 |
335 |
336 |
337 |
338 |
339 |
340 | |
def read(self, mode='rb'): |
""" read and return a bytestring from reading the path. """ |
if py.std.sys.version_info < (2,3): |
for x in 'u', 'U': |
if x in mode: |
mode = mode.replace(x, '') |
f = self.open(mode) |
try: |
return f.read() |
finally: |
f.close() | |
def readlines(self, cr=1):
read and return a list of lines from the path. if cr is False, the
newline will be removed from the end of each line.
arguments:
return value:
<List>
source: path/common.py
342 |
343 |
344 |
345 |
346 |
347 |
348 |
349 |
350 |
351 |
352 |
353 | |
def readlines(self, cr=1): |
""" read and return a list of lines from the path. if cr is False, the |
newline will be removed from the end of each line. """ |
if not cr: |
content = self.read('rU') |
return content.split('\n') |
else: |
f = self.open('rU') |
try: |
return f.readlines() |
finally: |
f.close() | |
def readlink(self):
return value of a symbolic link.
arguments:
return value:
<String>
source: path/local/posix.py
|
def readlink(self): |
""" return value of a symbolic link. """ |
return self._callex(os.readlink, self.strpath) | |
def realpath(self):
return a new path which contains no symbolic links.
source: path/local/local.py
|
def realpath(self): |
""" return a new path which contains no symbolic links.""" |
return self.__class__(os.path.realpath(self.strpath)) | |
def relto(self, relpath):
return a string which is the relative part of the path
to the given 'relpath'.
arguments:
return value:
AnyOf(<None>, <String>)
source: path/common.py
134 |
135 |
136 |
137 |
138 |
139 |
140 |
141 |
142 |
143 |
144 |
145 |
146 |
147 |
148 | |
def relto(self, relpath): |
""" return a string which is the relative part of the path |
to the given 'relpath'. |
""" |
if not isinstance(relpath, (str, PathBase)): |
raise TypeError("%r: not a string or path object" %(relpath,)) |
strrelpath = str(relpath) |
if strrelpath and strrelpath[-1] != self.sep: |
strrelpath += self.sep |
|
|
strself = str(self) |
if strself.startswith(strrelpath): |
return strself[len(strrelpath):] |
return "" | |
def remove(self, rec=1):
remove a file or directory (or a directory tree if rec=1).
arguments:
return value:
<None>
source: path/local/posix.py
104 |
105 |
106 |
107 |
108 |
109 |
110 |
111 |
112 | |
def remove(self, rec=1): |
""" remove a file or directory (or a directory tree if rec=1). """ |
if self.check(dir=1, link=0): |
if rec: |
self._callex(py.std.shutil.rmtree, self.strpath) |
else: |
self._callex(os.rmdir, self.strpath) |
else: |
self._callex(os.remove, self.strpath) | |
def rename(self, target):
rename this path to target.
arguments:
return value:
<None>
source: path/local/local.py
|
def rename(self, target): |
""" rename this path to target. """ |
return self._callex(os.rename, str(self), str(target)) | |
def setmtime(self, mtime=None):
set modification time for the given path. if 'mtime' is None
(the default) then the file's mtime is set to current time.
Note that the resolution for 'mtime' is platform dependent.
arguments:
return value:
<None>
source: path/local/local.py
323 |
324 |
325 |
326 |
327 |
328 |
329 |
330 |
331 |
332 |
333 |
334 | |
def setmtime(self, mtime=None): |
""" set modification time for the given path. if 'mtime' is None |
(the default) then the file's mtime is set to current time. |
|
Note that the resolution for 'mtime' is platform dependent. |
""" |
if mtime is None: |
return self._callex(os.utime, self.strpath, mtime) |
try: |
return self._callex(os.utime, self.strpath, (-1, mtime)) |
except py.error.EINVAL: |
return self._callex(os.utime, self.strpath, (self.atime(), mtime)) | |
def size(self):
return size of the underlying file object
arguments:
return value:
<Int>
def stat(self):
Return an os.stat() tuple.
arguments:
return value:
AnyOf(<None>, <Instance of Class PosixStat>)
source: path/local/local.py
|
def stat(self): |
""" Return an os.stat() tuple. """ |
stat = self._callex(os.stat, self.strpath) |
return self._makestat(stat) | |
def sysexec(self, *argv):
return stdout-put from executing a system child process,
where the self path points to the binary (XXX or script)
to be executed. Note that this process is directly
invoked and not through a system shell.
arguments:
return value:
AnyOf(<None>, <String>)
source: path/local/local.py
491 |
492 |
493 |
494 |
495 |
496 |
497 |
498 |
499 |
500 |
501 |
502 |
503 |
504 |
505 | |
def sysexec(self, *argv): |
""" return stdout-put from executing a system child process, |
where the self path points to the binary (XXX or script) |
to be executed. Note that this process is directly |
invoked and not through a system shell. |
""" |
from py.compat.subprocess import Popen, PIPE |
argv = map(str, argv) |
proc = Popen([str(self)] + list(argv), stdout=PIPE, stderr=PIPE) |
stdout, stderr = proc.communicate() |
ret = proc.wait() |
if ret != 0: |
raise py.process.cmdexec.Error(ret, ret, str(self), |
stdout, stderr,) |
return stdout | |
def sysfind(cls, name, checker=None):
return a path object found by looking at the systems
underlying PATH specification. If the checker is not None
it will be invoked to filter matching paths. If a binary
cannot be found, None is returned
Note: This is probably not working on plain win32 systems
but may work on cygwin.
source: path/local/local.py
507 |
508 |
509 |
510 |
511 |
512 |
513 |
514 |
515 |
516 |
517 |
518 |
519 |
520 |
521 |
522 |
523 |
524 |
525 |
526 |
527 |
528 |
529 |
530 |
531 |
532 |
533 |
534 |
535 |
536 |
537 |
538 |
539 |
540 |
541 |
542 |
543 |
544 |
545 | |
def sysfind(cls, name, checker=None): |
""" return a path object found by looking at the systems |
underlying PATH specification. If the checker is not None |
it will be invoked to filter matching paths. If a binary |
cannot be found, None is returned |
Note: This is probably not working on plain win32 systems |
but may work on cygwin. |
""" |
if os.path.isabs(name): |
p = py.path.local(name) |
if p.check(file=1): |
return p |
else: |
if py.std.sys.platform == 'win32': |
paths = py.std.os.environ['Path'].split(';') |
try: |
systemroot = os.environ['SYSTEMROOT'] |
except KeyError: |
pass |
else: |
paths = [re.sub('%SystemRoot%', systemroot, path) |
for path in paths] |
tryadd = '', '.exe', '.com', '.bat' |
else: |
paths = py.std.os.environ['PATH'].split(':') |
tryadd = ('',) |
|
for x in paths: |
for addext in tryadd: |
p = py.path.local(x).join(name, abs=True) + addext |
try: |
if p.check(file=1): |
if checker: |
if not checker(p): |
continue |
return p |
except py.error.EACCES: |
pass |
return None | |
def visit(self, fil=None, rec=None, ignore=<class py.__.path.common._dummyclass at 0x200003cead0>):
yields all paths below the current one
fil is a filter (glob pattern or callable), if not matching the
path will not be yielded, defaulting to None (everything is
returned)
rec is a filter (glob pattern or callable) that controls whether
a node is descended, defaulting to None
ignore is an Exception class that is ignoredwhen calling dirlist()
on any of the paths (by default, all exceptions are reported)
arguments:
- self: <Instance of Class LocalPath>
- fil: AnyOf(<String>, <Instance of Class fnmatch>, <Method>, <None>, <Instance of Class checker>, <Function>)
- rec: AnyOf(<Instance of Class checker>, <None>, <Method>, <Function>)
- ignore: AnyOf(Class ENOENT, Class _dummyclass)
return value:
AnyOf(<None>, <Instance of
Class LocalPath>)
source: path/common.py
192 |
193 |
194 |
195 |
196 |
197 |
198 |
199 |
200 |
201 |
202 |
203 |
204 |
205 |
206 |
207 |
208 |
209 |
210 |
211 |
212 |
213 |
214 |
215 |
216 |
217 |
218 |
219 |
220 |
221 |
222 |
223 | |
def visit(self, fil=None, rec=None, ignore=_dummyclass): |
""" yields all paths below the current one |
|
fil is a filter (glob pattern or callable), if not matching the |
path will not be yielded, defaulting to None (everything is |
returned) |
|
rec is a filter (glob pattern or callable) that controls whether |
a node is descended, defaulting to None |
|
ignore is an Exception class that is ignoredwhen calling dirlist() |
on any of the paths (by default, all exceptions are reported) |
""" |
if isinstance(fil, str): |
fil = fnmatch(fil) |
if rec: |
if isinstance(rec, str): |
rec = fnmatch(fil) |
elif not callable(rec): |
rec = lambda x: True |
reclist = [self] |
while reclist: |
current = reclist.pop(0) |
try: |
dirlist = current.listdir() |
except ignore: |
return |
for p in dirlist: |
if fil is None or fil(p): |
yield p |
if p.check(dir=1) and (rec is None or rec(p)): |
reclist.append(p) | |
def write(self, content, mode='wb'):
write string content into path.
arguments:
return value:
<None>
source: path/local/local.py
274 |
275 |
276 |
277 |
278 |
279 |
280 |
281 | |
def write(self, content, mode='wb'): |
""" write string content into path. """ |
s = str(content) |
f = self.open(mode) |
try: |
f.write(s) |
finally: |
f.close() | |
def __add__(self, other):
return new path object with 'other' added to the basename
source: path/common.py
|
def __add__(self, other): |
""" return new path object with 'other' added to the basename""" |
return self.new(basename=self.basename+str(other)) | |
def __cmp__(self, other):
return sort value (-1, 0, +1).
arguments:
return value:
<Int>
source: path/common.py
|
def __cmp__(self, other): |
""" return sort value (-1, 0, +1). """ |
try: |
return cmp(self.strpath, other.strpath) |
except AttributeError: |
return cmp(str(self), str(other)) | |
def __contains__(self, other):
*no docstring available*
arguments:
return value:
<Boolean>
source: path/common.py
121 |
122 |
123 |
124 |
125 |
126 |
127 |
128 | |
def __contains__(self, other): |
if isinstance(other, str): |
return self.join(other).check() |
else: |
if other.dirpath() != self: |
return False |
p = self.join(other.basename) |
return p.check() | |
def __div__(self, other):
def __eq__(self, other):
*no docstring available*
arguments:
return value:
<Boolean>
source: path/local/local.py
197 |
198 |
199 |
200 |
201 |
202 |
203 | |
def __eq__(self, other): |
s1 = str(self) |
s2 = str(other) |
if iswin32: |
s1 = s1.lower() |
s2 = s2.lower() |
return s1 == s2 | |
def __hash__(self):
*no docstring available*
arguments:
return value:
<Int>
def __iter__(self):
*no docstring available*
arguments:
return value:
<UNKNOWN>
def __repr__(self):
*no docstring available*
arguments:
return value:
<String>
def __str__(self):
return string representation of the Path.
arguments:
return value:
<String>