Repository navigation
Expand file tree
/
Copy pathutil.py
More file actions
445 lines (353 loc) · 13.1 KB
/
Copy pathutil.py
File metadata and controls
445 lines (353 loc) · 13.1 KB
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
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
435
436
437
438
439
440
441
442
443
444
"""Ginkgo utility functions and classes
This module contains functions and classes that are shared across modules or
are more general utilities that aren't specific to Ginkgo. This way we keep
Ginkgo modules very dense in readable domain specific code.
"""
import resource
import os
import errno
import tempfile
class defaultproperty(object):
"""
Allow for default-valued properties to be added to classes.
Example usage:
class Foo(object):
bar = defaultproperty(list)
"""
def __init__(self, default_factory, *args, **kwargs):
self.default_factory = default_factory
self.args = args
self.kwargs = kwargs
def __get__(self, instance, owner):
if instance is None:
return None
for kls in owner.__mro__:
for key, value in kls.__dict__.items():
if value == self:
newval = self.default_factory(*self.args, **self.kwargs)
instance.__dict__[key] = newval
return newval
def daemonize(preserve_fds=None):
"""\
Standard daemonization of a process.
http://www.svbug.com/documentation/comp.unix.programmer-FAQ/faq_2.html#SEC16
"""
def _maxfd(limit=1024):
maxfd = resource.getrlimit(resource.RLIMIT_NOFILE)[1]
if maxfd == resource.RLIM_INFINITY:
return limit
else:
return maxfd
def _devnull(default="/dev/null"):
if hasattr(os, "devnull"):
return os.devnull
else:
return default
def _close_fds(preserve=None):
preserve = preserve or []
for fd in range(0, _maxfd()):
if fd not in preserve:
try:
os.close(fd)
except OSError: # fd wasn't open to begin with (ignored)
pass
if os.fork():
os._exit(0)
os.setsid()
if os.fork():
os._exit(0)
os.umask(0)
_close_fds(preserve_fds)
os.open(_devnull(), os.O_RDWR)
os.dup2(0, 1)
os.dup2(0, 2)
def prevent_core_dump():
""" Prevent this process from generating a core dump.
Sets the soft and hard limits for core dump size to zero. On
Unix, this prevents the process from creating core dump
altogether.
"""
core_resource = resource.RLIMIT_CORE
try:
# Ensure the resource limit exists on this platform, by requesting
# its current value
core_limit_prev = resource.getrlimit(core_resource)
except ValueError as e:
raise RuntimeWarning(
"System does not support RLIMIT_CORE resource limit ({})".format(e))
# Set hard and soft limits to zero, i.e. no core dump at all
resource.setrlimit(core_resource, (0, 0))
class PassthroughEvent(object):
def wait(self, timeout=None): return
def set(self): return
def clear(self): return
class AbstractStateMachine(object):
event_class = PassthroughEvent
def __init__(self, subject):
self._state = self.initial_state
self._subject = subject
self._waitables = {}
if hasattr(self._subject, 'async'):
self.event_class = self._subject.async.event
for state in self.allow_wait:
self._waitables[state] = self.event_class()
@property
def current(self):
return self._state
def wait(self, state, timeout=None):
if state in self._waitables:
self._waitables[state].wait(timeout)
else:
raise RuntimeWarning("Unable to wait for state '{}'".format(state))
def __call__(self, event):
from_states, to_state, callback = self._lookup_event(event)
if self._state in from_states:
self._callback(callback)
self._transition(to_state)
else:
raise RuntimeWarning("""
Unable to enter '{}' from state '{}'
""".format(to_state, self.current).strip())
def _lookup_event(self, event):
event_definition = "event_{}".format(event)
if hasattr(self, event_definition):
return getattr(self, event_definition)
else:
raise AttributeError("No event '{}' on {}".format(event, self))
def _callback(self, name):
if name is not None and hasattr(self._subject, name):
getattr(self._subject, name)()
def _transition(self, new_state):
for state in self._waitables:
self._waitables[state].clear()
self._state = new_state
if new_state in self._waitables:
self._waitables[new_state].set()
class GlobalContext(object):
"""Context manager mixin for stackable singletons
Use this mixin when a class has a global singleton set somewhere that can
be temporarily set while in the context of an instance of that class::
class Foo(GlobalContext):
instance = None # where we'll keep the singleton
singleton_attr = (Foo, 'instance') # tell mixin where it is
Foo.instance = Foo() # set an initial Foo singleton
temporary_foo = Foo() # create another Foo
# now use it as a context
with temporary_foo:
# the singleton will be set to this instance
assert Foo.instance is temporary_foo
# then set back when you exit the context
assert Foo.instance is not temporary_foo
You can also nest global contexts if necessary. The main API is just
setting where the singleton is with `singleton_attr`, which is a tuple of
(object, attribute name). If `singleton_attr` is not set, there is no
effect when you use the context manager. You can define `singleton_attr`
outside the class definition to decouple your class definition from your
use of a singleton. For example::
class Foo(GlobalContext):
pass
singleton = Foo() # module level singleton
Foo.singleton_attr = (sys.modules[__name__], 'singleton')
"""
singleton_attr = None
_singleton_stacks = {}
@classmethod
def _get_singleton(cls):
if cls.singleton_attr:
return getattr(*cls.singleton_attr)
@classmethod
def _set_singleton(cls, value):
if cls.singleton_attr:
setattr(*list(cls.singleton_attr)+[value])
@classmethod
def _push_context(cls, obj):
if cls.singleton_attr:
klass = cls.__name__
if klass not in cls._singleton_stacks:
cls._singleton_stacks[klass] = []
cls._singleton_stacks[klass].append(cls._get_singleton())
cls._set_singleton(obj)
@classmethod
def _pop_context(cls):
if cls.singleton_attr:
klass = cls.__name__
cls._set_singleton(
cls._singleton_stacks.get(klass, []).pop())
def __enter__(self):
self.__class__._push_context(self)
return self
def __exit__(self, type, value, traceback):
self.__class__._pop_context()
class Pidfile(object):
"""\
Manage a PID file. If a specific name is provided
it and '"%s.oldpid" % name' will be used. Otherwise
we create a temp file using os.mkstemp.
"""
def __init__(self, fname):
self.fname = fname
self.pid = None
def create(self, pid):
oldpid = self.validate()
if oldpid:
if oldpid == os.getpid():
return
raise RuntimeError("Already running on PID %s " \
"(or pid file '%s' is stale)" % (os.getpid(), self.fname))
self.pid = pid
# Write pidfile
fdir = os.path.dirname(self.fname)
if fdir and not os.path.isdir(fdir):
raise RuntimeError("%s doesn't exist. Can't create pidfile." % fdir)
fd, fname = tempfile.mkstemp(dir=fdir)
os.write(fd, "%s\n" % self.pid)
if self.fname:
os.rename(fname, self.fname)
else:
self.fname = fname
os.close(fd)
# set permissions to -rw-r--r--
os.chmod(self.fname, 420)
def rename(self, path):
self.unlink()
self.fname = path
self.create(self.pid)
def unlink(self):
""" delete pidfile"""
try:
with open(self.fname, "r") as f:
pid1 = int(f.read() or 0)
if pid1 == self.pid:
os.unlink(self.fname)
except:
pass
def validate(self):
""" Validate pidfile and make it stale if needed"""
if not self.fname:
return
try:
with open(self.fname, "r") as f:
wpid = int(f.read() or 0)
if wpid <= 0:
return
try:
os.kill(wpid, 0)
return wpid
except OSError as e:
if e[0] == errno.ESRCH:
return
raise
except IOError as e:
if e[0] == errno.ENOENT:
return
raise
## The following is extracted from ProxyTypes 0.9 package, licensed under ZPL:
## http://pypi.python.org/pypi/ProxyTypes
class AbstractProxy(object):
"""Delegates all operations (except ``.__subject__``) to another object"""
__slots__ = ()
def __call__(self,*args,**kw):
return self.__subject__(*args,**kw)
def __getattribute__(self, attr, oga=object.__getattribute__):
subject = oga(self,'__subject__')
if attr=='__subject__':
return subject
return getattr(subject,attr)
def __setattr__(self,attr,val, osa=object.__setattr__):
if attr=='__subject__':
osa(self,attr,val)
else:
setattr(self.__subject__,attr,val)
def __delattr__(self,attr, oda=object.__delattr__):
if attr=='__subject__':
oda(self,attr)
else:
delattr(self.__subject__,attr)
def __nonzero__(self):
return bool(self.__subject__)
def __getitem__(self,arg):
return self.__subject__[arg]
def __setitem__(self,arg,val):
self.__subject__[arg] = val
def __delitem__(self,arg):
del self.__subject__[arg]
def __getslice__(self,i,j):
return self.__subject__[i:j]
def __setslice__(self,i,j,val):
self.__subject__[i:j] = val
def __delslice__(self,i,j):
del self.__subject__[i:j]
def __contains__(self,ob):
return ob in self.__subject__
for name in 'repr str hash len abs complex int long float iter oct hex'.split():
exec ("def __%s__(self): return %s(self.__subject__)" % (name,name))
for name in 'cmp', 'coerce', 'divmod':
exec ("def __%s__(self,ob): return %s(self.__subject__,ob)" % (name,name))
for name,op in [
('lt','<'), ('gt','>'), ('le','<='), ('ge','>='),
('eq','=='), ('ne','!=')
]:
exec ("def __%s__(self,ob): return self.__subject__ %s ob" % (name,op))
for name,op in [('neg','-'), ('pos','+'), ('invert','~')]:
exec ("def __%s__(self): return %s self.__subject__" % (name,op))
for name, op in [
('or','|'), ('and','&'), ('xor','^'), ('lshift','<<'), ('rshift','>>'),
('add','+'), ('sub','-'), ('mul','*'), ('div','/'), ('mod','%'),
('truediv','/'), ('floordiv','//')
]:
exec (
("def __%(name)s__(self,ob):\n"
" return self.__subject__ %(op)s ob\n"
"\n"
"def __r%(name)s__(self,ob):\n"
" return ob %(op)s self.__subject__\n"
"\n"
"def __i%(name)s__(self,ob):\n"
" self.__subject__ %(op)s=ob\n"
" return self\n"
) % locals())
del name, op
# Oddball signatures
def __rdivmod__(self,ob):
return divmod(ob, self.__subject__)
def __pow__(self,*args):
return pow(self.__subject__,*args)
def __ipow__(self,ob):
self.__subject__ **= ob
return self
def __rpow__(self,ob):
return pow(ob, self.__subject__)
class ObjectProxy(AbstractProxy):
"""Proxy for a specific object"""
__slots__ = "__subject__"
def __init__(self,subject):
self.__subject__ = subject
class AbstractWrapper(AbstractProxy):
"""Mixin to allow extra behaviors and attributes on proxy instance"""
__slots__ = ()
def __getattribute__(self, attr, oga=object.__getattribute__):
if attr.startswith('__'):
subject = oga(self,'__subject__')
if attr=='__subject__':
return subject
return getattr(subject,attr)
return oga(self,attr)
def __getattr__(self,attr, oga=object.__getattribute__):
return getattr(oga(self,'__subject__'), attr)
def __setattr__(self,attr,val, osa=object.__setattr__):
if (
attr=='__subject__'
or hasattr(type(self),attr) and not attr.startswith('__')
):
osa(self,attr,val)
else:
setattr(self.__subject__,attr,val)
def __delattr__(self,attr, oda=object.__delattr__):
if (
attr=='__subject__'
or hasattr(type(self),attr) and not attr.startswith('__')
):
oda(self,attr)
else:
delattr(self.__subject__,attr)
class ObjectWrapper(ObjectProxy, AbstractWrapper): __slots__ = ()