summaryrefslogtreecommitdiffstats
path: root/rpmci/subtask.py
blob: e97279eb117e8eff4c20d794649bd0f347538b42 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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