From b6c734f045c341ec6ac97528832dc19355cb57ba Mon Sep 17 00:00:00 2001 From: Pulkit Sharma Date: Mon, 2 Jan 2017 18:40:05 +0530 Subject: [PATCH 1/3] Re download binary if it is corrupt --- browserstack/local_binary.py | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/browserstack/local_binary.py b/browserstack/local_binary.py index 9b71a20..8c145da 100644 --- a/browserstack/local_binary.py +++ b/browserstack/local_binary.py @@ -1,4 +1,4 @@ -import platform, os, sys, zipfile, stat, tempfile +import platform, os, sys, zipfile, stat, tempfile, re, subprocess from browserstack.bserrors import BrowserStackLocalError try: @@ -81,12 +81,33 @@ def download(self, chunk_size=8192, progress_hook=None): os.chmod(final_path, st.st_mode | stat.S_IXUSR) return final_path + def __verify_binary(self,path): + try: + binary_response = subprocess.check_output([path,"--version"]) + pattern = re.compile("BrowserStack Local version \d+\.\d+") + return bool(pattern.match(binary_response)) + except: + return False + def get_binary(self): dest_parent_dir = os.path.join(os.path.expanduser('~'), '.browserstack') if not os.path.exists(dest_parent_dir): os.makedirs(dest_parent_dir) bsfiles = [f for f in os.listdir(dest_parent_dir) if f.startswith('BrowserStackLocal')] + if len(bsfiles) == 0: - return self.download() + binary_path = self.download() else: - return os.path.join(dest_parent_dir, bsfiles[0]) + binary_path = os.path.join(dest_parent_dir, bsfiles[0]) + + valid_binary = self.__verify_binary(binary_path) + if valid_binary: + return binary_path + else: + binary_path = self.download() + valid_binary = self.__verify_binary(binary_path) + if valid_binary: + return binary_path + else: + raise BrowserStackLocalError('BrowserStack Local binary is corrupt') + From 5fda9a488b3dfa5b12ac5f16a83f4aae962c894f Mon Sep 17 00:00:00 2001 From: Pulkit Sharma Date: Mon, 2 Jan 2017 19:01:42 +0530 Subject: [PATCH 2/3] Printing the exception,if Binary is corrupt --- browserstack/local_binary.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/browserstack/local_binary.py b/browserstack/local_binary.py index 8c145da..aa150ab 100644 --- a/browserstack/local_binary.py +++ b/browserstack/local_binary.py @@ -1,4 +1,4 @@ -import platform, os, sys, zipfile, stat, tempfile, re, subprocess +import platform, os, sys, zipfile, stat, tempfile, re, subprocess, traceback from browserstack.bserrors import BrowserStackLocalError try: @@ -87,6 +87,7 @@ def __verify_binary(self,path): pattern = re.compile("BrowserStack Local version \d+\.\d+") return bool(pattern.match(binary_response)) except: + traceback.print_exc() return False def get_binary(self): From ac3e454f3f2be263c23c277fd4000de042fde6e0 Mon Sep 17 00:00:00 2001 From: Pulkit Sharma Date: Mon, 2 Jan 2017 19:14:38 +0530 Subject: [PATCH 3/3] Python3 check_output responds with bytes --- browserstack/local_binary.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/browserstack/local_binary.py b/browserstack/local_binary.py index aa150ab..131878f 100644 --- a/browserstack/local_binary.py +++ b/browserstack/local_binary.py @@ -1,4 +1,4 @@ -import platform, os, sys, zipfile, stat, tempfile, re, subprocess, traceback +import platform, os, sys, zipfile, stat, tempfile, re, subprocess from browserstack.bserrors import BrowserStackLocalError try: @@ -83,11 +83,10 @@ def download(self, chunk_size=8192, progress_hook=None): def __verify_binary(self,path): try: - binary_response = subprocess.check_output([path,"--version"]) + binary_response = subprocess.check_output([path,"--version"]).decode("utf-8") pattern = re.compile("BrowserStack Local version \d+\.\d+") return bool(pattern.match(binary_response)) except: - traceback.print_exc() return False def get_binary(self):