1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
|
import logging
import os
import subprocess
import time
import sys
import urllib
class WPTServer(object):
def __init__(self, wpt_root):
self.logger = logging.getLogger()
self.wpt_root = wpt_root
# This is a terrible hack to get the default config of wptserve.
sys.path.insert(0, os.path.join(wpt_root, "tools"))
from serve.serve import build_config
with build_config(self.logger) as config:
self.host = config["browser_host"]
self.http_port = config["ports"]["http"][0]
self.https_port = config["ports"]["https"][0]
self.base_url = 'http://%s:%s' % (self.host, self.http_port)
self.https_base_url = 'https://%s:%s' % (self.host, self.https_port)
def start(self, ssl_context):
self.devnull = open(os.devnull, 'w')
wptserve_cmd = [os.path.join(self.wpt_root, 'wpt'), 'serve']
if sys.executable:
wptserve_cmd[0:0] = [sys.executable]
self.logger.info('Executing %s' % ' '.join(wptserve_cmd))
self.proc = subprocess.Popen(
wptserve_cmd,
stderr=self.devnull,
cwd=self.wpt_root)
for retry in range(5):
# Exponential backoff.
time.sleep(2 ** retry)
exit_code = self.proc.poll()
if exit_code != None:
logging.warning('Command "%s" exited with %s', ' '.join(wptserve_cmd), exit_code)
break
try:
urllib.request.urlopen(self.base_url, timeout=1)
urllib.request.urlopen(self.https_base_url, timeout=1, context=ssl_context)
return
except urllib.error.URLError:
pass
raise Exception('Could not start wptserve on %s' % self.base_url)
def stop(self):
self.proc.terminate()
self.proc.wait()
self.devnull.close()
def url(self, abs_path):
return self.https_base_url + '/' + os.path.relpath(abs_path, self.wpt_root)
|