|
1 |
| -import platform, os, sys, zipfile, stat, tempfile |
| 1 | +import platform, os, sys, zipfile, stat, tempfile, re, subprocess |
2 | 2 | from browserstack.bserrors import BrowserStackLocalError
|
3 | 3 |
|
4 | 4 | try:
|
@@ -81,12 +81,33 @@ def download(self, chunk_size=8192, progress_hook=None):
|
81 | 81 | os.chmod(final_path, st.st_mode | stat.S_IXUSR)
|
82 | 82 | return final_path
|
83 | 83 |
|
| 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 | + |
84 | 92 | def get_binary(self):
|
85 | 93 | dest_parent_dir = os.path.join(os.path.expanduser('~'), '.browserstack')
|
86 | 94 | if not os.path.exists(dest_parent_dir):
|
87 | 95 | os.makedirs(dest_parent_dir)
|
88 | 96 | bsfiles = [f for f in os.listdir(dest_parent_dir) if f.startswith('BrowserStackLocal')]
|
| 97 | + |
89 | 98 | if len(bsfiles) == 0:
|
90 |
| - return self.download() |
| 99 | + binary_path = self.download() |
91 | 100 | 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