aboutsummaryrefslogtreecommitdiff
path: root/node/prepare_build_config.py
blob: 30864533bc39ad0d7af56d966b0d82ad9082d59f (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
#!/usr/bin/env python
import sys
import os
import base64
import re
import pipes


SLAVE_TYPE_FILE = "/var/run/build-tools/slave-type"
SLAVE_TYPE_RESTRICTED = "Natty Release 64bit Instance Store - private builds"
# sf-safe build config is written to this file
BUILD_CONFIG_FILE = "/var/run/build-tools/build-config"


class BuildConfigMismatchException(Exception):
    pass

def shell_unquote(s):
    # Primitive implementation, we should
    if s[0] == '"' and s[-1] == '"':
        return s[1:-1]
    if s[0] == "'" and s[-1] == "'":
        return s[1:-1]
    return s

def get_slave_type():
    slave_type = ""
    try:
        slave_type = open(SLAVE_TYPE_FILE).read()
    except:
        pass
    return slave_type

def validate_config(config, slave_type):
    """Validate various constraints on the config params and overall
    environment."""
    full_job_name = os.environ.get("JOB_NAME")
    owner, job_subname = full_job_name.split("_", 1)

    # Deduce parameter "categories" which we can directly match against each other
    if slave_type == SLAVE_TYPE_RESTRICTED:
        slave_type_cat = "restricted"
    else:
        slave_type_cat = "normal"

    if owner in ["linaro-android-private", "linaro-android-restricted"]:
        owner_cat = "restricted"
    else:
        owner_cat = "normal"

    if config.get("BUILD_TYPE", "build-android") in ["build-android-private", "build-android-restricted"]:
        build_type_cat = "restricted"
    else:
        build_type_cat = "normal"

    # Now, process few most expected mismatches in adhox way, to provide better error messages
    if slave_type_cat == "restricted" and owner_cat != "restricted":
        raise BuildConfigMismatchException("Only jobs owned by ~linaro-android-restricted may run on this build slave type")

    if owner_cat == "restricted" and build_type_cat != "restricted":
        raise BuildConfigMismatchException("Jobs owned by ~linaro-android-restricted must use BUILD_TYPE=build-android-restricted")

    # Finally, generic mismatch detection
    if slave_type_cat != owner_cat or slave_type_cat != build_type_cat:
        raise BuildConfigMismatchException("Incompatible slave type, job owner and build type")


def convert_config_to_shell(config_text):
    config = {}
    out = open(BUILD_CONFIG_FILE, "w")

    for l in cfg.split("\n"):
        l = l.strip()
        if not l or l[0] == "#":
            continue
        if not re.match(r"[A-Za-z][A-Za-z0-9_]*=", l):
            print "Invalid build config syntax: " + l
            sys.exit(1)
        var, val = l.split("=", 1)
        val = shell_unquote(val)
        config[var] = val
        print "%s=%s" % (var, pipes.quote(val))
    out.close()


if __name__ == "__main__":
    config_text = base64.b64decode(sys.argv[1])
    config = convert_config_to_shell(config_text, get_slave_type())
    try:
        validate_config(config)
    except BuildConfigMismatchException, e:
        print str(e)
        sys.exit(1)