profileserver.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #!/usr/bin/python
  2. #
  3. # This Source Code Form is subject to the terms of the Mozilla Public
  4. # License, v. 2.0. If a copy of the MPL was not distributed with this
  5. # file, You can obtain one at http://mozilla.org/MPL/2.0/.
  6. from mozprofile import FirefoxProfile, Profile, Preferences
  7. from mozprofile.permissions import ServerLocations
  8. from mozrunner import FirefoxRunner, CLI
  9. from mozhttpd import MozHttpd
  10. import json
  11. import socket
  12. import threading
  13. import os
  14. import sys
  15. import shutil
  16. import tempfile
  17. from datetime import datetime
  18. from mozbuild.base import MozbuildObject
  19. from buildconfig import substs
  20. PORT = 8888
  21. if __name__ == '__main__':
  22. cli = CLI()
  23. debug_args, interactive = cli.debugger_arguments()
  24. build = MozbuildObject.from_environment()
  25. httpd = MozHttpd(port=PORT,
  26. docroot=os.path.join(build.topsrcdir, "build", "pgo"))
  27. httpd.start(block=False)
  28. locations = ServerLocations()
  29. locations.add_host(host='127.0.0.1',
  30. port=PORT,
  31. options='primary,privileged')
  32. #TODO: mozfile.TemporaryDirectory
  33. profilePath = tempfile.mkdtemp()
  34. try:
  35. #TODO: refactor this into mozprofile
  36. prefpath = os.path.join(build.topsrcdir, "testing", "profiles", "prefs_general.js")
  37. prefs = {}
  38. prefs.update(Preferences.read_prefs(prefpath))
  39. interpolation = { "server": "%s:%d" % httpd.httpd.server_address,
  40. "OOP": "false"}
  41. prefs = json.loads(json.dumps(prefs) % interpolation)
  42. for pref in prefs:
  43. prefs[pref] = Preferences.cast(prefs[pref])
  44. profile = FirefoxProfile(profile=profilePath,
  45. preferences=prefs,
  46. addons=[os.path.join(build.topsrcdir, 'tools', 'quitter', 'quitter@mozilla.org.xpi')],
  47. locations=locations)
  48. env = os.environ.copy()
  49. env["XPCOM_DEBUG_BREAK"] = "warn"
  50. # For VC12+, make sure we can find the right bitness of pgort1x0.dll
  51. if not substs.get('HAVE_64BIT_BUILD'):
  52. for e in ('VS140COMNTOOLS', 'VS120COMNTOOLS'):
  53. if e not in env:
  54. continue
  55. vcdir = os.path.abspath(os.path.join(env[e], '../../VC/bin'))
  56. if os.path.exists(vcdir):
  57. env['PATH'] = '%s;%s' % (vcdir, env['PATH'])
  58. break
  59. # Run Firefox a first time to initialize its profile
  60. runner = FirefoxRunner(profile=profile,
  61. binary=build.get_binary_path(where="staged-package"),
  62. cmdargs=['javascript:Quitter.quit()'],
  63. env=env)
  64. runner.start()
  65. runner.wait()
  66. jarlog = os.getenv("JARLOG_FILE")
  67. if jarlog:
  68. env["MOZ_JAR_LOG_FILE"] = os.path.abspath(jarlog)
  69. print "jarlog: %s" % env["MOZ_JAR_LOG_FILE"]
  70. cmdargs = ["http://localhost:%d/index.html" % PORT]
  71. runner = FirefoxRunner(profile=profile,
  72. binary=build.get_binary_path(where="staged-package"),
  73. cmdargs=cmdargs,
  74. env=env)
  75. runner.start(debug_args=debug_args, interactive=interactive)
  76. runner.wait()
  77. httpd.stop()
  78. finally:
  79. shutil.rmtree(profilePath)