1- import subprocess , os , time , json ,logging
1+ import subprocess , os , time , json , logging , re
22import psutil
33
44from browserstack .local_binary import LocalBinary
@@ -70,7 +70,16 @@ def start(self, **kwargs):
7070 del self .options ['key' ]
7171
7272 if 'binarypath' in self .options :
73- self .binary_path = self .options ['binarypath' ]
73+ candidate = os .path .realpath (self .options ['binarypath' ])
74+ if not os .path .isfile (candidate ):
75+ raise BrowserStackLocalError ('binarypath does not point to a file' )
76+ try :
77+ version_output = subprocess .check_output ([candidate , '--version' ], timeout = 10 ).decode ('utf-8' )
78+ except (subprocess .SubprocessError , OSError ) as e :
79+ raise BrowserStackLocalError ('binarypath failed verification: {}' .format (e ))
80+ if not re .match (LocalBinary .VERSION_REGEX , version_output ):
81+ raise BrowserStackLocalError ('binarypath failed verification' )
82+ self .binary_path = candidate
7483 del self .options ['binarypath' ]
7584 else :
7685 l = LocalBinary (self .key )
@@ -90,10 +99,18 @@ def start(self, **kwargs):
9099 if 'source' in self .options :
91100 del self .options ['source' ]
92101
102+ logfile_dir = os .path .dirname (self .local_logfile_path )
103+ if logfile_dir :
104+ os .makedirs (logfile_dir , exist_ok = True )
105+ try :
106+ with open (self .local_logfile_path , 'w' ) as f :
107+ f .write ('' )
108+ except OSError as e :
109+ raise BrowserStackLocalError ('Unable to open logfile: {}' .format (e ))
110+
93111 self .proc = subprocess .Popen (self ._generate_cmd (), stdout = subprocess .PIPE , stderr = subprocess .PIPE )
94112 (out , err ) = self .proc .communicate ()
95113
96- os .system ('echo "" > "' + self .local_logfile_path + '"' )
97114 try :
98115 if out :
99116 output_string = out .decode ()
0 commit comments