This repository has been archived by the owner on Mar 4, 2021. It is now read-only.
/
_base.py
342 lines (273 loc) · 10.7 KB
/
_base.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
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
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
import os
import stat
import errno
class InsecurePathError(Exception):
"""
The path operation is unsafe to perform.
An insecure operation was requested, for example:
* a join is performed with an absolute path as input parameter
* '..' is passed as a parameter to child method
* Symlinks not passing security validations
"""
pass
class CrossDeviceRenameError(Exception):
"""
Rename old and new paths are not on the same filesystem.
Rename cannot work across different python filesystems backends.
New path is not on the local filesystem.
"""
pass
## utility function for checking for safe file names.
## TODO: should it be encapsulated within a class?
def raise_on_insecure_file_name(name):
if u'/' in name:
raise InsecurePathError(
'child name contains directory separator')
# this may be too naive
if name == u'..':
raise InsecurePathError(
'child trying to climb out of directory')
## TODO: RFC: Is there any presedence for this naming convention? As
## I understand it, "Mixin" means that this class can be mixed into
## the parent class list in a class definition to get misc methods
## mixed in - but I find this naming convention mostly confusing. I
## think "...Helper" is clearer? 2008-07-12
class StatWrappersMixin(object):
"""
If a class implements stat(), several other methods can be
implemented as wrappers around stat. Simply pull in this class to
get those implemented: exists, isdir, isfile, islink, size. (islink
depends on lstat being implemented - assumes the file system does
not support symlinks and hence has no symlinks if lstat is not
implemented)
"""
def size(self):
"""
Return the size of the item represented by this path.
If the path cannot be accessed, raise an ``OSError``.
"""
return self.stat().st_size
def exists(self):
"""
Return ``True`` if this path actually exists in the concrete
filesystem, else return ``False``.
If there is an error, raise ``OSError``. Note that the
non-existence of the path isn't an error, but simply returns
``False``.
"""
try:
self.stat()
except OSError, e:
# ENOENT means the path wasn't found
if e.errno == errno.ENOENT:
return False
else:
raise
return True
def isdir(self):
"""
Return ``True`` if the path corresponds to a directory or a
symlink to one, else return ``False``.
Raise an ``OSError`` if the operation fails.
"""
return stat.S_ISDIR(self.stat().st_mode)
def isfile(self):
"""
Return ``True`` if the path corresponds to a file or a symlink
to it, else return ``False``.
Raise an ``OSError`` if the operation fails.
"""
return stat.S_ISREG(self.stat().st_mode)
def islink(self):
"""
Return ``True`` if the path corresponds to a symlink, else
return ``False``.
Raise an ``OSError`` if the operation fails.
"""
return (hasattr(self, 'lstat') and
stat.S_ISLNK(self.lstat().st_mode))
class WalkMixin(object):
"""
This class gives the method walk. If there is no
filesystem-specific way to do a walk, and if your file system
class implements iteration, parent, isdir and islink, it may
inheritate this class.
"""
def walk(self, topdown=True):
"""Directory tree generator.
For each directory in the directory tree rooted at top
(including top itself), yields a 3-tuple
(directory, subdirectories, nondirs)
When topdown is true, the caller can modify the subdirectories
list in-place (e.g., via del or slice assignment), and walk
will only recurse into the remaining subdirectories. This can
be used to prune the search, or to impose a specific order of
visiting.
TODO: os.walk can handle errors through callbacks, not to
interrupt the whole walk on errors. We should probably do the
same.
TODO: we should have a follow_symlinks flag, but then we also
need loop control.
"""
children = list(self)
## TODO: optimize?
subdirs = [c for c in children if c.isdir()]
nondirs = [c for c in children if not c.isdir()]
if topdown:
yield (self, subdirs, nondirs)
for d in subdirs:
if (d.parent() != self or d == self):
raise InsecurePathError("walk is only allowed into subdirs")
if not (hasattr(d, 'islink') and d.islink()):
for w in d.walk(topdown):
yield w
if not topdown:
yield (self, subdirs, nondirs)
class PathnameMixin(object):
"""
This class asserts self._pathname exists
Class for dealing with pathnames, to be subclassed by file system
implementations where such path name handling is useful.
This class implements comparition operators, it allows pathname
building through the join and child methods, and one can move
upwards in the hierarchy by using the parent method.
No methods in this class will perform any IO-operations, though
the class depends on the os-module to actually do some of the
work.
"""
def __init__(self, pathname):
self._pathname = pathname
def __str__(self):
return str(self._pathname)
def __unicode__(self):
return unicode(self._pathname)
def __repr__(self):
return '%s(%r)' % (self.__class__.__name__, self._pathname)
def join(self, relpath):
"""
Return a new ``path`` object, with the relative path
``relpath`` joined on it.
For example,
p = filesystem.path(u"/some/path").join(u"some_more")
The appended path has to be a relative path. Otherwise, an
``InsecurePathError`` is raised.
"""
if relpath.startswith(u'/'):
raise InsecurePathError('path name to join must be relative')
return self.__class__(os.path.join(self._pathname, relpath))
def child(self, *segments):
"""
Return a child of the ``path`` object. (A child is a path
representing a path one or more levels "deeper" than the
original path, on which this method is called.)
This method takes one or more positional arguments,
``segments``, each representing an item further below the
path.
For example:
>>> path = filesystem.path(u"/usr/share")
>>> child_path = path.child(u"doc", u"python")
>>> print child_path
/usr/share/doc/python
If one of the segments contains a slash or equals the string
"..", that is considered an insecure operation and thus an
``InsecurePathError`` is raised.
"""
p = self
for segment in segments:
raise_on_insecure_file_name(segment)
p = p.join(segment)
return p
def parent(self):
"""
Return a path object representing the parent of this ``path``
object on which this method is called. The result is the path
one level up.
"""
if self._pathname=='/':
return self
head, tail = os.path.split(self._pathname)
if head == '':
head = '.'
return self.__class__(head)
def name(self):
"""Return last segment of path."""
return os.path.basename(self._pathname)
def _incomparable(self, other):
"""
Returns ``NotImplemented`` if the other object is considered
incomparable, and ``False`` if the other object is considered
comparable. Can be used like:
return (self._incomparable(other) or
other._pathname == self._pathname)
"""
## TODO: trap: we're asserting that root wasn't replaced with
## a equivalent but non-identical root object (comparing
## other.root != self.root would cause a recursion)
if not hasattr(other, 'root') or other.root is not self.root:
return NotImplemented
if not isinstance(other, PathnameMixin):
return NotImplemented
return False
def __lt__(self, other):
return (self._incomparable(other) or
(self._pathname < other._pathname))
def __eq__(self, other):
return (self._incomparable(other) or
(self._pathname == other._pathname))
def __gt__(self, other):
return not (self < other or self == other)
def __le__(self, other):
return self < other or self == other
def __ge__(self, other):
return not self < other
def __ne__(self, other):
return not self == other
class SimpleComparitionMixin(object):
"""
This class implements the equity/comparition-methods using
self.name() and self.parent(). Not compatible with the
PathnameMixin class.
"""
def _incomparable(self, other):
"""
Returns ``NotImplemented`` if the other object is considered
incomparable, and ``False`` if the other object is considered
comparable. Can be used like:
return (self._incomparable(other) or
other._pathname == self._pathname)
"""
for name in ('root', 'parent', 'name'):
if not hasattr(other, name):
return NotImplemented
return False
def __lt__(self, other):
if self._incomparable(other):
return NotImplemented
## root object
if self.parent() is self and other.parent() is other:
return self.name() < other.name()
## compare parents ... and if they are equal, compare name
return ((self.parent() < other.parent()) or
(self.name() < other.name()))
def __eq__(self, other):
if self._incomparable(other):
return NotImplemented
## root object
if self.parent() is self and other.parent() is other:
return self.name() == other.name()
return ((self.parent() == other.parent()) and
(self.name() == other.name()))
def __gt__(self, other):
return not self <= other
def __le__(self, other):
return self < other or self == other
def __ge__(self, other):
return not self < other
def __unicode__(self):
if self.parent() == self:
return u'/'
return str(self.parent())+ u'/' + self.name()
def __str__(self):
return unicode(self).encode('utf8')
def __repr__(self):
return '%s(%r)' % (self.__class__.__name__, str(self))