<s> from os import devnull <EOL> import subprocess <EOL> from subprocess import PIPE <EOL> import time <EOL> from selenium . common . exceptions import WebDriverException <EOL> from selenium . webdriver . common import utils <EOL> class Service ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , executable_path , port = <NUM_LIT:0> , quiet = False ) : <EOL> """<STR_LIT>""" <EOL> self . port = port <EOL> self . path = executable_path <EOL> if self . port == <NUM_LIT:0> : <EOL> self . port = utils . free_port ( ) <EOL> self . quiet = quiet <EOL> def start ( self ) : <EOL> """<STR_LIT>""" <EOL> kwargs = dict ( ) <EOL> if self . quiet : <EOL> devnull_out = open ( devnull , '<STR_LIT:w>' ) <EOL> kwargs . update ( stdout = devnull_out , <EOL> stderr = devnull_out ) <EOL> try : <EOL> self . process = subprocess . Popen ( [ "<STR_LIT>" , "<STR_LIT>" , self . path , "<STR_LIT>" , "<STR_LIT:%s>" % self . port ] , <EOL> ** kwargs ) <EOL> except : <EOL> raise WebDriverException ( <EOL> "<STR_LIT>" ) <EOL> time . sleep ( <NUM_LIT:10> ) <EOL> count = <NUM_LIT:0> <EOL> while not utils . is_connectable ( self . port ) : <EOL> count += <NUM_LIT:1> <EOL> time . sleep ( <NUM_LIT:1> ) <EOL> if count == <NUM_LIT:30> : <EOL> raise WebDriverException ( "<STR_LIT>" ) <EOL> @ property <EOL> def service_url ( self ) : <EOL> """<STR_LIT>""" <EOL> return "<STR_LIT>" % self . port <EOL> def stop ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . process is None : <EOL> return <EOL> self . process . kill ( ) <EOL> self . process . wait ( ) </s>
