2010-11-22 03:21:17 -08:00
|
|
|
import sys, os, errno, fcntl
|
2010-11-13 00:53:55 -08:00
|
|
|
import vars
|
2010-11-12 05:24:46 -08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def unlink(f):
|
|
|
|
|
"""Delete a file at path 'f' if it currently exists.
|
|
|
|
|
|
|
|
|
|
Unlike os.unlink(), does not throw an exception if the file didn't already
|
|
|
|
|
exist.
|
|
|
|
|
"""
|
|
|
|
|
try:
|
|
|
|
|
os.unlink(f)
|
|
|
|
|
except OSError, e:
|
|
|
|
|
if e.errno == errno.ENOENT:
|
|
|
|
|
pass # it doesn't exist, that's what you asked for
|
|
|
|
|
|
|
|
|
|
|
2010-11-12 07:03:06 -08:00
|
|
|
def mkdirp(d, mode=None):
|
|
|
|
|
"""Recursively create directories on path 'd'.
|
|
|
|
|
|
|
|
|
|
Unlike os.makedirs(), it doesn't raise an exception if the last element of
|
|
|
|
|
the path already exists.
|
|
|
|
|
"""
|
|
|
|
|
try:
|
|
|
|
|
if mode:
|
|
|
|
|
os.makedirs(d, mode)
|
|
|
|
|
else:
|
|
|
|
|
os.makedirs(d)
|
|
|
|
|
except OSError, e:
|
|
|
|
|
if e.errno == errno.EEXIST:
|
|
|
|
|
pass
|
|
|
|
|
else:
|
|
|
|
|
raise
|
|
|
|
|
|
|
|
|
|
|
2010-11-18 23:06:38 -08:00
|
|
|
def log_(s):
|
2010-11-13 00:53:55 -08:00
|
|
|
sys.stdout.flush()
|
2010-11-22 01:50:46 -08:00
|
|
|
if vars.DEBUG_PIDS:
|
|
|
|
|
sys.stderr.write('%d %s' % (os.getpid(), s))
|
|
|
|
|
else:
|
|
|
|
|
sys.stderr.write(s)
|
2010-11-13 00:53:55 -08:00
|
|
|
sys.stderr.flush()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _clog(s):
|
2010-11-18 23:06:38 -08:00
|
|
|
log_('\x1b[32mredo %s\x1b[1m%s\x1b[m' % (vars.DEPTH, s))
|
2010-11-13 00:53:55 -08:00
|
|
|
def _bwlog(s):
|
2010-11-18 23:06:38 -08:00
|
|
|
log_('redo %s%s' % (vars.DEPTH, s))
|
2010-11-13 01:21:59 -08:00
|
|
|
|
|
|
|
|
def _cerr(s):
|
2010-11-18 23:06:38 -08:00
|
|
|
log_('\x1b[31mredo: %s\x1b[1m%s\x1b[m' % (vars.DEPTH, s))
|
2010-11-13 01:21:59 -08:00
|
|
|
def _bwerr(s):
|
2010-11-18 23:06:38 -08:00
|
|
|
log_('redo: %s%s' % (vars.DEPTH, s))
|
2010-11-13 01:21:59 -08:00
|
|
|
|
|
|
|
|
|
2010-11-13 00:53:55 -08:00
|
|
|
if os.isatty(2):
|
|
|
|
|
log = _clog
|
2010-11-13 01:21:59 -08:00
|
|
|
err = _cerr
|
2010-11-13 00:53:55 -08:00
|
|
|
else:
|
|
|
|
|
log = _bwlog
|
2010-11-13 01:21:59 -08:00
|
|
|
err = _bwerr
|
2010-11-13 00:53:55 -08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def debug(s):
|
2010-11-16 04:13:17 -08:00
|
|
|
if vars.DEBUG >= 1:
|
2010-11-18 23:06:38 -08:00
|
|
|
log_('redo: %s%s' % (vars.DEPTH, s))
|
2010-11-16 04:13:17 -08:00
|
|
|
def debug2(s):
|
|
|
|
|
if vars.DEBUG >= 2:
|
2010-11-18 23:06:38 -08:00
|
|
|
log_('redo: %s%s' % (vars.DEPTH, s))
|
2010-11-25 06:35:22 -08:00
|
|
|
def debug3(s):
|
|
|
|
|
if vars.DEBUG >= 3:
|
|
|
|
|
log_('redo: %s%s' % (vars.DEPTH, s))
|
2010-11-13 00:53:55 -08:00
|
|
|
|
|
|
|
|
|
2010-11-22 03:21:17 -08:00
|
|
|
def close_on_exec(fd, yes):
|
|
|
|
|
fl = fcntl.fcntl(fd, fcntl.F_GETFD)
|
|
|
|
|
fl &= ~fcntl.FD_CLOEXEC
|
|
|
|
|
if yes:
|
|
|
|
|
fl |= fcntl.FD_CLOEXEC
|
|
|
|
|
fcntl.fcntl(fd, fcntl.F_SETFD, fl)
|
|
|
|
|
|
|
|
|
|
|