2013-02-15 09:16:16 +00:00
#!/usr/bin/env python
# Rhizome mirror daemon
# Copyright (C) 2013 Serval Project Inc.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
"""This daemon regularly extracts file-sharing (service=file) bundles from
Rhizome into a mirror directory, and unpacks all bundles which are archive
format (zip, tar, tgz, etc.), into their constituent files. If a newer version
of an already-unpacked bundle arrives, the prior unpack is deleted and the new
version unpacked in its place.
This effectively maintains the directory as an up-to-date mirror of the local
Rhizome store content.
On-disk file/directory names are formed by conactenating the bundle name field
and the bundle ID separated by ':', in order to avoid collisions.
@author Andrew Bettison <andrew@servalproject.com>
"""
import sys
2013-02-18 06:54:01 +00:00
import errno
import time
import os
2013-02-15 09:16:16 +00:00
import os.path
import re
import argparse
import subprocess
import datetime
2013-02-21 12:12:21 +00:00
import shutil
2013-02-18 06:54:01 +00:00
import fnmatch
2013-02-21 12:12:21 +00:00
import zipfile
import tarfile
2013-02-15 09:16:16 +00:00
def main():
2013-02-21 12:12:21 +00:00
instancepath = os.environ.get('SERVALINSTANCE_PATH')
2013-02-20 07:05:18 +00:00
parser = argparse.ArgumentParser(description='Continuously extract Rhizome store into mirror directory.')
2013-02-21 12:12:21 +00:00
parser.add_argument('--mirror-dir', dest='mirror_dir', metavar='PATH', required=True, help='Path of directory to store extracted payloads')
parser.add_argument('--servald', dest='servald', metavar='PATH', required=True, help='Path of servald executable')
parser.add_argument('--instance', dest='instancepath', metavar='PATH', required=not instancepath, default=instancepath, help='Path of servald instance directory')
parser.add_argument('--interval', dest='interval', metavar='N', type=float, default=0, help='Sleep N seconds between polling Rhizome; N=0 means run once and exit')
parser.add_argument('--filter-name', dest='name_filter', metavar='GLOB', help='Only mirror bundles whose names match GLOB pattern')
parser.add_argument('--unpack-dir', dest='unpack_dir', metavar='PATH', default=None, help='Path of directory in which to unpack bundles (zip, tar, etc.)')
parser.add_argument('--exec-on-unpack', dest='exec_on_unpack', metavar='EXECUTABLE', help='Run EXECUTABLE after unpacking one or more bundles')
parser.add_argument('--expire-delay', dest='expire_delay', metavar='N', type=int, default=0, help='Keep bundles in mirror for N seconds after no longer listed by Rhizome')
parser.add_argument('--error-retry', dest='error_retry', metavar='N', type=int, default=600, help='Wait N seconds before retrying failed operations')
2013-02-18 06:54:01 +00:00
parser.add_argument('--paranoid', dest='paranoid', action='store_true', help='Continually check for and correct corrupted mirror contents')
2013-02-21 12:12:21 +00:00
parser.add_argument('--log-to-stdout', dest='log_to_stdout', action='store_true', help='Log activity on standard output')
2013-02-15 09:16:16 +00:00
opts = parser.parse_args()
2013-02-21 12:12:21 +00:00
global log_output
log_output = sys.stderr if opts.log_to_stdout else None
2013-02-20 07:05:18 +00:00
try:
status, output = invoke_servald(opts, ['help'])
except ServaldInterfaceException, e:
fatal(e)
if status is None:
2013-02-15 09:16:16 +00:00
fatal('no executable servald')
2013-02-20 07:05:18 +00:00
if status != 0 or output is None:
fatal('faulty servald')
2013-02-15 09:16:16 +00:00
mirror = RhizomeMirror(opts)
2013-02-21 12:12:21 +00:00
mirror.clean_unpack_dir()
2013-02-15 09:16:16 +00:00
mirror.seed()
2013-02-18 06:54:01 +00:00
while True:
mirror.list()
mirror.update()
mirror.expire()
2013-02-21 12:12:21 +00:00
if mirror.unpacked:
if opts.exec_on_unpack:
args = [opts.exec_on_unpack] + sorted(mirror.unpacked)
log("execute " + ' '.join(args))
try:
subprocess.call(args, stdin=open('/dev/null'))
mirror.unpacked.clear()
except OSError, e:
error('cannot execute %r - %s' % (opts.exec_on_unpack, e))
2013-02-18 06:54:01 +00:00
if not opts.interval:
break
time.sleep(opts.interval)
sys.exit(0)
2013-02-15 09:16:16 +00:00
class RhizomeMirror(object):
def __init__(self, opts):
self.opts = opts
2013-02-20 07:05:18 +00:00
self.hash_errors = {}
self.extract_errors = {}
2013-02-15 09:16:16 +00:00
self.extracted = {}
2013-02-18 06:54:01 +00:00
self.available = None
2013-02-21 12:12:21 +00:00
self.unpacked = set()
2013-02-15 09:16:16 +00:00
self.payloads_path = opts.mirror_dir
self.manifests_path = os.path.join(self.payloads_path, '.manifests')
2013-02-21 12:12:21 +00:00
self.unpacks_path = opts.unpack_dir
2013-02-15 09:16:16 +00:00
2013-02-18 06:54:01 +00:00
def manifest_path(self, manifest):
return os.path.join(self.manifests_path, manifest.stem()) + '.manifest'
def payload_path(self, manifest):
return os.path.join(self.payloads_path, manifest.stem()) + manifest.suffix()
2013-02-21 12:12:21 +00:00
def unpack_path(self, manifest):
if not self.unpacks_path:
return None
return os.path.join(self.unpacks_path, manifest.stem())
def unpack_path_tmp(self, manifest, var):
unpack_path = self.unpack_path(manifest)
if not unpack_path:
return None
head, tail = os.path.split(unpack_path)
return os.path.join(head, '.tmp-%s-%s' % (var, tail))
def clean_unpack_dir(self):
if self.unpacks_path and os.path.isdir(self.unpacks_path):
for name in os.listdir(self.unpacks_path):
if name.startswith('.tmp-'):
self.unlink(os.path.join(self.unpacks_path, name))
2013-02-15 09:16:16 +00:00
def seed(self):
self.extracted = {}
2013-02-21 12:12:21 +00:00
if self.mkdirs(self.manifests_path) is not None:
for manifest_name in os.listdir(self.manifests_path):
manifest_path = os.path.join(self.manifests_path, manifest_name)
if manifest_name.endswith('.manifest'):
stem = os.path.splitext(manifest_name)[0]
manifest = RhizomeManifest.from_file(file(manifest_path))
if manifest is not None and stem == manifest.stem():
if self.sync(manifest):
log('seeded %r' % (stem,))
self.extracted[stem] = manifest
continue
# Remove invalid manifests.
self.unlink(manifest_path)
2013-02-18 06:54:01 +00:00
def sync(self, manifest):
payload_path = self.payload_path(manifest)
if manifest.filesize == 0:
self.unlink(payload_path)
return True
elif os.path.exists(payload_path):
2013-02-20 07:05:18 +00:00
payload_hash = None
if self.hash_errors.get(manifest.id, 0) + self.opts.error_retry < time.time():
payload_hash = servald_rhizome_hash(self.opts, payload_path)
if payload_hash is None:
self.hash_errors[manifest.id] = time.time()
if payload_hash is None:
# Can't tell if payload matches manifest or not.
pass
elif payload_hash == manifest.filehash:
# This logic is DEFECTIVE for the case of encrypted payload, in which the hash is
# for the ciphertext, not the clear text, but we are extracting the clear text, so
# the hash will never match.
2013-02-21 12:12:21 +00:00
unpack_dir = self.unpack_path(manifest)
if not os.path.isdir(unpack_dir):
self.unpack_payload(manifest)
2013-02-18 06:54:01 +00:00
return True
else:
# Remove payload that does not match its manifest.
self.unlink(payload_path)
# Not synced -- have to extract the payload.
return False
def filter(self, manifest):
if self.opts.name_filter:
if not fnmatch.fnmatch(manifest.name or '', self.opts.name_filter):
return False
return True
def list(self):
self.available = None
entries = servald_rhizome_list(self.opts)
if entries is not None:
self.available = {}
for ent in entries:
manifest = RhizomeManifest.from_list_entry(ent)
if manifest is not None and manifest.service == 'file':
stem = manifest.stem()
self.available[stem] = manifest
def update(self):
if self.available is not None:
for stem, manifest in self.available.iteritems():
manifest_path = self.manifest_path(manifest)
payload_path = self.payload_path(manifest)
if self.filter(manifest):
extracted_manifest = self.extracted.get(stem)
kwargs = {}
if extracted_manifest is None or manifest.succeeds(extracted_manifest):
kwargs['manifest_path'] = manifest_path
if manifest.filesize == 0:
self.unlink(payload_path)
else:
kwargs['payload_path'] = payload_path
elif manifest.id == extracted_manifest.id:
# Assume manifest and payload files are correct if present, unless
# in 'paranoid' mode.
if manifest.filesize == 0:
self.unlink(payload_path)
elif os.path.exists(payload_path):
2013-02-21 12:12:21 +00:00
payload_hash = None
2013-02-18 06:54:01 +00:00
if self.opts.paranoid:
2013-02-20 07:05:18 +00:00
if self.hash_errors.get(manifest.id, 0) + self.opts.error_retry < time.time():
payload_hash = servald_rhizome_hash(self.opts, payload_path)
if payload_hash is None:
self.hash_errors[manifest.id] = time.time()
2013-02-21 12:12:21 +00:00
if payload_hash is None or payload_hash == manifest.filehash:
# Payload is consistent with manifest. Check that it is
# unpacked.
unpack_dir = self.unpack_path(manifest)
if not os.path.isdir(unpack_dir):
self.unpack_payload(manifest)
else:
# This logic is DEFECTIVE for the case of encrypted payload, in
# which the hash is for the ciphertext, not the clear text, but
# we are extracting the clear text, so the hash will never
# match.
kwargs['payload_path'] = payload_path
2013-02-18 06:54:01 +00:00
else:
kwargs['payload_path'] = payload_path
if os.path.exists(manifest_path):
self.touch(manifest_path) # Remember when this was last available
if self.opts.paranoid:
check_manifest = RhizomeManifest.from_file(file(manifest_path))
if check_manifest is None or check_manifest != extracted_manifest:
kwargs['manifest_path'] = manifest_path
else:
kwargs['manifest_path'] = manifest_path
else:
# Ignore listed manifests with the same stem but different bundle ID; keep
# the already-extracted bundle (until expired).
pass
if kwargs:
2013-02-20 07:05:18 +00:00
extracted = None
if self.extract_errors.get(manifest.id, 0) + self.opts.error_retry < time.time():
extracted = servald_rhizome_extract(self.opts, manifest.id, **kwargs)
if extracted is None:
self.extract_errors[manifest.id] = time.time()
if extracted is None:
pass
elif extracted:
2013-02-18 06:54:01 +00:00
extracted_manifest = RhizomeManifest.from_file(file(manifest_path))
if extracted_manifest is None or extracted_manifest.id != manifest.id or extracted_manifest.version != manifest.version:
error('invalid manifest extracted for bid=%s' % (manifest.id,))
self.unlink(payload_path)
self.unlink(manifest_path)
self.extracted[stem] = None
2013-02-20 07:05:18 +00:00
self.extract_errors[manifest.id] = time.time()
2013-02-18 06:54:01 +00:00
else:
self.extracted[stem] = extracted_manifest
2013-02-21 12:12:21 +00:00
if 'payload_path' in kwargs:
self.unpack_payload(manifest)
def unpack_payload(self, manifest):
unpack_dir_new = self.unpack_path_tmp(manifest, 'new')
unpack_dir_old = self.unpack_path_tmp(manifest, 'old')
self.rmtree(unpack_dir_new)
if self.mkdirs(unpack_dir_new) is not None:
payload_path = self.payload_path(manifest)
try:
if zipfile.is_zipfile(payload_path):
log('unzip %s into %s' % (payload_path, unpack_dir_new))
with zipfile.ZipFile(payload_path) as zf:
names = []
for name in zf.namelist():
if name.startswith('/') or '../' in name:
log(' skip %s' % name)
else:
log(' extract %s' % name)
zf.extract(name, unpack_dir_new)
elif tarfile.is_tarfile(payload_path):
log('untar %s into %s' % (payload_path, unpack_dir_new))
with tarfile.open(payload_path) as tf:
names = []
for name in tf.getnames():
if name.startswith('/') or '../' in name:
log(' skip %s' % name)
else:
log(' extract %s' % name)
tf.extract(name, unpack_dir_new)
else:
return False
unpack_dir = self.unpack_path(manifest)
if os.path.exists(unpack_dir):
self.rename(unpack_dir, unpack_dir_old)
if self.rename(unpack_dir_new, unpack_dir):
self.unpacked.add(unpack_dir)
return True
except zipfile.BadZipfile, e:
error('cannot unzip %s: %s' % (payload_path, e))
return None
except tarfile.TarError, e:
error('cannot untar %s: %s' % (payload_path, e))
return None
finally:
self.rmtree(unpack_dir_new)
self.rmtree(unpack_dir_old)
return False
2013-02-18 06:54:01 +00:00
def expire(self):
now = time.time()
if self.available is not None:
for stem, extracted_manifest in self.extracted.iteritems():
2013-02-20 07:05:18 +00:00
if extracted_manifest is not None and stem not in self.available:
manifest_path = self.manifest_path(extracted_manifest)
payload_path = self.payload_path(extracted_manifest)
2013-02-18 06:54:01 +00:00
if os.path.exists(manifest_path):
if self.mtime(manifest_path) + self.opts.expire_delay < now:
self.unlink(payload_path)
self.unlink(manifest_path)
self.extracted[stem] = None
else:
self.unlink(payload_path)
2013-02-21 12:12:21 +00:00
def mkdirs(self, path):
if os.path.isdir(path):
return False
log('mkdirs %r' % (path,))
try:
os.makedirs(path)
return True
except OSError, e:
error('cannot mkdir -p %r - %s' % (path, e))
return None
def rmtree(self, path):
if not os.path.exists(path):
return False
log('rmdirs %r' % (path,))
try:
shutil.rmtree(path)
return True
except OSError, e:
error('cannot rm -r %r - %s' % (path, e))
return None
2013-02-18 06:54:01 +00:00
def touch(self, path):
try:
2013-02-21 12:12:21 +00:00
open(path, "r+").close()
2013-02-18 06:54:01 +00:00
except OSError, e:
error('cannot touch %r - %s' % (path, e))
2013-02-21 12:12:21 +00:00
def rename(self, path, newpath):
if not os.path.exists(path):
return False
log('rename %r %r' % (path, newpath))
try:
os.rename(path, newpath)
return True
except OSError, e:
error('cannot rename %r to %r - %s' % (path, newpath, e))
return None
2013-02-18 06:54:01 +00:00
def unlink(self, path):
2013-02-21 12:12:21 +00:00
if not os.path.exists(path):
return False
log('unlink %r' % (path,))
try:
os.unlink(path)
return True
except OSError, e:
error('cannot unlink %r - %s' % (path, e))
return None
2013-02-15 09:16:16 +00:00
2013-02-18 06:54:01 +00:00
def mtime(self, path):
try:
return os.stat(path).st_mtime
except OSError, e:
if e.errno != errno.ENOENT:
error('cannot stat %r - %s' % (path, e))
return None
2013-02-15 09:16:16 +00:00
class RhizomeManifest(object):
2013-02-18 06:54:01 +00:00
def __init__(self, **fields):
self.service = str_nonempty(fields['service'])
self.id = bundle_id(fields['id'])
self.version = ulong(fields['version'])
self.filesize = ulong(fields['filesize']) if 'filesize' in fields else None
self.date = time_ms(fields['date']) if 'date' in fields else None
self.filehash = file_hash(fields['filehash']) if self.filesize or self.filesize is None else None
self.sender = subscriber_id(fields['sender']) if 'sender' in fields else None
self.recipient = subscriber_id(fields['recipient']) if 'recipient' in fields else None
self.name = str(fields['name']) if 'name' in fields else None
self._other = fields
2013-02-15 09:16:16 +00:00
2013-02-18 06:54:01 +00:00
@staticmethod
def fieldname(text):
if text.isalnum():
return text.lower()
raise ValueError('invalid literal for RhizomeManifest.fieldname(): %s' % (text,))
2013-02-15 09:16:16 +00:00
2013-02-18 06:54:01 +00:00
@classmethod
def is_fieldname(cls, text):
2013-02-15 09:16:16 +00:00
try:
2013-02-18 06:54:01 +00:00
cls.fieldname(text)
return True
except ValueError:
return False
2013-02-20 07:05:18 +00:00
def __eq__(self, other):
if not isinstance(other, type(self)):
return NotImplemented
return (self.service == other.service
and self.id == other.id
and self.version == other.version
and self.filesize == other.filesize
and self.date == other.date
and self.filehash == other.filehash
and self.sender == other.sender
and self.recipient == other.recipient
and self.name == other.name
and self._other == other._other)
def __ne__(self, other):
if not isinstance(other, type(self)):
return NotImplemented
return not self.__eq__(other)
2013-02-18 06:54:01 +00:00
def stem(self):
return os.path.splitext(self.name)[0] + ':' + self.id[:12]
def suffix(self):
return os.path.splitext(self.name)[1]
@classmethod
def from_list_entry(cls, ent):
fieldmap = {}
fields = dict((fieldmap.get(key, key), value) for key, value in ent.__dict__.iteritems() if cls.is_fieldname(key) and len(value))
return cls(**fields)
@classmethod
def from_file(cls, f):
body, sig = f.read().split('\0', 1)
if body.endswith('\n'):
body = body[:-1]
2013-02-15 09:16:16 +00:00
fields = {}
try:
2013-02-18 06:54:01 +00:00
for line in body.split('\n'):
2013-02-15 09:16:16 +00:00
field, value = line.split('=', 1)
2013-02-18 06:54:01 +00:00
fields[cls.fieldname(field)] = value
2013-02-15 09:16:16 +00:00
except (KeyError, ValueError):
return None
2013-02-18 06:54:01 +00:00
return cls(**fields)
def succeeds(self, other):
return self.id == other.id and self.version > other.version
def ulong(text):
n = long(text)
if n >= 0:
return n
raise ValueError('invalid literal for ulong(): %s' % (text,))
def str_nonempty(text):
s = str(text)
if len(s) > 0:
return s
raise ValueError('invalid literal for str_nonempty(): %s' % (text,))
2013-02-15 09:16:16 +00:00
def time_ms(text):
try:
return long(text)
except ValueError:
pass
raise ValueError('invalid literal for time_ms(): %r' % (text,))
def datetime_ms(text):
try:
ms = time_ms(text)
return datetime.fromtimestamp(ms / 1000).replace(microsecond= ms % 1000)
except ValueError:
pass
raise ValueError('invalid literal for datetime_ms(): %r' % (text,))
def subscriber_id(text):
try:
if len(text) == 64:
2013-02-18 06:54:01 +00:00
return '%064X' % long(text, 16)
2013-02-15 09:16:16 +00:00
except ValueError:
pass
raise ValueError('invalid literal for subscriber_id(): %r' % (text,))
2013-02-18 06:54:01 +00:00
def bundle_id(text):
2013-02-15 09:16:16 +00:00
try:
if len(text) == 64:
2013-02-18 06:54:01 +00:00
return '%064X' % long(text, 16)
2013-02-15 09:16:16 +00:00
except ValueError:
pass
2013-02-18 06:54:01 +00:00
raise ValueError('invalid literal for bundle_id(): %r' % (text,))
2013-02-15 09:16:16 +00:00
2013-02-18 06:54:01 +00:00
def file_hash(text):
2013-02-15 09:16:16 +00:00
try:
if len(text) == 128:
2013-02-18 06:54:01 +00:00
return '%0128X' % long(text, 16)
2013-02-15 09:16:16 +00:00
except ValueError:
pass
2013-02-18 06:54:01 +00:00
raise ValueError('invalid literal for file_hash(): %r' % (text,))
2013-02-15 09:16:16 +00:00
class ServaldInterfaceException(Exception):
2013-02-20 07:05:18 +00:00
def __init__(self, servald, status, output, msg):
2013-02-22 03:51:58 +00:00
super(ServaldInterfaceException, self).__init__('%s (exit status %d): %s' % (servald, status, msg))
2013-02-20 07:05:18 +00:00
self.status = status
2013-02-15 09:16:16 +00:00
self.output = output
self.servald = servald
class RhizomeListEntry(object):
def __init__(self, **kwargs):
self.__dict__.update(kwargs)
def __repr__(self):
return '%s(%s)' % (self.__class__.__name__, ', '.join('%s=%r' % i for i in self.__dict__.iteritems()))
def servald_rhizome_list(opts):
args = ['rhizome', 'list', 'file']
2013-02-20 07:05:18 +00:00
try:
status, words = invoke_servald(opts, args, output_words=True)
except ServaldInterfaceException, e:
error(e)
return None
2013-02-15 09:16:16 +00:00
if words is None:
return None
try:
if len(words) < 1:
raise ValueError('missing first word')
ncols = int(words[0])
if len(words) < 1 + ncols:
raise ValueError('missing column header')
if (len(words) - (1 + ncols)) % ncols != 0:
raise ValueError('incomplete row')
colmap = {}
for col, hdr in enumerate(words[1:ncols+1]):
colmap[col] = re.sub(r'[^A-Za-z0-9_]', '_', hdr)
rows = []
for i in xrange(ncols + 1, len(words), ncols):
ent = RhizomeListEntry()
for col in xrange(ncols):
setattr(ent, colmap[col], words[i + col])
rows.append(ent)
return rows
except ValueError, e:
error('invalid output from %s: %s' % (' '.join([opts.servald,] + args), e))
return None
def servald_rhizome_hash(opts, path):
args = ['rhizome', 'hash', 'file', path]
2013-02-20 07:05:18 +00:00
try:
status, out = invoke_servald(opts, args)
except ServaldInterfaceException, e:
error(e)
return None
2013-02-15 09:16:16 +00:00
if out is None:
return None
2013-02-18 06:54:01 +00:00
if out.endswith('\n'):
out = out[:-1]
2013-02-15 09:16:16 +00:00
try:
2013-02-18 06:54:01 +00:00
return file_hash(out)
2013-02-15 09:16:16 +00:00
except ValueError:
2013-02-20 07:05:18 +00:00
raise ServaldInterfaceException(opts.servald, status, out, 'invalid output, not a hex file hash')
2013-02-15 09:16:16 +00:00
def servald_rhizome_extract(opts, bid, manifest_path=None, payload_path=None):
args = None
if payload_path and manifest_path:
args = ['rhizome', 'extract', 'bundle', bid, manifest_path, payload_path]
elif payload_path:
args = ['rhizome', 'extract', 'file', bid, payload_path]
elif manifest_path:
args = ['rhizome', 'extract', 'manifest', bid, manifest_path]
if not args:
return None
2013-02-20 07:05:18 +00:00
try:
status, out = invoke_servald(opts, args, output_keyvalue=True)
except ServaldInterfaceException, e:
error(e)
return None
return status == 0
2013-02-15 09:16:16 +00:00
def invoke_servald(opts, args, output_keyvalue=False, output_words=False):
env = dict(os.environ)
if output_words or output_keyvalue:
delim = '\x01'
env['SERVALD_OUTPUT_DELIMITER'] = delim
env['SERVALD_INSTANCEPATH'] = opts.instancepath
try:
allargs = (opts.servald,) + tuple(args)
log('execute ' + ' '.join(map(repr, allargs)))
proc = subprocess.Popen(allargs,
stdout= subprocess.PIPE,
stderr= subprocess.PIPE,
env= env,
)
out, err = proc.communicate()
except OSError, e:
error('cannot execute %s - %s' % (executable, e))
2013-02-20 07:05:18 +00:00
return None, None
if proc.returncode == 255:
allargs = (os.path.basename(opts.servald),) + tuple(args)
2013-02-15 09:16:16 +00:00
for line in err.split('\n'):
if line.startswith('ERROR:') or line.startswith('WARN:'):
error(re.sub(r'^(ERROR|WARN):\s*(\[\d+])?\s*\d\d\:\d\d\:\d\d\.\d+\s*', '', line))
2013-02-22 03:51:58 +00:00
raise ServaldInterfaceException(opts.servald, proc.returncode, None, 'exited with error')
2013-02-20 07:05:18 +00:00
if out is not None and (output_words or output_keyvalue):
2013-02-15 09:16:16 +00:00
if not out.endswith(delim):
2013-02-20 07:05:18 +00:00
raise ServaldInterfaceException(opts.servald, proc.returncode, out, 'missing delimiter')
2013-02-15 09:16:16 +00:00
out = out[:-1]
words = out.split(delim)
if output_keyvalue:
keyvalue = {}
if len(words) % 2 != 0:
2013-02-20 07:05:18 +00:00
raise ServaldInterfaceException(opts.servald, proc.returncode, out, 'odd number of output fields')
2013-02-15 09:16:16 +00:00
while words:
key = words.pop(0)
value = words.pop(0)
keyvalue[key] = value
2013-02-20 07:05:18 +00:00
out= keyvalue
else:
out = words
return proc.returncode, out
2013-02-15 09:16:16 +00:00
2013-02-21 12:12:21 +00:00
log_output = None
2013-02-15 09:16:16 +00:00
def log(msg):
2013-02-21 12:12:21 +00:00
if log_output:
print >>log_output, '+ %s' % (msg,)
2013-02-15 09:16:16 +00:00
def error(msg):
print >>sys.stderr, '%s: %s' % (os.path.basename(sys.argv[0]), msg)
def fatal(msg):
error(msg)
sys.exit(1)
if __name__ == '__main__':
main()