michael@0: #!/usr/bin/python michael@0: michael@0: # This Source Code Form is subject to the terms of the Mozilla Public michael@0: # License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: # You can obtain one at http://mozilla.org/MPL/2.0/. michael@0: michael@0: from argparse import ArgumentParser michael@0: from shutil import rmtree michael@0: from subprocess import Popen michael@0: from sys import argv michael@0: from sys import exit michael@0: from tempfile import mkdtemp michael@0: michael@0: DEFAULT_PORT = 8080 michael@0: DEFAULT_HOSTNAME = 'localhost' michael@0: michael@0: def run_server(srcdir, objdir, js_file, hostname=DEFAULT_HOSTNAME, michael@0: port=DEFAULT_PORT): michael@0: michael@0: dist_dir = '%s/dist' % objdir michael@0: head_dir = '%s/services/common/tests/unit' % srcdir michael@0: michael@0: head_paths = [ michael@0: 'head_global.js', michael@0: 'head_helpers.js', michael@0: 'head_http.js', michael@0: ] michael@0: michael@0: head_paths = ['"%s/%s"' % (head_dir, path) for path in head_paths] michael@0: michael@0: args = [ michael@0: '%s/bin/xpcshell' % dist_dir, michael@0: '-g', '%s/bin' % dist_dir, michael@0: '-a', '%s/bin' % dist_dir, michael@0: '-r', '%s/bin/components/httpd.manifest' % dist_dir, michael@0: '-m', michael@0: '-n', michael@0: '-s', michael@0: '-f', '%s/testing/xpcshell/head.js' % srcdir, michael@0: '-e', 'const _SERVER_ADDR = "%s";' % hostname, michael@0: '-e', 'const _TESTING_MODULES_DIR = "%s/_tests/modules";' % objdir, michael@0: '-e', 'const SERVER_PORT = "%s";' % port, michael@0: '-e', 'const INCLUDE_FILES = [%s];' % ', '.join(head_paths), michael@0: '-e', '_register_protocol_handlers();', michael@0: '-e', 'for each (let name in INCLUDE_FILES) load(name);', michael@0: '-e', '_fakeIdleService.activate();', michael@0: '-f', '%s/services/common/tests/%s' % (srcdir, js_file) michael@0: ] michael@0: michael@0: profile_dir = mkdtemp() michael@0: print 'Created profile directory: %s' % profile_dir michael@0: michael@0: try: michael@0: env = {'XPCSHELL_TEST_PROFILE_DIR': profile_dir} michael@0: proc = Popen(args, env=env) michael@0: michael@0: return proc.wait() michael@0: michael@0: finally: michael@0: print 'Removing profile directory %s' % profile_dir michael@0: rmtree(profile_dir) michael@0: michael@0: if __name__ == '__main__': michael@0: parser = ArgumentParser(description="Run a standalone JS server.") michael@0: parser.add_argument('srcdir', michael@0: help="Root directory of Firefox source code.") michael@0: parser.add_argument('objdir', michael@0: help="Root directory object directory created during build.") michael@0: parser.add_argument('js_file', michael@0: help="JS file (in this directory) to execute.") michael@0: parser.add_argument('--port', default=DEFAULT_PORT, type=int, michael@0: help="Port to run server on.") michael@0: parser.add_argument('--address', default=DEFAULT_HOSTNAME, michael@0: help="Hostname to bind server to.") michael@0: michael@0: args = parser.parse_args() michael@0: michael@0: exit(run_server(args.srcdir, args.objdir, args.js_file, args.address, michael@0: args.port))