Skip to content

Commit 7a5ca0d

Browse files
authored
Merge pull request #17 from browserstack/retry_on_invalid
download binary again if it is corrupt.
2 parents 58c59b6 + ac3e454 commit 7a5ca0d

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

browserstack/local_binary.py

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import platform, os, sys, zipfile, stat, tempfile
1+
import platform, os, sys, zipfile, stat, tempfile, re, subprocess
22
from browserstack.bserrors import BrowserStackLocalError
33

44
try:
@@ -81,12 +81,33 @@ def download(self, chunk_size=8192, progress_hook=None):
8181
os.chmod(final_path, st.st_mode | stat.S_IXUSR)
8282
return final_path
8383

84+
def __verify_binary(self,path):
85+
try:
86+
binary_response = subprocess.check_output([path,"--version"]).decode("utf-8")
87+
pattern = re.compile("BrowserStack Local version \d+\.\d+")
88+
return bool(pattern.match(binary_response))
89+
except:
90+
return False
91+
8492
def get_binary(self):
8593
dest_parent_dir = os.path.join(os.path.expanduser('~'), '.browserstack')
8694
if not os.path.exists(dest_parent_dir):
8795
os.makedirs(dest_parent_dir)
8896
bsfiles = [f for f in os.listdir(dest_parent_dir) if f.startswith('BrowserStackLocal')]
97+
8998
if len(bsfiles) == 0:
90-
return self.download()
99+
binary_path = self.download()
91100
else:
92-
return os.path.join(dest_parent_dir, bsfiles[0])
101+
binary_path = os.path.join(dest_parent_dir, bsfiles[0])
102+
103+
valid_binary = self.__verify_binary(binary_path)
104+
if valid_binary:
105+
return binary_path
106+
else:
107+
binary_path = self.download()
108+
valid_binary = self.__verify_binary(binary_path)
109+
if valid_binary:
110+
return binary_path
111+
else:
112+
raise BrowserStackLocalError('BrowserStack Local binary is corrupt')
113+

0 commit comments

Comments
 (0)