def executePolicyDir(func, d, prefix=None): """ Execute all .py files in a directory, in alphabetical order, calling func on the module This throws a TryError if any of the modules fail. The result is a list of tuples consisting of the module name and str rep of the exception """ ## # a bit cheap but we want to temporarily make this direcotry in our path if it isn't already # so safe out sys.path, add d to it, and put it back when done oldpath = sys.path sys.path = [d] + sys.path path = d if prefix: path = os.path.join(path, prefix) files = [f[:-3] for f in os.listdir(path) if f.endswith('.py') and f != '__init__.py'] files.sort() errorRes = [] try: for f in files: if prefix: f = prefix + '.' + f try: m = namedModule(f) func(m) except Exception, err: errorRes.append((f, str(err), getStacktrace())) finally: sys.path = oldpath if errorRes: raise TryError('Failed to execute all modules', errorRes)
def runTask(taskName, f): """ This takes a task name and a function. It runs the function, if the function does not throw an exception then it loads the task and marks it as completed. If an exception is thrown it loads the task and marks it as failed and logs the exception in the task. runTask will fail if the task does not exist """ try: f() task.updateTask(task.loadTask(taskName).setState(task.TASK_COMPLETED)) except Exception, err: task.updateTask(task.loadTask(taskName ).setState(task.TASK_FAILED ).addException(str(err), err, errors.getStacktrace()))
def runTask(taskName, f): """ This takes a task name and a function. It runs the function, if the function does not throw an exception then it loads the task and marks it as completed. If an exception is thrown it loads the task and marks it as failed and logs the exception in the task. runTask will fail if the task does not exist """ try: f() task.updateTask(task.loadTask(taskName).setState(task.TASK_COMPLETED)) except Exception, err: task.updateTask( task.loadTask(taskName).setState(task.TASK_FAILED).addException( str(err), err, errors.getStacktrace()))
def _(err): errors = pymongo.Connection().clovr.errors runInfo['error_msg'] = str(err) runInfo['timestamp'] = int(time.time()) runInfo['stacktrace'] = errorutils.getStacktrace() errors.insert(runInfo)