From 13db3daadc2fb30320de90fe7667ab1cf9166799 Mon Sep 17 00:00:00 2001 From: Philip Puryear Date: Thu, 25 Oct 2012 22:41:20 -0500 Subject: browse.py: Python 3 support. Signed-off-by: Philip Puryear --- src/browse.py | 63 ++++++++++++++++++++++++++++++++--------------------------- 1 file changed, 34 insertions(+), 29 deletions(-) diff --git a/src/browse.py b/src/browse.py index 17e67cf..6bbf4de 100755 --- a/src/browse.py +++ b/src/browse.py @@ -20,7 +20,12 @@ This script is inlined into the final executable and spawned by it when needed. """ -import BaseHTTPServer +from __future__ import print_function + +try: + import http.server as httpserver +except ImportError: + import BaseHTTPServer as httpserver import subprocess import sys import webbrowser @@ -55,12 +60,12 @@ def parse(text): outputs = [] try: - target = lines.next()[:-1] # strip trailing colon + target = next(lines)[:-1] # strip trailing colon - line = lines.next() + line = next(lines) (match, rule) = match_strip(line, ' input: ') if match: - (match, line) = match_strip(lines.next(), ' ') + (match, line) = match_strip(next(lines), ' ') while match: type = None (match, line) = match_strip(line, '| ') @@ -70,21 +75,21 @@ def parse(text): if match: type = 'order-only' inputs.append((line, type)) - (match, line) = match_strip(lines.next(), ' ') + (match, line) = match_strip(next(lines), ' ') match, _ = match_strip(line, ' outputs:') if match: - (match, line) = match_strip(lines.next(), ' ') + (match, line) = match_strip(next(lines), ' ') while match: outputs.append(line) - (match, line) = match_strip(lines.next(), ' ') + (match, line) = match_strip(next(lines), ' ') except StopIteration: pass return Node(inputs, rule, target, outputs) def generate_html(node): - print ''' + document = [''' ''' +'''] - print '

%s

' % node.target + document.append('

%s

' % node.target) if node.inputs: - print '

target is built using rule %s of

' % node.rule + document.append('

target is built using rule %s of

' % + node.rule) if len(node.inputs) > 0: - print '
' + document.append('
') for input, type in sorted(node.inputs): extra = '' if type: extra = ' (%s)' % type - print '%s%s
' % (input, input, extra) - print '
' + document.append('%s%s
' % + (input, input, extra)) + document.append('
') if node.outputs: - print '

dependent edges build:

' - print '
' + document.append('

dependent edges build:

') + document.append('
') for output in sorted(node.outputs): - print '%s
' % (output, output) - print '
' + document.append('%s
' % + (output, output)) + document.append('
') + + return '\n'.join(document) def ninja_dump(target): proc = subprocess.Popen([sys.argv[1], '-t', 'query', target], - stdout=subprocess.PIPE) + stdout=subprocess.PIPE, universal_newlines=True) return proc.communicate()[0] -class RequestHandler(BaseHTTPServer.BaseHTTPRequestHandler): +class RequestHandler(httpserver.BaseHTTPRequestHandler): def do_GET(self): assert self.path[0] == '/' target = self.path[1:] @@ -156,24 +166,19 @@ class RequestHandler(BaseHTTPServer.BaseHTTPRequestHandler): self.send_response(200) self.end_headers() - stdout = sys.stdout - sys.stdout = self.wfile - try: - generate_html(parse(input.strip())) - finally: - sys.stdout = stdout + self.wfile.write(generate_html(parse(input.strip())).encode('utf-8')) def log_message(self, format, *args): pass # Swallow console spam. port = 8000 -httpd = BaseHTTPServer.HTTPServer(('',port), RequestHandler) +httpd = httpserver.HTTPServer(('',port), RequestHandler) try: - print 'Web server running on port %d, ctl-C to abort...' % port + 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 + print() pass # Swallow console spam. -- cgit v0.12 From 10a0a7d9659591c44bba1ab74b069c9313a47dfa Mon Sep 17 00:00:00 2001 From: Philip Puryear Date: Thu, 25 Oct 2012 22:43:03 -0500 Subject: browse.py: Fix truncation with an unknown target. Signed-off-by: Philip Puryear --- src/browse.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/browse.py b/src/browse.py index 6bbf4de..bf4ff49 100755 --- a/src/browse.py +++ b/src/browse.py @@ -60,7 +60,9 @@ def parse(text): outputs = [] try: - target = next(lines)[:-1] # strip trailing colon + target = next(lines) + if target.endswith(':'): + target = target[:-1] # strip trailing colon line = next(lines) (match, rule) = match_strip(line, ' input: ') -- cgit v0.12 From 477b8b96aa5e4cee6cc85b9c629a986cd8469d07 Mon Sep 17 00:00:00 2001 From: Philip Puryear Date: Fri, 26 Oct 2012 16:56:48 -0500 Subject: browse.py: Don't truncate error message if ninja -t query fails. Signed-off-by: Philip Puryear --- src/browse.py | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/src/browse.py b/src/browse.py index bf4ff49..14d5edd 100755 --- a/src/browse.py +++ b/src/browse.py @@ -60,9 +60,7 @@ def parse(text): outputs = [] try: - target = next(lines) - if target.endswith(':'): - target = target[:-1] # strip trailing colon + target = next(lines)[:-1] # strip trailing colon line = next(lines) (match, rule) = match_strip(line, ' input: ') @@ -90,8 +88,8 @@ def parse(text): return Node(inputs, rule, target, outputs) -def generate_html(node): - document = [''' +def create_page(body): + return ''' '''] + +''' + body - document.append('

%s

' % node.target) +def generate_html(node): + document = ['

%s

' % node.target] if node.inputs: document.append('

target is built using rule %s of

' % @@ -145,7 +145,7 @@ tt { def ninja_dump(target): proc = subprocess.Popen([sys.argv[1], '-t', 'query', target], stdout=subprocess.PIPE, universal_newlines=True) - return proc.communicate()[0] + return (proc.communicate()[0], proc.returncode) class RequestHandler(httpserver.BaseHTTPRequestHandler): def do_GET(self): @@ -164,11 +164,16 @@ class RequestHandler(httpserver.BaseHTTPRequestHandler): return target = target[1:] - input = ninja_dump(target) + input, exit_code = ninja_dump(target) + if exit_code == 0: + page_body = generate_html(parse(input.strip())) + else: + # Relay ninja's error message. + page_body = '

%s

' % input self.send_response(200) self.end_headers() - self.wfile.write(generate_html(parse(input.strip())).encode('utf-8')) + self.wfile.write(create_page(page_body).encode('utf-8')) def log_message(self, format, *args): pass # Swallow console spam. -- cgit v0.12