#!/usr/bin/python """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): 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 ''' ''' print '' print '' print '' print '
' print '

%s

' % node print '
' print '

input

' if input: print '

%s:

' % input[0] print '
    ' for i in input[1:]: print '
  • %s
  • ' % (i, i) print '
' print '
 ' print '

outputs

' for output in outputs: print '

%s:

' % output[0] print '
    ' for i in output[1:]: print '
  • %s
  • ' % (i, i) print '
' print '
' 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.