2014-01-19 05:20:01 +00:00
|
|
|
import os
|
2014-10-24 04:21:28 +00:00
|
|
|
import sys
|
2016-05-20 23:53:14 +00:00
|
|
|
import netlib.utils
|
2014-02-27 05:33:48 +00:00
|
|
|
|
2016-05-07 18:15:03 +00:00
|
|
|
import six
|
|
|
|
|
2012-04-29 00:05:38 +00:00
|
|
|
|
2012-07-23 03:03:56 +00:00
|
|
|
SIZE_UNITS = dict(
|
2015-06-18 16:12:11 +00:00
|
|
|
b=1024 ** 0,
|
|
|
|
k=1024 ** 1,
|
|
|
|
m=1024 ** 2,
|
|
|
|
g=1024 ** 3,
|
|
|
|
t=1024 ** 4,
|
2012-07-23 03:03:56 +00:00
|
|
|
)
|
|
|
|
|
2013-01-05 02:25:09 +00:00
|
|
|
|
2015-06-18 09:07:33 +00:00
|
|
|
class MemBool(object):
|
2015-06-18 16:12:11 +00:00
|
|
|
|
2013-01-05 02:25:09 +00:00
|
|
|
"""
|
|
|
|
Truth-checking with a memory, for use in chained if statements.
|
|
|
|
"""
|
2015-05-30 00:03:13 +00:00
|
|
|
|
2013-01-05 02:25:09 +00:00
|
|
|
def __init__(self):
|
|
|
|
self.v = None
|
|
|
|
|
|
|
|
def __call__(self, v):
|
|
|
|
self.v = v
|
|
|
|
return bool(v)
|
|
|
|
|
|
|
|
|
2012-07-23 03:03:56 +00:00
|
|
|
def parse_size(s):
|
|
|
|
try:
|
|
|
|
return int(s)
|
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
for i in SIZE_UNITS.keys():
|
|
|
|
if s.endswith(i):
|
|
|
|
try:
|
|
|
|
return int(s[:-1]) * SIZE_UNITS[i]
|
|
|
|
except ValueError:
|
|
|
|
break
|
|
|
|
raise ValueError("Invalid size specification.")
|
|
|
|
|
|
|
|
|
2012-06-24 04:38:32 +00:00
|
|
|
def parse_anchor_spec(s):
|
2012-04-29 00:05:38 +00:00
|
|
|
"""
|
2012-06-24 04:38:32 +00:00
|
|
|
Return a tuple, or None on error.
|
2012-04-29 00:05:38 +00:00
|
|
|
"""
|
2015-06-18 09:07:33 +00:00
|
|
|
if "=" not in s:
|
2012-06-24 04:38:32 +00:00
|
|
|
return None
|
|
|
|
return tuple(s.split("=", 1))
|
2012-04-29 00:05:38 +00:00
|
|
|
|
2012-04-28 00:42:03 +00:00
|
|
|
|
2012-07-23 12:00:55 +00:00
|
|
|
def xrepr(s):
|
|
|
|
return repr(s)[1:-1]
|
|
|
|
|
|
|
|
|
2012-07-24 22:44:21 +00:00
|
|
|
def inner_repr(s):
|
|
|
|
"""
|
|
|
|
Returns the inner portion of a string or unicode repr (i.e. without the
|
|
|
|
quotes)
|
|
|
|
"""
|
2016-05-10 20:39:42 +00:00
|
|
|
if (six.PY2 and isinstance(s, unicode)) or isinstance(s, bytes):
|
2012-07-24 22:44:21 +00:00
|
|
|
return repr(s)[2:-1]
|
|
|
|
else:
|
|
|
|
return repr(s)[1:-1]
|
|
|
|
|
|
|
|
|
2012-07-23 04:39:25 +00:00
|
|
|
def escape_unprintables(s):
|
2012-07-24 22:44:21 +00:00
|
|
|
"""
|
|
|
|
Like inner_repr, but preserves line breaks.
|
|
|
|
"""
|
2012-07-23 04:39:25 +00:00
|
|
|
s = s.replace("\r\n", "PATHOD_MARKER_RN")
|
|
|
|
s = s.replace("\n", "PATHOD_MARKER_N")
|
2016-05-07 18:15:03 +00:00
|
|
|
if six.PY2:
|
|
|
|
s = inner_repr(s)
|
|
|
|
else:
|
|
|
|
s = s.encode('unicode_escape').decode('ascii')
|
2012-07-23 04:39:25 +00:00
|
|
|
s = s.replace("PATHOD_MARKER_RN", "\n")
|
|
|
|
s = s.replace("PATHOD_MARKER_N", "\n")
|
|
|
|
return s
|
|
|
|
|
|
|
|
|
2016-05-20 23:53:14 +00:00
|
|
|
data = netlib.utils.Data(__name__)
|
2014-10-24 04:19:55 +00:00
|
|
|
|
2015-05-30 00:03:13 +00:00
|
|
|
|
2016-03-27 10:02:41 +00:00
|
|
|
def daemonize(stdin='/dev/null', stdout='/dev/null', stderr='/dev/null'): # pragma: no cover
|
2014-10-24 04:19:55 +00:00
|
|
|
try:
|
|
|
|
pid = os.fork()
|
|
|
|
if pid > 0:
|
|
|
|
sys.exit(0)
|
2015-05-30 00:03:13 +00:00
|
|
|
except OSError as e:
|
2014-10-24 04:19:55 +00:00
|
|
|
sys.stderr.write("fork #1 failed: (%d) %s\n" % (e.errno, e.strerror))
|
|
|
|
sys.exit(1)
|
|
|
|
os.chdir("/")
|
|
|
|
os.umask(0)
|
|
|
|
os.setsid()
|
|
|
|
try:
|
|
|
|
pid = os.fork()
|
|
|
|
if pid > 0:
|
|
|
|
sys.exit(0)
|
2015-05-30 00:03:13 +00:00
|
|
|
except OSError as e:
|
2014-10-24 04:19:55 +00:00
|
|
|
sys.stderr.write("fork #2 failed: (%d) %s\n" % (e.errno, e.strerror))
|
|
|
|
sys.exit(1)
|
|
|
|
si = open(stdin, 'rb')
|
|
|
|
so = open(stdout, 'a+b')
|
|
|
|
se = open(stderr, 'a+b', 0)
|
|
|
|
os.dup2(si.fileno(), sys.stdin.fileno())
|
|
|
|
os.dup2(so.fileno(), sys.stdout.fileno())
|
|
|
|
os.dup2(se.fileno(), sys.stderr.fileno())
|