summaryrefslogtreecommitdiffstats
path: root/scripts/updateallchanges.py
blob: e5d81c8c2d0cdefb5ae682852088c92be9f9ca0c (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/usr/bin/env python

"""
This script invokes updatechanges.py for all host/platform/branch combinations.
"""

import sys
from subprocess import Popen, PIPE
from dbaccess import setDatabase, execQuery, commit
from misc import getOptions, idToText


# --- BEGIN Global functions ----------------------------------------------

def printUsage():
    sys.stderr.write(
        "usage: " + sys.argv[0] + " --help | [--dbhost H --dbport P] --db D\n")


def printVerboseUsage():
    printUsage()
    sys.stderr.write("\noptions:\n")
    sys.stderr.write(
        "    --help: This help.\n")
    sys.stderr.write(
        "  --dbhost: The database server host (overriding the default).\n")
    sys.stderr.write(
        "  --dbport: The database server port (overriding the default).\n")
    sys.stderr.write(
        "      --db: The database. One of 'bm' or 'bm-dev' (the latter "
        "intended for experimentation).\n")


# Executes the external updatechanges.py script with appropriate arguments.
def execUpdateChanges(host, platform, branch, options):

    cmd = [
        "updatechanges.py", "--db", options["db"], "--host", host,
        "--platform", platform, "--branch", branch, "--noprogress", "true"]
    if "dbhost" in options:
        cmd += ["--dbhost", options["dbhost"]]
    if "dbport" in options:
        cmd += ["--dbport", options["dbport"]]

    sys.stdout.write(
        "\nupdating changes for " + host + " / " + platform + " / " + branch +
        " ...\n")
    sys.stdout.flush()

    p = Popen(cmd, stdout = PIPE, stderr = PIPE)
    stdout, stderr = p.communicate()
    if (p.returncode != 0):
        sys.stdout.write("failed to execute command '" + str(cmd) + "':\n")
        sys.stdout.write("  return code: " + str(p.returncode) + "\n")
        sys.stdout.write("  stdout: >" + stdout.strip() + "<\n")
        sys.stdout.write("  stderr: >" + stderr.strip() + "<\n")
    else:
        sys.stdout.write("updatechanges.py executed successfully:\n")
        sys.stdout.write("  return code: " + str(p.returncode) + "\n")
        sys.stdout.write("  stdout: >" + stdout.strip() + "<\n")
        sys.stdout.write("  stderr: >" + stderr.strip() + "<\n")

# --- END Global functions ----------------------------------------------


# --- BEGIN Main program ----------------------------------------------

options, http_get = getOptions()

if "help" in options:
    printVerboseUsage()
    sys.exit(1)

if not ("db" in options):
    printUsage()
    sys.exit(1)

setDatabase(
    options["dbhost"] if "dbhost" in options else None,
    options["dbport"] if "dbport" in options else None,
    options["db"])

hpb_ids = execQuery(
    "SELECT DISTINCT hostId, platformId, branchId FROM context "
    "ORDER BY hostId, platformId, branchId", ())

for host_id, platform_id, branch_id in hpb_ids:
    execUpdateChanges(
        idToText("host", host_id),
        idToText("platform", platform_id),
        idToText("branch", branch_id),
        options)

sys.exit(0)

# --- END Main program ----------------------------------------------