mach_commands.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. # This Source Code Form is subject to the terms of the Mozilla Public
  2. # License, v. 2.0. If a copy of the MPL was not distributed with this
  3. # file, You can obtain one at http://mozilla.org/MPL/2.0/.
  4. from __future__ import absolute_import, unicode_literals
  5. import mozpack.path as mozpath
  6. from mozbuild.base import (
  7. MachCommandBase,
  8. )
  9. from mach.decorators import (
  10. CommandArgument,
  11. CommandProvider,
  12. Command,
  13. )
  14. from mach.registrar import (
  15. Registrar
  16. )
  17. from shutil import rmtree
  18. from subprocess import Popen
  19. from sys import argv
  20. from sys import exit
  21. from tempfile import mkdtemp
  22. DEFAULT_PORT = 8080
  23. DEFAULT_HOSTNAME = 'localhost'
  24. SRCDIR = mozpath.abspath(mozpath.dirname(__file__))
  25. STORAGE_SERVER_SCRIPT = mozpath.join(SRCDIR, 'run_storage_server.js')
  26. def SyncStorageCommand(func):
  27. """Decorator that adds shared command arguments to services commands."""
  28. port = CommandArgument('--port', metavar='PORT', type=int,
  29. default=DEFAULT_PORT, help='Port to run server on.')
  30. func = port(func)
  31. address = CommandArgument('--address', metavar='ADDRESS',
  32. default=DEFAULT_HOSTNAME,
  33. help='Hostname to bind server to.')
  34. func = address(func)
  35. return func
  36. Registrar.register_category(name='services',
  37. title='Services utilities',
  38. description='Commands for services development.')
  39. @CommandProvider
  40. class SyncTestCommands(MachCommandBase):
  41. def __init__(self, context):
  42. MachCommandBase.__init__(self, context)
  43. def run_server(self, js_file, hostname, port):
  44. topsrcdir = self.topsrcdir
  45. topobjdir = self.topobjdir
  46. unit_test_dir = mozpath.join(SRCDIR, 'unit')
  47. head_paths = [
  48. 'head_global.js',
  49. 'head_helpers.js',
  50. 'head_http.js',
  51. ]
  52. head_paths = ['"%s"' % mozpath.join(unit_test_dir, path) for path in head_paths]
  53. args = [
  54. '%s/run-mozilla.sh' % self.bindir,
  55. '%s/xpcshell' % self.bindir,
  56. '-g', self.bindir,
  57. '-a', self.bindir,
  58. '-r', '%s/components/httpd.manifest' % self.bindir,
  59. '-m',
  60. '-s',
  61. '-e', 'const _TESTING_MODULES_DIR = "%s/_tests/modules";' % topobjdir,
  62. '-f', '%s/testing/xpcshell/head.js' % topsrcdir,
  63. '-e', 'const _SERVER_ADDR = "%s";' % hostname,
  64. '-e', 'const SERVER_PORT = "%s";' % port,
  65. '-e', 'const INCLUDE_FILES = [%s];' % ', '.join(head_paths),
  66. '-e', '_register_protocol_handlers();',
  67. '-e', 'for (let name of INCLUDE_FILES) load(name);',
  68. '-e', '_fakeIdleService.activate();',
  69. '-f', js_file
  70. ]
  71. profile_dir = mkdtemp()
  72. print 'Created profile directory: %s' % profile_dir
  73. try:
  74. env = {'XPCSHELL_TEST_PROFILE_DIR': profile_dir}
  75. proc = Popen(args, env=env)
  76. return proc.wait()
  77. finally:
  78. print 'Removing profile directory %s' % profile_dir
  79. rmtree(profile_dir)
  80. @Command('storage-server', category='services',
  81. description='Run a storage server.')
  82. @SyncStorageCommand
  83. def run_storage_server(self, port=DEFAULT_PORT, address=DEFAULT_HOSTNAME):
  84. exit(self.run_server(STORAGE_SERVER_SCRIPT, address, port))