summaryrefslogtreecommitdiff
path: root/tests/automated/configure.py
blob: 8be437ffe4f2f7bad806f25ccfe50f09fe1226e2 (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
#!/usr/bin/python

import optparse
import os
import platform
import socket
import subprocess
import sys

PROG = "r" + "$Rev: 17 $".strip("$ ").replace("Rev: ", "")

#
# Get gcc version
#
def gcc_version(gcc):
    proc = subprocess.Popen(gcc + " -v", stdout=subprocess.PIPE,
                            stderr=subprocess.STDOUT, shell=True)
    ver = ""
    while True:
        s = proc.stdout.readline()
        if not s:
            break
        if s.find("gcc version") >= 0:
            ver = s.split(None, 3)[2]
            break
    proc.wait()
    return "gcc-" + ver

def replace_vars(text):
        while True:
                if text.find("$(PJSUA-TESTS)") >= 0:
                        proc = subprocess.Popen("python runall.py --list-xml", cwd="../pjsua",
                                                shell=True, stdout=subprocess.PIPE)
                        content = proc.stdout.read()
                        text = text.replace("$(PJSUA-TESTS)", content)
                elif text.find("$(GCC)") >= 0:
                        text = text.replace("$(GCC)", gcc_version("gcc"))
                elif text.find("$(DISABLED)") >= 0:
                        text = text.replace("$(DISABLED)", "0")
                elif text.find("$(OS)") >= 0:
                        os_info = platform.system() + platform.release() + "-" + platform.machine()
                        if platform.system().lower() == "linux":
                                os_info =  os_info + "-" + "-".join(platform.linux_distribution()[0:2])
                        text = text.replace("$OS", os_info)
                elif text.find("$(SUFFIX)") >= 0:
                        proc = subprocess.Popen("sh config.guess", cwd="../..",
                                                shell=True, stdout=subprocess.PIPE)
                        plat = proc.stdout.readline().rstrip(" \r\n")
                        text = text.replace("$(SUFFIX)", plat)
                elif text.find("$(HOSTNAME)") >= 0:
                        text = text.replace("$(HOSTNAME)", socket.gethostname())
                elif text.find("$(PJDIR)") >= 0:
                        wdir = os.path.join(os.getcwd(), "../..")
                        wdir = os.path.normpath(wdir)
                        text = text.replace("$(PJDIR)", wdir)
                else:
                        break
        return text


def main(args):
        usage = """Usage: configure.py scenario_template_file
"""

        args.pop(0)
        if not len(args):
                print usage
                return 1
        
        tpl_file = args[len(args)-1]
        if not os.path.isfile(tpl_file):
                print "Error: unable to find template file '%s'" % (tpl_file)
                return 1
                
        f = open(tpl_file, "r")
        tpl = f.read()
        f.close()
        
        tpl = replace_vars(tpl)
        print tpl
        return 0


if __name__ == "__main__":
    rc = main(sys.argv)
    sys.exit(rc)