summaryrefslogtreecommitdiff
path: root/tools/build/test/collect_debug_info.py
blob: b25c15cc3a0c2c9d6a2e8c392c64cf0231c6a890 (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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#!/usr/bin/python

# Copyright 2012 Jurko Gospodnetic
# Distributed under the Boost Software License, Version 1.0.
# (See accompanying file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)

#   Temporarily enabled dummy test that always fails and is used to collect
# extra debugging information from Boost Build test runner sites.

import BoostBuild

import os
import re
import sys


###############################################################################
#
# Public interface.
#
###############################################################################

def collectDebugInfo():
    t = _init()

    global tag

    tag = "Python version"
    try:
        _info(sys.version)
    except:
        _info_exc()

    tag = "Python platform"
    try:
        _info(sys.platform)
    except:
        _info_exc()

    tag = "Boost Jam/Build version"
    try:
        _infoX(_getJamVersionInfo(t))
    except:
        _info_exc()

    #_collectDebugInfo_environ()

    # Report prepared annotations.
    t.fail_test(1, dump_difference=False, dump_stdio=False, dump_stack=False)


###############################################################################
#
# Private interface.
#
###############################################################################

varSeparator = "###$^%~~~"


def _collect(results, prefix, name, t):
    results.append("%s - %s - os.getenv(): %r" % (prefix, name, os.getenv(
        name)))
    results.append("%s - %s - os.environ.get(): %r" % (prefix, name,
        os.environ.get(name)))
    external_values = _getExternalValues(t, name)
    results.append("%s - %s - external: %r" % (prefix, name,
        external_values[name]))


def _collectDebugInfo_environ(t):
    dummyVars = ["WOOF_WOOFIE_%d" % x for x in xrange(4)]
    global tag

    tag = "XXX in os.environ"
    try:
        def f(name):
            return "%s: %s" % (name, name in os.environ)
        _infoX(f(x) for x in dummyVars)
    except:
        _info_exc()

    tag = "os.environ[XXX]"
    try:
        def f(name):
            try:
                result = os.environ[name]
            except:
                result = _str_exc()
            return "%s: %r" % (name, result)
        _infoX(f(x) for x in dummyVars)
    except:
        _info_exc()

    tag = "os.environ.get(XXX)"
    try:
        def f(name):
            return "%s: %r" % (name, os.environ.get(name))
        _infoX(f(x) for x in dummyVars)
    except:
        _info_exc()

    tag = "os.getenv(XXX)"
    try:
        def f(name):
            return "%s: %r" % (name, os.getenv(name))
        _infoX(f(x) for x in dummyVars)
    except:
        _info_exc()

    name = dummyVars[0]
    value = "foo"
    tag = "os.putenv(%s) to %r" % (name, value)
    try:
        results = []
        _collect(results, "before", name, t)
        os.putenv(name, value)
        _collect(results, "after", name, t)
        _infoX(results)
    except:
        _info_exc()

    name = dummyVars[1]
    value = "bar"
    tag = "os.environ[%s] to %r" % (name, value)
    try:
        results = []
        _collect(results, "before", name, t)
        os.environ[name] = value
        _collect(results, "after", name, t)
        _infoX(results)
    except:
        _info_exc()

    name = dummyVars[1]
    value = "baz"
    tag = "os.putenv(%s) to %r" % (name, value)
    try:
        results = []
        _collect(results, "before", name, t)
        os.putenv(name, value)
        _collect(results, "after", name, t)
        _infoX(results)
    except:
        _info_exc()

    name = dummyVars[1]
    value = ""
    tag = "os.putenv(%s) to %r" % (name, value)
    try:
        results = []
        _collect(results, "before", name, t)
        os.putenv(name, value)
        _collect(results, "after", name, t)
        _infoX(results)
    except:
        _info_exc()

    name = dummyVars[2]
    value = "foo"
    tag = "os.unsetenv(%s) from %r" % (name, value)
    try:
        results = []
        os.environ[name] = value
        _collect(results, "before", name, t)
        os.unsetenv(name)
        _collect(results, "after", name, t)
        _infoX(results)
    except:
        _info_exc()

    name = dummyVars[2]
    value = "foo"
    tag = "del os.environ[%s] from %r" % (name, value)
    try:
        results = []
        os.environ[name] = value
        _collect(results, "before", name, t)
        del os.environ[name]
        _collect(results, "after", name, t)
        _infoX(results)
    except:
        _info_exc()

    name = dummyVars[2]
    value = "foo"
    tag = "os.environ.pop(%s) from %r" % (name, value)
    try:
        results = []
        os.environ[name] = value
        _collect(results, "before", name, t)
        os.environ.pop(name)
        _collect(results, "after", name, t)
        _infoX(results)
    except:
        _info_exc()

    name = dummyVars[2]
    value1 = "foo"
    value2 = ""
    tag = "os.environ[%s] to %r from %r" % (name, value2, value1)
    try:
        results = []
        os.environ[name] = value1
        _collect(results, "before", name, t)
        os.environ[name] = value2
        _collect(results, "after", name, t)
        _infoX(results)
    except:
        _info_exc()

    name = dummyVars[3]
    value = '""'
    tag = "os.environ[%s] to %r" % (name, value)
    try:
        results = []
        _collect(results, "before", name, t)
        os.environ[name] = value
        _collect(results, "after", name, t)
        _infoX(results)
    except:
        _info_exc()


def _getExternalValues(t, *args):
    t.run_build_system(["---var-name=%s" % x for x in args])
    result = dict()
    for x in args:
        m = re.search(r"^\*\*\*ENV\*\*\* %s: '(.*)' \*\*\*$" % x, t.stdout(),
            re.MULTILINE)
        if m:
            result[x] = m.group(1)
        else:
            result[x] = None
    return result


def _getJamVersionInfo(t):
    result = []

    # JAM version variables.
    t.run_build_system(["---version"])
    for m in re.finditer(r"^\*\*\*VAR\*\*\* ([^:]*): (.*)\*\*\*$", t.stdout(),
        re.MULTILINE):
        name = m.group(1)
        value = m.group(2)
        if not value:
            value = []
        elif value[-1] == ' ':
            value = value[:-1].split(varSeparator)
        else:
            value = "!!!INVALID!!! - '%s'" % value
        result.append("%s = %s" % (name, value))
    result.append("")

    # bjam -v output.
    t.run_build_system(["-v"])
    result.append("--- output for 'bjam -v' ---")
    result.append(t.stdout())

    # bjam --version output.
    t.run_build_system(["--version"], status=1)
    result.append("--- output for 'bjam --version' ---")
    result.append(t.stdout())

    return result


def _init():
    toolsetName = "__myDummyToolset__"

    t = BoostBuild.Tester(["toolset=%s" % toolsetName], pass_toolset=False,
        use_test_config=False)

    #   Prepare a dummy toolset so we do not get errors in case the default one
    # is not found.
    t.write(toolsetName + ".jam", """\
import feature ;
feature.extend toolset : %s ;
rule init ( ) { }
""" % toolsetName )

    # Python version of the same dummy toolset.
    t.write(toolsetName + ".py", """\
from b2.build import feature
feature.extend('toolset', ['%s'])
def init(): pass
""" % toolsetName )

    t.write("jamroot.jam", """\
import os ;
.argv = [ modules.peek : ARGV ] ;
local names = [ MATCH ^---var-name=(.*) : $(.argv) ] ;
for x in $(names)
{
    value = [ os.environ $(x) ] ;
    ECHO ***ENV*** $(x): '$(value)' *** ;
}
if ---version in $(.argv)
{
    for x in JAMVERSION JAM_VERSION JAMUNAME JAM_TIMESTAMP_RESOLUTION OS
    {
        v = [ modules.peek : $(x) ] ;
        ECHO ***VAR*** $(x): "$(v:J=%s)" *** ;
    }
}
""" % varSeparator)

    return t


def _info(*values):
    values = list(values) + [""]
    BoostBuild.annotation(tag, "\n".join(str(x) for x in values))


def _infoX(values):
    _info(*values)


def _info_exc():
    _info(_str_exc())


def _str_exc():
    exc_type, exc_value = sys.exc_info()[0:2]
    if exc_type is None:
        exc_type_name = "None"
    else:
        exc_type_name = exc_type.__name__
    return "*** EXCEPTION *** %s - %s ***" % (exc_type_name, exc_value)


###############################################################################
#
# main()
# ------
#
###############################################################################

collectDebugInfo()