Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/devbot/command.py
blob: 0d9c809ea79cf6a3d13a8d5fee96455ec589ea96 (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
import subprocess
import time

from devbot import config


def run(args, test=False, retry=0):
    if test:
        print " ".join(args)
        return

    log_file = None
    subprocess_args = {"args": args}

    if config.log_path:
        log_file = open(config.log_path, "a")
        subprocess_args["stdout"] = log_file
        subprocess_args["stderr"] = subprocess.STDOUT

    tries = 0
    while tries < retry + 1:
        try:
            tries = tries + 1
            subprocess.check_call(**subprocess_args)
            break
        except subprocess.CalledProcessError, e:
            print "\nCommand failed, tail of %s\n" % config.log_path
            if config.log_path:
                subprocess.call(["tail", config.log_path])

            if tries < retry + 1:
                print "Retrying (attempt %d) in 1 minute" % tries
                time.sleep(60)
            else:
                raise e

    if log_file:
        log_file.close()


def run_with_sudo(args, test=False, retry=0):
    args_with_sudo = ["sudo"]
    args_with_sudo.extend(args)

    print " ".join(args_with_sudo)

    run(args_with_sudo, test=test, retry=retry)