summaryrefslogtreecommitdiffstats
path: root/src/browse.py
blob: 1860df3f1b6a5655aa1309e345d643812ae02e2a (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
#!/usr/bin/python
#
# Copyright 2001 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Simple web server for browsing dependency graph data.

This script is inlined into the final executable and spawned by
it when needed.
"""

import BaseHTTPServer
import subprocess
import sys
import webbrowser

def match_strip(prefix, line):
    if not line.startswith(prefix):
        print prefix, line
        assert line.startswith(prefix)
    return line[len(prefix):]

def parse(text):
    lines = text.split('\n')
    node = lines.pop(0)
    node = node[:-1]  # strip trailing colon

    input = []
    if lines and lines[0].startswith('  input:'):
        input.append(match_strip('  input: ', lines.pop(0)))
        while lines and lines[0].startswith('    '):
            input.append(lines.pop(0).strip())

    outputs = []
    while lines:
        output = []
        output.append(match_strip('  output: ', lines.pop(0)))
        while lines and lines[0].startswith('    '):
            output.append(lines.pop(0).strip())
        outputs.append(output)

    return (node, input, outputs)

def generate_html(data):
    node, input, outputs = data
    print '''<!DOCTYPE html>
<style>
body {
    font-family: sans;
    font-size: 0.8em;
    margin: 4ex;
}
h1 {
    font-weight: normal;
    text-align: center;
    margin: 0;
}
h2 {
    font-weight: normal;
}
tt {
    font-family: WebKitHack, monospace;
}
</style>'''
    print '<table><tr><td colspan=3>'
    print '<h1>%s</h1>' % node
    print '</td></tr>'

    print '<tr><td valign=top>'
    print '<h2>input</h2>'
    if input:
        print '<p><tt>%s</tt>:</p>' % input[0]
        print '<ul>'
        for i in input[1:]:
            print '<li><tt><a href="?%s">%s</a></tt></li>' % (i, i)
        print '</ul>'
    print '</td>'
    print '<td width=50>&nbsp;</td>'

    print '<td valign=top>'
    print '<h2>outputs</h2>'
    for output in outputs:
        print '<p><tt>%s</tt>:</p>' % output[0]
        print '<ul>'
        for i in output[1:]:
            print '<li><tt><a href="?%s">%s</a></tt></li>' % (i, i)
        print '</ul>'
    print '</td></tr></table>'

def ninja_dump(target):
    proc = subprocess.Popen(['./ninja', '-t', 'query', target],
                            stdout=subprocess.PIPE)
    return proc.communicate()[0]

class RequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
    def do_GET(self):
        assert self.path[0] == '/'
        target = self.path[1:]

        if target == '':
            self.send_response(302)
            self.send_header('Location', '?' + sys.argv[1])
            self.end_headers()
            return

        if not target.startswith('?'):
            self.send_response(404)
            self.end_headers()
            return
        target = target[1:]

        input = ninja_dump(target)

        self.send_response(200)
        self.end_headers()
        stdout = sys.stdout
        sys.stdout = self.wfile
        try:
            generate_html(parse(input.strip()))
        finally:
            sys.stdout = stdout

    def log_message(self, format, *args):
        pass  # Swallow console spam.

port = 8000
httpd = BaseHTTPServer.HTTPServer(('',port), RequestHandler)
try:
    print 'Web server running on port %d, ctl-C to abort...' % port
    webbrowser.open_new('http://localhost:%s' % port)
    httpd.serve_forever()
except KeyboardInterrupt:
    print
    pass  # Swallow console spam.