summaryrefslogtreecommitdiff
path: root/tests/cdash/builder.py
blob: 424b3c7f24219467392e3dffb9330e312a7e1c24 (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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#
# builder.py - PJSIP test scenarios builder
#
# Copyright (C) 2008-2009 Teluu Inc. (http://www.teluu.com)
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#

import ccdash
import os
import platform
import re
import subprocess
import sys
import time

class Operation:
    """\
    The Operation class describes the individual ccdash operation to be 
    performed.

    """
    # Types:
    UPDATE = "update"           # Update operation
    CONFIGURE = "configure"     # Configure operation
    BUILD = "build"             # Build operation
    TEST = "test"               # Unit test operation

    def __init__(self, type, cmdline, name="", wdir=""):
        self.type = type
        self.cmdline = cmdline
        self.name = name
        self.wdir = wdir
        if self.type==self.TEST and not self.name:
            raise "name required for tests"

    def encode(self, base_dir):
        s = [self.type]
        if self.type == self.TEST:
            s.append(self.name)
        if self.type != self.UPDATE:
            s.append(self.cmdline)
        s.append("-w")
        if self.wdir:
            s.append(base_dir + "/" + self.wdir)
        else:
            s.append(base_dir)
        return s


#
# Update operation
#
update_ops = [Operation(Operation.UPDATE, "")]

#
# The standard library tests (e.g. pjlib-test, pjsip-test, etc.)
#
std_test_ops= [
    Operation(Operation.TEST, "./pjlib-test-$SUFFIX", name="pjlib test",
              wdir="pjlib/bin"),
    Operation(Operation.TEST, "./pjlib-util-test-$SUFFIX", 
              name="pjlib-util test", wdir="pjlib-util/bin"),
    Operation(Operation.TEST, "./pjnath-test-$SUFFIX", name="pjnath test",
              wdir="pjnath/bin"),
    Operation(Operation.TEST, "./pjmedia-test-$SUFFIX", name="pjmedia test",
              wdir="pjmedia/bin"),
    Operation(Operation.TEST, "./pjsip-test-$SUFFIX", name="pjsip test",
              wdir="pjsip/bin")
]

#
# These are operations to build the software on GNU/Posix systems
#
gnu_build_ops = [
    Operation(Operation.CONFIGURE, "./configure"),
    Operation(Operation.BUILD, "make distclean; make dep && make; cd pjsip-apps/src/python; python setup.py clean build"),
    #Operation(Operation.BUILD, "python setup.py clean build",
    #          wdir="pjsip-apps/src/python")
]

#
# These are pjsua Python based unit test operations
#
def build_pjsua_test_ops():
    ops = []
    cwd = os.getcwd()
    os.chdir("../pjsua")
    os.system("python runall.py --list > list")
    f = open("list", "r")
    for e in f:
        e = e.rstrip("\r\n ")
        (mod,param) = e.split(None,2)
        name = mod[4:mod.find(".py")] + "_" + \
               param[param.find("/")+1:param.find(".py")]
        ops.append(Operation(Operation.TEST, "python run.py " + e, name=name,
                   wdir="tests/pjsua"))
    os.chdir(cwd)
    return ops

#
# 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

#
# Test config
#
class BaseConfig:
    def __init__(self, base_dir, url, site, group, options=None):
        self.base_dir = base_dir
        self.url = url
        self.site = site
        self.group = group
        self.options = options

#
# Base class for test configurator
#
class TestBuilder:
    def __init__(self, config, build_config_name="",
                 user_mak="", config_site="", exclude=[], not_exclude=[]):
        self.config = config                        # BaseConfig instance
        self.build_config_name = build_config_name  # Optional build suffix
        self.user_mak = user_mak                    # To be put in user.mak
        self.config_site = config_site              # To be put in config_s..
        self.saved_user_mak = ""                    # To restore user.mak
        self.saved_config_site = ""                 # To restore config_s..
        self.exclude = exclude                      # List of exclude pattern
        self.not_exclude = not_exclude              # List of include pattern
        self.ccdash_args = []                       # ccdash cmd line

    def stamp(self):
        return time.strftime("%Y%m%d-%H%M", time.localtime())

    def pre_action(self):
        # Override user.mak
        name = self.config.base_dir + "/user.mak"
        if os.access(name, os.F_OK):
            f = open(name, "r")
            self.saved_user_mak = f.read()
            f.close()
        if True:
            f = open(name, "wt")
            f.write(self.user_mak)
            f.close()
        # Override config_site.h
        name = self.config.base_dir + "/pjlib/include/pj/config_site.h"
        if os.access(name, os.F_OK):
            f = open(name, "r")
            self.saved_config_site= f.read()
            f.close()
        if True:
            f = open(name, "wt")
            f.write(self.config_site)
            f.close()


    def post_action(self):
        # Restore user.mak
        name = self.config.base_dir + "/user.mak"
        if self.saved_user_mak:
            f = open(name, "wt")
            f.write(self.saved_user_mak)
            f.close()
        else:
            os.remove(name)
        # Restore config_site.h
        name = self.config.base_dir + "/pjlib/include/pj/config_site.h"
        if self.saved_config_site:
            f = open(name, "wt")
            f.write(self.saved_config_site)
            f.close()
        else:
            os.remove(name)

    def build_tests(self):
        # This should be overridden by subclasses
        pass

    def execute(self):
        if len(self.ccdash_args)==0:
            self.build_tests()
        self.pre_action()
        counter = 0
        for a in self.ccdash_args:
            # Check if this test is in exclusion list
            fullcmd = " ".join(a)
            excluded = False
            included = False
            for pat in self.exclude:
                if pat and re.search(pat, fullcmd) != None:
                    excluded = True
                    break
            if excluded:
                for pat in self.not_exclude:
                    if pat and re.search(pat, fullcmd) != None:
                        included = True
                        break
            if excluded and not included:
                print "Skipping test '%s'.." % (fullcmd)
                continue

            #a.extend(["-o", "/tmp/xx" + a[0] + ".xml"])
            #print a
            #a = ["ccdash.py"].extend(a)
            b = ["ccdash.py"]
            b.extend(a)
            a = b
            #print a
            ccdash.main(a)
            counter = counter + 1
        self.post_action()


#
# GNU test configurator
#
class GNUTestBuilder(TestBuilder):
    def __init__(self, config, build_config_name="", user_mak="", \
                 config_site="", cross_compile="", exclude=[], not_exclude=[]):
        TestBuilder.__init__(self, config, build_config_name=build_config_name,
                             user_mak=user_mak, config_site=config_site,
                             exclude=exclude, not_exclude=not_exclude)
        self.cross_compile = cross_compile
        if self.cross_compile and self.cross_compile[-1] != '-':
            self.cross_compile.append("-")

    def build_tests(self):
        if self.cross_compile:
            suffix = self.cross_compile
            build_name =  suffix + gcc_version(self.cross_compile + "gcc")
        else:
            proc = subprocess.Popen(self.config.base_dir+"/config.guess",
                                    stdout=subprocess.PIPE)
            suffix = proc.stdout.readline().rstrip(" \r\n")
            build_name =  suffix+"-"+gcc_version(self.cross_compile + "gcc")

        if self.build_config_name:
            build_name = build_name + "-" + self.build_config_name
        cmds = []
        cmds.extend(update_ops)
        cmds.extend(gnu_build_ops)
        cmds.extend(std_test_ops)
        cmds.extend(build_pjsua_test_ops())
        self.ccdash_args = []
        for c in cmds:
            c.cmdline = c.cmdline.replace("$SUFFIX", suffix)
            args = c.encode(self.config.base_dir)
            args.extend(["-U", self.config.url, 
                         "-S", self.config.site, 
                         "-T", self.stamp(), 
                         "-B", build_name, 
                         "-G", self.config.group])
            args.extend(self.config.options)
            self.ccdash_args.append(args)