def main(): # build the args we support. start = datetime.datetime.now() all_args = {} for n, v in globals().iteritems(): # explicit check for functions so twisted classes don't match.. if type(v) == type(main) and getattr(v, "__doc__", None): all_args[n.replace("_", "-")] = v all_arg_names = sorted(all_args.keys()) description = ( __doc__ + "\nCommands\n help\n " + "\n ".join(all_args.keys()) + "\nUse '%prog help command-name' for help on a specific command.\n" ) parser = optparse.OptionParser("%prog [options]", description=description, formatter=HelpFormatter()) for opt in opts.get_program_options(): parser.add_option(opt) for opt in opts.get_request_options(): parser.add_option(opt) options, args = parser.parse_args() opts.setup_logging(options) init_config(options.config) proto.init_protocols() # patch up keys. if options.keys: def _fix_unicode(result): if isinstance(result, list): for i, v in enumerate(result): result[i] = _fix_unicode(result[i]) elif isinstance(result, unicode): result = result.encode("utf-8") return result for i, val in enumerate(options.keys): try: # help windows - replace single quote with double... val = val.replace("'", '"') options.keys[i] = _fix_unicode(json.loads(val)) except ValueError, why: parser.error("Invalid key value %r: %s" % (val, why))
def go(_): try: _ = yield init_config() dm = get_doc_model() _ = yield make_graph(dm) finally: print "stopping" reactor.stop()
def check_couch(): if sys.version_info < (2,6): import simplejson as json else: import json from raindrop.config import init_config config = init_config() couch = config.couches['local'] url = "http://%(host)s:%(port)s/" % couch try: info = json.load(urllib2.urlopen(url)) except IOError, why: fail("Can't connect to couchdb: %s", why)
def main(): # for now, so we know the couch defaults cfg = config.init_config().couches['local'] parser = optparse.OptionParser("%prog [options]", description="the raindrop api runner") parser.add_option("", "--couchdb-host", help="Specifies the hostname to use for couchdb requests", default=cfg['host']) parser.add_option("", "--couchdb-port", help="Specifies the port number to use for couchdb requests", default=cfg['port']) parser.add_option("", "--show-perf", action="store_true", help="Reports some information about how long some things take") options, args = parser.parse_args() # no args used if len(args) != 0: print >> sys.stderr, __doc__ print >> sys.stderr, "this program takes no arguments" sys.exit(1) if options.show_perf: global note_timeline note_timeline = do_note_timeline CouchDB.default_host = options.couchdb_host CouchDB.default_port = options.couchdb_port log("raindrops keep falling on my api...") # and now the main loop. while True: line = sys.stdin.readline() if not line: break req = json.loads(line) resp = process(req, options) json.dump(resp, sys.stdout) sys.stdout.write("\n") sys.stdout.flush() log("raindrop api runner terminating normally")
def main(): # no options or args now! if len(sys.argv) != 1: print >> sys.stderr, __doc__ print >> sys.stderr, "this program takes no arguments" sys.exit(1) # for now, so we know the couch. cfg = config.init_config().couches['local'] log("raindrops keep falling on my api...") # and now the main loop. while True: line = sys.stdin.readline() if not line: break req = json.loads(line) # handle 'special' requests' if len(req['path'])==3 and req['path'][-1] == '_reset': msg = '%d document(s) dropped' % len(_handlers) resp = {'code': 200, 'json': {'info': msg}} _handlers.clear() elif len(req['path'])==3 and req['path'][-1] == '_exit': json.dump({'code': 200, 'json': {'info': 'goodbye'}}, sys.stdout) sys.stdout.write("\n") sys.stdout.flush() sys.exit(0) else: try: handler = get_api_handler(cfg, req) except APILoadError, exc: log("%s", exc.msg) resp = {'code': 400, 'json': {'error': 400, 'reason': exc.msg}} else: # call the API handler - we expect the API to be robust and # catch mostexceptions, so if we see one, we just die. note_timeline("api request: %s", req['path']) resp = handler(req) note_timeline("api back")
def main(): descs = [] for n, v in globals().iteritems(): if callable(v) and getattr(v, '__doc__', None) and v.__module__==__name__: all_commands[n.replace('_', '-')] = v descs.append("%s:" % n) descs.append(" %s" % v.__doc__) description = '\n'.join([__doc__, 'Commands:', '']+descs) + '\n' + \ get_request_opts_help() parser = optparse.OptionParser("%prog [options]", description=description, formatter=HelpFormatter()) for opt in opts.get_program_options(): parser.add_option(opt) # For testing... parser.add_option("", "--one", action="store_true", help="Only process one request and exit; accepts multi-line " "input and emits multi-line, pretty-printed output.") options, args = parser.parse_args() if args: # later may accept input filenames for testing? parser.error("this program accepts no args") opts.setup_logging(options) config.init_config() proto.init_protocols() logger.info("raindrops keep falling on my couch...") # create an initial deferred to perform tasks which must occur before we # can start. The final callback added will fire up the real servers. d = defer.Deferred() # XXX - this is suspect - we need to better rationilize 'options' handling d.addCallback(lambda _: _init({'query': {}})) def start_stdio(whateva): # using twisted's stdio is just way too hard on windows - just use a # dedicated thread... if options.one: target = stdio_one else: target = stdio_interact t = threading.Thread(target=target) t.start() d.addCallback(start_stdio) def done(whateva): pass def error(failure): # XXX - this is wrong - couch wants a "better" response with some # real errors - but at least this "works" in so far as couch reports # the incorrect output and continues on correctly... log_twisted_failure(failure, "unhandled top-level error") resp = {"error": failure.getErrorMessage() + "\n" + failure.getTraceback()} sys.stdout.write(json.dumps(resp) + "\n") sys.stdout.flush() d.addCallbacks(done, error) reactor.callWhenRunning(d.callback, None) reactor.run()