diff --git a/python/mozbuild/mozbuild/html_build_viewer.py b/python/mozbuild/mozbuild/html_build_viewer.py index 619f79569bb8..5151f04a4824 100644 --- a/python/mozbuild/mozbuild/html_build_viewer.py +++ b/python/mozbuild/mozbuild/html_build_viewer.py @@ -10,6 +10,8 @@ import BaseHTTPServer import json import os +import requests + class HTTPHandler(BaseHTTPServer.BaseHTTPRequestHandler): def do_GET(self): @@ -36,9 +38,7 @@ class HTTPHandler(BaseHTTPServer.BaseHTTPRequestHandler): self.send_header('Content-Type', 'application/json; charset=utf-8') self.end_headers() - with open(s.json_files[key], 'rb') as fh: - self.wfile.write(fh.read()) - + self.wfile.write(s.json_files[key]) return if p == '/': @@ -105,7 +105,15 @@ class BuildViewerServer(object): """Register a resource JSON file with the server. The file will be made available under the name/key specified.""" - self.json_files[key] = path + with open(path, 'rb') as fh: + self.json_files[key] = fh.read() + + def add_resource_json_url(self, key, url): + """Register a resource JSON file at a URL.""" + r = requests.get(url) + if r.status_code != 200: + raise Exception('Non-200 HTTP response code') + self.json_files[key] = r.text def run(self): while not self.do_shutdown: diff --git a/python/mozbuild/mozbuild/mach_commands.py b/python/mozbuild/mozbuild/mach_commands.py index aeeb5aa42f43..a2cb8db61945 100644 --- a/python/mozbuild/mozbuild/mach_commands.py +++ b/python/mozbuild/mozbuild/mach_commands.py @@ -579,19 +579,25 @@ class Build(MachCommandBase): help='Port number the HTTP server should listen on.') @CommandArgument('--browser', default='firefox', help='Web browser to automatically open. See webbrowser Python module.') - def resource_usage(self, address=None, port=None, browser=None): + @CommandArgument('--url', + help='URL of JSON document to display') + def resource_usage(self, address=None, port=None, browser=None, url=None): import webbrowser from mozbuild.html_build_viewer import BuildViewerServer - last = self._get_state_filename('build_resources.json') - if not os.path.exists(last): - print('Build resources not available. If you have performed a ' - 'build and receive this message, the psutil Python package ' - 'likely failed to initialize properly.') - return 1 - server = BuildViewerServer(address, port) - server.add_resource_json_file('last', last) + + if url: + server.add_resource_json_url('url', url) + else: + last = self._get_state_filename('build_resources.json') + if not os.path.exists(last): + print('Build resources not available. If you have performed a ' + 'build and receive this message, the psutil Python package ' + 'likely failed to initialize properly.') + return 1 + + server.add_resource_json_file('last', last) try: webbrowser.get(browser).open_new_tab(server.url) except Exception: