diff options
Diffstat (limited to 'rpmci/subtask.py')
-rw-r--r-- | rpmci/subtask.py | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/rpmci/subtask.py b/rpmci/subtask.py new file mode 100644 index 0000000..e97279e --- /dev/null +++ b/rpmci/subtask.py @@ -0,0 +1,69 @@ +# subprocess_msg.py: +# Wrapper for creating processes that logs their stderr to +# a given directory, rotating earlier logs. +# +# Licensed under the new-BSD license (http://www.opensource.org/licenses/bsd-license.php) +# Copyright (C) 2010 Red Hat, Inc. +# Written by Colin Walters <walters@verbum.org> + +import os +import shutil +import logging +import time + +import subprocess +import async_subprocess + +_base_path = None +_failed_path = None +_old_path = None +def global_configure(config): + global _base_path + global _failed_path + global _old_path + _base_path = config.get('subtask', 'subtask_dir') + _failed_path = os.path.join(_base_path, 'failed') + _old_path = os.path.join(_base_path, 'old') + if not os.path.isdir(_failed_path): + os.makedirs(_failed_path) + if not os.path.isdir(_old_path): + os.makedirs(_old_path) + +def prepare_task_logfile(taskid): + log_path = os.path.join(_base_path, '%s.log' % (taskid, )) + if os.path.isfile(log_path): + curtime = int(time.time()) + saved_name = '%s-%d.log' % (taskid, int(time.time()),) + os.rename(log_path, os.path.join(_old_path, saved_name)) + return log_path + +def _init_task_run(taskid, argv, cwd): + log_path = prepare_task_logfile(taskid) + logging.info("Running task %r synchronously, cwd=%r args=%r" % (taskid, cwd, argv)) + return log_path + +def spawn_sync(taskid, argv, cwd=None): + log_path = _init_task_run(taskid, argv, cwd) + f = open(log_path, 'w') + nullf = open(os.devnull, 'w') + try: + subprocess.check_call(argv, cwd=cwd, stdin=nullf, stdout=f, stderr=f) + except subprocess.CalledProcessError, e: + f.close() + shutil.move(log_path, _failed_path) + raise e + f.close() + +def spawn_sync_get_output(taskid, argv, cwd=None): + log_path = _init_task_run(taskid, argv, cwd) + f = open(log_path, 'w') + nullf = open(os.devnull, 'w') + try: + proc = subprocess.Popen(argv, cwd=cwd, stdin=nullf, stdout=subprocess.PIPE, stderr=f) + output = proc.communicate()[0] + except subprocess.CalledProcessError, e: + f.close() + shutil.move(log_path, _failed_path) + raise e + f.close() + return output |