1import sys
2import os.path
3import re
4
5assert len(sys.argv) == 2
6mochiPath = sys.argv[1]
7
8extDotPos = mochiPath.find('.html')
9assert extDotPos != -1, 'mochitest target must be an html doc.'
10
11testPath = mochiPath[:extDotPos] + '.solo.html'
12
13def ReadLocalFile(include):
14    incPath = os.path.dirname(mochiPath)
15    filePath = os.path.join(incPath, include)
16
17    data = None
18    try:
19        f = open(filePath, 'r')
20        data = f.read()
21    except:
22        pass
23
24    try:
25        f.close()
26    except:
27        pass
28
29    return data
30
31kSimpleTestReplacement = '''
32
33<script>
34// SimpleTest.js replacement
35
36function debug(text) {
37  var elem = document.getElementById('mochi-to-testcase-output');
38  elem.innerHTML += '\\n<br/>\\n' + text;
39}
40
41function ok(val, text) {
42  var status = val ? 'Test <font color=\\'green\\'>passed</font>: '
43                   : 'Test <font color=\\'red\\'  >FAILED</font>: ';
44  debug(status + text);
45}
46
47function todo(val, text) {
48  var status = val ? 'Test <font color=\\'orange\\'>UNEXPECTED PASS</font>: '
49                   : 'Test <font color=\\'blue\\'  >todo</font>: ';
50  debug(status + text);
51}
52
53SimpleTest = {
54  waitForExplicitFinish: function() {},
55  finish: function() {},
56  requestFlakyTimeout: function() {},
57};
58</script>
59<div id='mochi-to-testcase-output'></div>
60
61'''
62
63fin = open(mochiPath, 'rb')
64fout = open(testPath, 'wb')
65includePattern = re.compile('<script\\s*src=[\'"](.*)\\.js[\'"]>\\s*</script>')
66cssPattern = re.compile('<link\\s*rel=[\'"]stylesheet[\'"]\\s*href=[\'"]([^=>]*)[\'"]>')
67for line in fin:
68    skipLine = False
69    for css in cssPattern.findall(line):
70        skipLine = True
71        print('Ignoring stylesheet: ' + css)
72
73    for inc in includePattern.findall(line):
74        skipLine = True
75        if inc == '/MochiKit/MochiKit':
76            continue
77
78        if inc == '/tests/SimpleTest/SimpleTest':
79            print('Injecting SimpleTest replacement')
80            fout.write(kSimpleTestReplacement);
81            continue
82
83        incData = ReadLocalFile(inc + '.js')
84        if not incData:
85            print('Warning: Unknown JS file ignored: ' + inc + '.js')
86            continue
87
88        print('Injecting include: ' + inc + '.js')
89        fout.write('\n<script>\n// Imported from: ' + inc + '.js\n');
90        fout.write(incData);
91        fout.write('\n</script>\n');
92        continue
93
94    if skipLine:
95        continue
96
97    fout.write(line)
98    continue
99
100fin.close()
101fout.close()
102