<s> import os <EOL> import platform <EOL> from subprocess import Popen , STDOUT <EOL> from selenium . common . exceptions import WebDriverException <EOL> from selenium . webdriver . common import utils <EOL> import time <EOL> class FirefoxBinary ( object ) : <EOL> NO_FOCUS_LIBRARY_NAME = "<STR_LIT>" <EOL> def __init__ ( self , firefox_path = None , log_file = None ) : <EOL> """<STR_LIT>""" <EOL> self . _start_cmd = firefox_path <EOL> self . _log_file = log_file or open ( os . devnull , "<STR_LIT:wb>" ) <EOL> self . command_line = None <EOL> if self . _start_cmd is None : <EOL> self . _start_cmd = self . _get_firefox_start_cmd ( ) <EOL> if not self . _start_cmd . strip ( ) : <EOL> raise Exception ( "<STR_LIT>" + <EOL> "<STR_LIT>" ) <EOL> self . _firefox_env = os . environ . copy ( ) <EOL> self . _firefox_env [ "<STR_LIT>" ] = "<STR_LIT:1>" <EOL> self . _firefox_env [ "<STR_LIT>" ] = "<STR_LIT:1>" <EOL> self . _firefox_env [ "<STR_LIT>" ] = "<STR_LIT:1>" <EOL> def add_command_line_options ( self , * args ) : <EOL> self . command_line = args <EOL> def launch_browser ( self , profile ) : <EOL> """<STR_LIT>""" <EOL> self . profile = profile <EOL> self . _start_from_profile_path ( self . profile . path ) <EOL> self . _wait_until_connectable ( ) <EOL> def kill ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . process : <EOL> self . process . kill ( ) <EOL> self . process . wait ( ) <EOL> def _start_from_profile_path ( self , path ) : <EOL> self . _firefox_env [ "<STR_LIT>" ] = path <EOL> if platform . system ( ) . lower ( ) == '<STR_LIT>' : <EOL> self . _modify_link_library_path ( ) <EOL> command = [ self . _start_cmd , "<STR_LIT>" ] <EOL> if self . command_line is not None : <EOL> for cli in self . command_line : <EOL> command . append ( cli ) <EOL> Popen ( command , stdout = self . _log_file , stderr = STDOUT , <EOL> env = self . _firefox_env ) . communicate ( ) <EOL> command [ <NUM_LIT:1> ] = '<STR_LIT>' <EOL> self . process = Popen ( <EOL> command , stdout = self . _log_file , stderr = STDOUT , <EOL> env = self . _firefox_env ) <EOL> def _wait_until_connectable ( self ) : <EOL> """<STR_LIT>""" <EOL> count = <NUM_LIT:0> <EOL> while not utils . is_connectable ( self . profile . port ) : <EOL> if self . process . poll ( ) is not None : <EOL> raise WebDriverException ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if count == <NUM_LIT:30> : <EOL> self . kill ( ) <EOL> raise WebDriverException ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> count += <NUM_LIT:1> <EOL> time . sleep ( <NUM_LIT:1> ) <EOL> return True <EOL> def _find_exe_in_registry ( self ) : <EOL> try : <EOL> from _winreg import OpenKey , QueryValue , HKEY_LOCAL_MACHINE , HKEY_CURRENT_USER <EOL> except ImportError : <EOL> from winreg import OpenKey , QueryValue , HKEY_LOCAL_MACHINE , HKEY_CURRENT_USER <EOL> import shlex <EOL> keys = ( <EOL> r"<STR_LIT>" , <EOL> r"<STR_LIT>" <EOL> ) <EOL> command = "<STR_LIT>" <EOL> for path in keys : <EOL> try : <EOL> key = OpenKey ( HKEY_LOCAL_MACHINE , path ) <EOL> command = QueryValue ( key , "<STR_LIT>" ) <EOL> break <EOL> except OSError : <EOL> try : <EOL> key = OpenKey ( HKEY_CURRENT_USER , path ) <EOL> command = QueryValue ( key , "<STR_LIT>" ) <EOL> break <EOL> except OSError : <EOL> pass <EOL> else : <EOL> return "<STR_LIT>" <EOL> if not command : <EOL> return "<STR_LIT>" <EOL> return shlex . split ( command ) [ <NUM_LIT:0> ] <EOL> def _get_firefox_start_cmd ( self ) : <EOL> """<STR_LIT>""" <EOL> start_cmd = "<STR_LIT>" <EOL> if platform . system ( ) == "<STR_LIT>" : <EOL> start_cmd = ( "<STR_LIT>" ) <EOL> elif platform . system ( ) == "<STR_LIT>" : <EOL> start_cmd = ( self . _find_exe_in_registry ( ) or <EOL> self . _default_windows_location ( ) ) <EOL> elif platform . system ( ) == '<STR_LIT>' and os . _name == '<STR_LIT>' : <EOL> start_cmd = self . _default_windows_location ( ) <EOL> else : <EOL> for ffname in [ "<STR_LIT>" , "<STR_LIT>" ] : <EOL> start_cmd = self . which ( ffname ) <EOL> if start_cmd is not None : <EOL> break <EOL> else : <EOL> raise RuntimeError ( "<STR_LIT>" + <EOL> "<STR_LIT>" ) <EOL> return start_cmd <EOL> def _default_windows_location ( self ) : <EOL> program_files = [ os . getenv ( "<STR_LIT>" , r"<STR_LIT>" ) , <EOL> os . getenv ( "<STR_LIT>" , r"<STR_LIT>" ) ] <EOL> for path in program_files : <EOL> binary_path = os . path . join ( path , r"<STR_LIT>" ) <EOL> if os . access ( binary_path , os . X_OK ) : <EOL> return binary_path <EOL> return "<STR_LIT>" <EOL> def _modify_link_library_path ( self ) : <EOL> existing_ld_lib_path = os . environ . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> new_ld_lib_path = self . _extract_and_check ( <EOL> self . profile , self . NO_FOCUS_LIBRARY_NAME , "<STR_LIT>" , "<STR_LIT>" ) <EOL> new_ld_lib_path += existing_ld_lib_path <EOL> self . _firefox_env [ "<STR_LIT>" ] = new_ld_lib_path <EOL> self . _firefox_env [ '<STR_LIT>' ] = self . NO_FOCUS_LIBRARY_NAME <EOL> def _extract_and_check ( self , profile , no_focus_so_name , x86 , amd64 ) : <EOL> paths = [ x86 , amd64 ] <EOL> built_path = "<STR_LIT>" <EOL> for path in paths : <EOL> library_path = os . path . join ( profile . path , path ) <EOL> os . makedirs ( library_path ) <EOL> import shutil <EOL> shutil . copy ( os . path . join ( os . path . dirname ( __file__ ) , path , <EOL> self . NO_FOCUS_LIBRARY_NAME ) , <EOL> library_path ) <EOL> built_path += library_path + "<STR_LIT::>" <EOL> return built_path <EOL> def which ( self , fname ) : <EOL> """<STR_LIT>""" <EOL> for pe in os . environ [ '<STR_LIT>' ] . split ( os . pathsep ) : <EOL> checkname = os . path . join ( pe , fname ) <EOL> if os . access ( checkname , os . X_OK ) and not os . path . isdir ( checkname ) : <EOL> return checkname <EOL> return None </s>
