def try_starting_mongo(settings): """ If the `mongo.run` configuration option is set, try to start mongod """ if not settings.get("mongo.run", None): raise MongoStartFailure( "Couldn't connect to mongodb, not attempting to start mongod " "because mongo.run not set") # Hmm, maybe we should try and start it # Do we have a mongo configured? mongo_path = settings.get("mongo.path", None) if mongo_path: if not exists(mongo_path): raise RuntimeError("Invalid mongo.path specified in configuration") if not start_mongo(pjoin(mongo_path, "mongod"), settings): return False our_mongo = pjoin(sys.prefix, "bin", "mongod") log.info("Our mongo would be located at: {0}".format(our_mongo)) if exists(our_mongo): # There is one installed in our prefix, try that first if start_mongo(our_mongo, settings): return True # Prefix didn't work, let's try our PATH. return start_mongo("mongod", settings)
def content(self): # TODO(pwaller): Remove defunct raise/parentage if self.format == "raise": class UserThrow(RuntimeError): pass raise UserThrow("Stopping because you asked me to.") log.debug("Rendering {0} from {1}".format(self.format, self)) params = self.params params.update(self.request.params) resolution = int(params.get("resolution", self.RESOLUTION_DEFAULT)) resolution = min(resolution, self.RESOLUTION_MAX) rootformat = "eps" if self.format == "pdf": rootformat = "pdf" with NamedTemporaryFile(suffix="." + rootformat) as tmpfile: with self.canvas as canvas: self.configure_canvas(params, canvas) self.render(canvas) canvas.Update() try: canvas.SaveAs(tmpfile.name) except ROOTError as err: if "illegal number of points" in err.msg: log.warning('problem plotting canvas "%s", error from ROOT "%s"', canvas.GetName(), err.msg) else: raise log.info("RENDERING {0} -- {1}".format(self.format, rootformat)) if self.format == rootformat: # No conversion necessary, ROOT did it directly. # grab the file from disk with open(tmpfile.name) as eps_fd: content = eps_fd.read() else: # convert_eps releases the GIL by doing the work off-process. # This is where the speed comes from. content = convert_eps(tmpfile.name, resolution, self.format) # print "Made EPS: {0:5.2f} content = {1:5.2f}".format(len(epsdata) / # 1024., len(content) / 1024.) extra_args = {} if "attach" in params: log.error("Attaching rendered image") # Give the file a sensible name, rather than the last fragment # of the URL which was visited. extra_args.update(content_disposition=( "Content-Disposition: attachment; filename={0};" .format(self.filename))) return Response(content, content_type="image/{0}".format(self.format), **extra_args)
def draw_ttree(t, params, what): #test_entries = 1000 #every_n_events = t.GetEntries() // test_entries #selection = "Entry$ % {0} == 0".format(every_n_events) drawn = t.Draw(what, "1", "goff") #, test_entries) h = t.GetHistogram() log.info("Drawn {0} with {1} entries".format(what, drawn)) return h
def draw_ttree(t, params, what): # test_entries = 1000 # every_n_events = t.GetEntries() // test_entries # selection = "Entry$ % {0} == 0".format(every_n_events) drawn = t.Draw(what, "1", "goff") # , test_entries) h = t.GetHistogram() log.info("Drawn {0} with {1} entries".format(what, drawn)) return h
def start_mongo(bin, settings): """ Attempts to start a mongod process located at bin (or searches the path). Returns True if mongod appears to be working correctly, False if it had a problem. Registers an atexit handler which kills mongod. """ log.getChild("start_mongo").info("Starting {0}".format(bin)) args = [] for item, value in settings.iteritems(): if item.startswith("mongo.args."): args.extend(["--" + item[len("mongo.args."):], value]) dbpath = settings.get("mongo.dbpath", None) # args.extend(("--dbpath", "\"%s\"" % dbpath)) if dbpath and not exists(dbpath): makedirs(dbpath) # TODO: cStringIO this output, send it to a different logger mongo_logger = PythonizeMongoOutput(log.manager.getLogger("mongod")) try: log.info("Mongo args: {0}".format(args)) mongo_process = spawn(bin, args, logfile=mongo_logger, timeout=None) except ExceptionPexpect as e: if not e.value.startswith("The command was not found"): # Something weird happened that we don't know how to deal with raise # It doesn't exist return False @atexit.register def kill_mongo(): log.info("Killing MongoDB") mongo_process.kill(SIGKILL) log.info("Waiting for mongo startup (can be slow the first time)") possibilities = [EOF, "exception", "waiting for connections"] GOOD_STATE = len(possibilities) - 1 index = mongo_process.expect(possibilities) if index != GOOD_STATE: log.info("Mongod didn't reach '{0}' state".format( possibilities[GOOD_STATE])) log.info(" -- instead is '{0}'".format(possibilities[index])) mongo_process.kill(SIGKILL) return False return True
def start_mongo(bin, settings): """ Attempts to start a mongod process located at bin (or searches the path). Returns True if mongod appears to be working correctly, False if it had a problem. Registers an atexit handler which kills mongod. """ log.getChild("start_mongo").info("Starting {0}".format(bin)) args = [] for item, value in settings.iteritems(): if item.startswith("mongo.args."): args.extend(["--" + item[len("mongo.args."):], value]) dbpath = settings.get("mongo.dbpath", None) # args.extend(("--dbpath", "\"%s\"" % dbpath)) if dbpath and not exists(dbpath): makedirs(dbpath) # TODO: cStringIO this output, send it to a different logger mongo_logger = PythonizeMongoOutput(log.manager.getLogger("mongod")) try: log.info("Mongo args: {0}".format(args)) mongo_process = spawn(bin, args, logfile=mongo_logger, timeout=None) except ExceptionPexpect as e: if not e.value.startswith("The command was not found"): # Something weird happened that we don't know how to deal with raise # It doesn't exist return False @atexit.register def kill_mongo(): log.info("Killing MongoDB") mongo_process.kill(SIGKILL) log.info("Waiting for mongo startup (can be slow the first time)") possibilities = [EOF, "exception", "waiting for connections"] GOOD_STATE = len(possibilities) - 1 index = mongo_process.expect(possibilities) if index != GOOD_STATE: log.info("Mongod didn't reach '{0}' state".format(possibilities[GOOD_STATE])) log.info(" -- instead is '{0}'".format(possibilities[index])) mongo_process.kill(SIGKILL) return False return True
def kill_mongo(): log.info("Killing MongoDB") mongo_process.kill(SIGKILL)
def content(self): # TODO(pwaller): Remove defunct raise/parentage if self.format == "raise": class UserThrow(RuntimeError): pass raise UserThrow("Stopping because you asked me to.") log.debug("Rendering {0} from {1}".format(self.format, self)) params = self.params params.update(self.request.params) resolution = int(params.get("resolution", self.RESOLUTION_DEFAULT)) resolution = min(resolution, self.RESOLUTION_MAX) rootformat = "eps" if self.format == "pdf": rootformat = "pdf" with NamedTemporaryFile(suffix="." + rootformat) as tmpfile: with self.canvas as canvas: self.configure_canvas(params, canvas) self.render(canvas) canvas.Update() try: canvas.SaveAs(tmpfile.name) except ROOTError as err: if "illegal number of points" in err.msg: log.warning( 'problem plotting canvas "%s", error from ROOT "%s"', canvas.GetName(), err.msg) else: raise log.info("RENDERING {0} -- {1}".format(self.format, rootformat)) if self.format == rootformat: # No conversion necessary, ROOT did it directly. # grab the file from disk with open(tmpfile.name) as eps_fd: content = eps_fd.read() else: # convert_eps releases the GIL by doing the work off-process. # This is where the speed comes from. content = convert_eps(tmpfile.name, resolution, self.format) # print "Made EPS: {0:5.2f} content = {1:5.2f}".format(len(epsdata) / # 1024., len(content) / 1024.) extra_args = {} if "attach" in params: log.error("Attaching rendered image") # Give the file a sensible name, rather than the last fragment # of the URL which was visited. extra_args.update(content_disposition=( "Content-Disposition: attachment; filename={0};".format( self.filename))) return Response(content, content_type="image/{0}".format(self.format), **extra_args)