def validate(options): if not options.address: print 'Address must be specified' parser.print_help() sys.exit(1) if not options.target: print 'Target must be specified' parser.print_help() sys.exit(1) if '.' not in options.target: print 'Target must be: <class>.<method>' parser.print_help() sys.exit(1) if options.ttl: try: int(options.ttl) except ValueError: print 'TTL must be <int>' parser.print_help() sys.exit(1) if options.wait: try: int(options.wait) except ValueError: print 'Wait must be <int>' parser.print_help() sys.exit(1) if options.data: try: json.loads(options.data) except ValueError: print 'data must be valid json' parser.print_help() sys.exit(1) if options.input: try: document = json.loads(options.input) if not isinstance(document, list): raise ValueError() if len(document) != 2: raise ValueError() if not isinstance(document[0], list): raise ValueError() if not isinstance(document[1], dict): raise ValueError() except ValueError, e: print utf8(e) print 'Input must be valid json: [[], {}]' parser.print_help() sys.exit(1)
def run(self, *command): """ Run the specified command. :param command: A command and parameters. :type command: tuple :return: (status, {stdout:<str>, stderr:<str>}) :rtype: tuple """ details = { STDOUT: '', STDERR: '', } result = { STDOUT: '', STDERR: '', } context = Context.current() p = Popen(command, stdout=PIPE, stderr=PIPE) try: while True: n_read = 0 if context.cancelled(): p.terminate() break for fp, key in ((p.stdout, STDOUT), (p.stderr, STDERR)): line = fp.readline() if line: n_read += len(line) details[key] = line result[key] += line self.report(details) if not n_read: # EOF break p.stdout.close() p.stderr.close() status = p.wait() return status, result except OSError, e: return -1, utf8(e)
def __call__(self, pipe): """ Perform RMI on the child-side of the forked call as follows: - Reset the RMI context. - Invoke the method - Send result: retval, progress, raised exception. All output is sent to the parent using the inter-process pipe. :param pipe: A message pipe. :type pipe: multiprocessing.Connection """ try: context = Context.current() context.cancelled = lambda: False context.progress = Progress(pipe) result = self.method(*self.args, **self.kwargs) reply = protocol.Result(result) reply.send(pipe) except Exception, e: log.exception(utf8(e)) reply = protocol.Raised(e) reply.send(pipe)
def __call__(self, pipe): """ Perform RMI on the child-side of the forked call as follows: - Reset the RMI context. - Invoke the method - Send result: retval, progress, raised exception. All output is sent to the parent using the inter-process pipe. :param pipe: A message pipe. :type pipe: gofer.mp.Writer """ try: context = Context.current() context.cancelled = lambda: False context.progress = Progress(pipe) result = self.method(*self.args, **self.kwargs) reply = protocol.Result(result) reply.send(pipe) except Exception, e: log.exception(utf8(e)) reply = protocol.Raised(e) reply.send(pipe)
def _load(): """ Load the adapters and return a list and catalog. :return: A tuple of (list, dict) :rtype: tuple """ _list = [] catalog = {} _dir = os.path.dirname(__file__) for name in sorted(os.listdir(_dir)): package = '.'.join((PACKAGE, name)) path = os.path.join(_dir, name) if not os.path.isdir(path): continue try: pkg = __import__(package, {}, {}, REQUIRED) _list.append(pkg) catalog[name] = pkg catalog[package] = pkg for capability in pkg.PROVIDES: catalog[capability] = pkg except (ImportError, AttributeError), e: log.warn('Import: %s, failed: %s', package, utf8(e))
def __str__(self): return utf8(self)