From a9329d84c505585d644c23a6f0396ff5afdea59a Mon Sep 17 00:00:00 2001 From: Nels Chantarotwong <43188172+nchantarotwong@users.noreply.github.com> Date: Thu, 23 Mar 2023 10:55:34 -0700 Subject: [PATCH 1/4] added proxy support to BS local download --- browserstack/local_binary.py | 238 ++++++++++++++++++----------------- 1 file changed, 125 insertions(+), 113 deletions(-) diff --git a/browserstack/local_binary.py b/browserstack/local_binary.py index 4fe0741..36005eb 100644 --- a/browserstack/local_binary.py +++ b/browserstack/local_binary.py @@ -1,122 +1,134 @@ -import platform, os, sys, stat, tempfile, re, subprocess -from browserstack.bserrors import BrowserStackLocalError +import os +import sys +import platform +import re +import stat +import subprocess +import tempfile +import urllib3 -try: - from urllib.request import urlopen -except ImportError: - from urllib2 import urlopen +from browserstack.bserrors import BrowserStackLocalError class LocalBinary: - def __init__(self): - is_64bits = sys.maxsize > 2**32 - self.is_windows = False - osname = platform.system() - if osname == 'Darwin': - self.http_path = "https://bstack-local-prod.s3.amazonaws.com/BrowserStackLocal-darwin-x64" - elif osname == 'Linux': - if self.is_alpine(): - self.http_path = "https://bstack-local-prod.s3.amazonaws.com/BrowserStackLocal-alpine" - else: - if is_64bits: - self.http_path = "https://bstack-local-prod.s3.amazonaws.com/BrowserStackLocal-linux-x64" + """BrowserStack LocalBinary class to download to local, also supporting proxies.""" + def __init__(self, proxy=None): + is_64bits = sys.maxsize > 2 ** 32 + self.is_windows = False + self.proxy = proxy + osname = platform.system() + if osname == 'Darwin': + self.http_path = "https://bstack-local-prod.s3.amazonaws.com/BrowserStackLocal-darwin-x64" + elif osname == 'Linux': + if self.is_alpine(): + self.http_path = "https://bstack-local-prod.s3.amazonaws.com/BrowserStackLocal-alpine" + else: + if is_64bits: + self.http_path = "https://bstack-local-prod.s3.amazonaws.com/BrowserStackLocal-linux-x64" + else: + self.http_path = "https://bstack-local-prod.s3.amazonaws.com/BrowserStackLocal-linux-ia32" + else: + self.is_windows = True + self.http_path = "https://bstack-local-prod.s3.amazonaws.com/BrowserStackLocal.exe" + + self.ordered_paths = [ + os.path.join(os.path.expanduser('~'), '.browserstack'), + os.getcwd(), + tempfile.gettempdir() + ] + self.path_index = 0 + + + def __available_dir(self): + while self.path_index < len(self.ordered_paths): + if self.__make_path(self.ordered_paths[self.path_index]): + final_path = self.ordered_paths[self.path_index] + self.path_index += 1 + return final_path + else: + self.path_index += 1 + raise BrowserStackLocalError('Error trying to download BrowserStack Local binary') + + + def is_alpine(self): + grepOutput = subprocess.run("gfrep -w NAME /etc/os-release", capture_output=True, shell=True) + if grepOutput.stdout.decode('utf-8').find('Alpine') > -1: + return True + return False + + + def __make_path(self, dest_path): + try: + if not os.path.exists(dest_path): + os.makedirs(dest_path) + return True + except: + return False + + + def download(self, chunk_size=8192, progress_hook=None): + print(f'Downloading BrowserStack Local binary, proxy ({self.proxy}).', flush=True) + if self.proxy: + proxy = urllib3.ProxyManager(self.proxy) + response = proxy.request('GET', self.http_path, preload_content=False) else: - self.http_path = "https://bstack-local-prod.s3.amazonaws.com/BrowserStackLocal-linux-ia32" - else: - self.is_windows = True - self.http_path = "https://bstack-local-prod.s3.amazonaws.com/BrowserStackLocal.exe" - - self.ordered_paths = [ - os.path.join(os.path.expanduser('~'), '.browserstack'), - os.getcwd(), - tempfile.gettempdir() - ] - self.path_index = 0 - - def is_alpine(self): - grepOutput = subprocess.run("gfrep -w NAME /etc/os-release", capture_output=True, shell=True) - if grepOutput.stdout.decode('utf-8').find('Alpine') > -1: - return True - return False - - def __make_path(self, dest_path): - try: - if not os.path.exists(dest_path): - os.makedirs(dest_path) - return True - except: - return False - - def __available_dir(self): - while self.path_index < len(self.ordered_paths): - if self.__make_path(self.ordered_paths[self.path_index]): - final_path = self.ordered_paths[self.path_index] - self.path_index += 1 + http = urllib3.PoolManager() + response = http.request('GET', self.http_path, preload_content=False) + + total_size = int(response.headers['Content-Length'].strip()) + bytes_so_far = 0 + + dest_parent_dir = self.__available_dir() + dest_binary_name = 'BrowserStackLocal' + if self.is_windows: + dest_binary_name += '.exe' + + with open(os.path.join(dest_parent_dir, dest_binary_name), 'wb') as local_file: + for chunk in response.stream(chunk_size): + bytes_so_far += len(chunk) + if not chunk: + break + + if progress_hook: + progress_hook(bytes_so_far, chunk_size, total_size) + + try: + local_file.write(chunk) + except: + return self.download(chunk_size, progress_hook) + + final_path = os.path.join(dest_parent_dir, dest_binary_name) + st = os.stat(final_path) + os.chmod(final_path, st.st_mode | stat.S_IXUSR) return final_path - else: - self.path_index += 1 - raise BrowserStackLocalError('Error trying to download BrowserStack Local binary') - - def download(self, chunk_size=8192, progress_hook=None): - response = urlopen(self.http_path) - try: - total_size = int(response.info().getheader('Content-Length').strip()) - except: - total_size = int(response.info().get_all('Content-Length')[0].strip()) - bytes_so_far = 0 - - dest_parent_dir = self.__available_dir() - dest_binary_name = 'BrowserStackLocal' - if self.is_windows: - dest_binary_name += '.exe' - - with open(os.path.join(dest_parent_dir, dest_binary_name), 'wb') as local_file: - while True: - chunk = response.read(chunk_size) - bytes_so_far += len(chunk) - - if not chunk: - break - - if progress_hook: - progress_hook(bytes_so_far, chunk_size, total_size) + + def __verify_binary(self, path): try: - local_file.write(chunk) + 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: - return self.download(chunk_size, progress_hook) - - final_path = os.path.join(dest_parent_dir, dest_binary_name) - st = os.stat(final_path) - 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"]).decode("utf-8") - 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: - binary_path = self.download() - else: - 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') + 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: + binary_path = self.download() + else: + 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 2853ebab45381c99c3f1674464303cc39309164d Mon Sep 17 00:00:00 2001 From: Nels Chantarotwong <43188172+nchantarotwong@users.noreply.github.com> Date: Thu, 23 Mar 2023 10:57:24 -0700 Subject: [PATCH 2/4] converted the indents --- browserstack/local_binary.py | 244 +++++++++++++++++------------------ 1 file changed, 122 insertions(+), 122 deletions(-) diff --git a/browserstack/local_binary.py b/browserstack/local_binary.py index 36005eb..556dc58 100644 --- a/browserstack/local_binary.py +++ b/browserstack/local_binary.py @@ -10,125 +10,125 @@ from browserstack.bserrors import BrowserStackLocalError class LocalBinary: - """BrowserStack LocalBinary class to download to local, also supporting proxies.""" - def __init__(self, proxy=None): - is_64bits = sys.maxsize > 2 ** 32 - self.is_windows = False - self.proxy = proxy - osname = platform.system() - if osname == 'Darwin': - self.http_path = "https://bstack-local-prod.s3.amazonaws.com/BrowserStackLocal-darwin-x64" - elif osname == 'Linux': - if self.is_alpine(): - self.http_path = "https://bstack-local-prod.s3.amazonaws.com/BrowserStackLocal-alpine" - else: - if is_64bits: - self.http_path = "https://bstack-local-prod.s3.amazonaws.com/BrowserStackLocal-linux-x64" - else: - self.http_path = "https://bstack-local-prod.s3.amazonaws.com/BrowserStackLocal-linux-ia32" - else: - self.is_windows = True - self.http_path = "https://bstack-local-prod.s3.amazonaws.com/BrowserStackLocal.exe" - - self.ordered_paths = [ - os.path.join(os.path.expanduser('~'), '.browserstack'), - os.getcwd(), - tempfile.gettempdir() - ] - self.path_index = 0 - - - def __available_dir(self): - while self.path_index < len(self.ordered_paths): - if self.__make_path(self.ordered_paths[self.path_index]): - final_path = self.ordered_paths[self.path_index] - self.path_index += 1 - return final_path - else: - self.path_index += 1 - raise BrowserStackLocalError('Error trying to download BrowserStack Local binary') - - - def is_alpine(self): - grepOutput = subprocess.run("gfrep -w NAME /etc/os-release", capture_output=True, shell=True) - if grepOutput.stdout.decode('utf-8').find('Alpine') > -1: - return True - return False - - - def __make_path(self, dest_path): - try: - if not os.path.exists(dest_path): - os.makedirs(dest_path) - return True - except: - return False - - - def download(self, chunk_size=8192, progress_hook=None): - print(f'Downloading BrowserStack Local binary, proxy ({self.proxy}).', flush=True) - if self.proxy: - proxy = urllib3.ProxyManager(self.proxy) - response = proxy.request('GET', self.http_path, preload_content=False) - else: - http = urllib3.PoolManager() - response = http.request('GET', self.http_path, preload_content=False) - - total_size = int(response.headers['Content-Length'].strip()) - bytes_so_far = 0 - - dest_parent_dir = self.__available_dir() - dest_binary_name = 'BrowserStackLocal' - if self.is_windows: - dest_binary_name += '.exe' - - with open(os.path.join(dest_parent_dir, dest_binary_name), 'wb') as local_file: - for chunk in response.stream(chunk_size): - bytes_so_far += len(chunk) - if not chunk: - break - - if progress_hook: - progress_hook(bytes_so_far, chunk_size, total_size) - - try: - local_file.write(chunk) - except: - return self.download(chunk_size, progress_hook) - - final_path = os.path.join(dest_parent_dir, dest_binary_name) - st = os.stat(final_path) - 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"]).decode("utf-8") - 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: - binary_path = self.download() - else: - 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') + """BrowserStack LocalBinary class to download to local, also supporting proxies.""" + def __init__(self, proxy=None): + is_64bits = sys.maxsize > 2 ** 32 + self.is_windows = False + self.proxy = proxy + osname = platform.system() + if osname == 'Darwin': + self.http_path = "https://bstack-local-prod.s3.amazonaws.com/BrowserStackLocal-darwin-x64" + elif osname == 'Linux': + if self.is_alpine(): + self.http_path = "https://bstack-local-prod.s3.amazonaws.com/BrowserStackLocal-alpine" + else: + if is_64bits: + self.http_path = "https://bstack-local-prod.s3.amazonaws.com/BrowserStackLocal-linux-x64" + else: + self.http_path = "https://bstack-local-prod.s3.amazonaws.com/BrowserStackLocal-linux-ia32" + else: + self.is_windows = True + self.http_path = "https://bstack-local-prod.s3.amazonaws.com/BrowserStackLocal.exe" + + self.ordered_paths = [ + os.path.join(os.path.expanduser('~'), '.browserstack'), + os.getcwd(), + tempfile.gettempdir() + ] + self.path_index = 0 + + + def __available_dir(self): + while self.path_index < len(self.ordered_paths): + if self.__make_path(self.ordered_paths[self.path_index]): + final_path = self.ordered_paths[self.path_index] + self.path_index += 1 + return final_path + else: + self.path_index += 1 + raise BrowserStackLocalError('Error trying to download BrowserStack Local binary') + + + def is_alpine(self): + grepOutput = subprocess.run("gfrep -w NAME /etc/os-release", capture_output=True, shell=True) + if grepOutput.stdout.decode('utf-8').find('Alpine') > -1: + return True + return False + + + def __make_path(self, dest_path): + try: + if not os.path.exists(dest_path): + os.makedirs(dest_path) + return True + except: + return False + + + def download(self, chunk_size=8192, progress_hook=None): + print(f'Downloading BrowserStack Local binary, proxy ({self.proxy}).', flush=True) + if self.proxy: + proxy = urllib3.ProxyManager(self.proxy) + response = proxy.request('GET', self.http_path, preload_content=False) + else: + http = urllib3.PoolManager() + response = http.request('GET', self.http_path, preload_content=False) + + total_size = int(response.headers['Content-Length'].strip()) + bytes_so_far = 0 + + dest_parent_dir = self.__available_dir() + dest_binary_name = 'BrowserStackLocal' + if self.is_windows: + dest_binary_name += '.exe' + + with open(os.path.join(dest_parent_dir, dest_binary_name), 'wb') as local_file: + for chunk in response.stream(chunk_size): + bytes_so_far += len(chunk) + if not chunk: + break + + if progress_hook: + progress_hook(bytes_so_far, chunk_size, total_size) + + try: + local_file.write(chunk) + except: + return self.download(chunk_size, progress_hook) + + final_path = os.path.join(dest_parent_dir, dest_binary_name) + st = os.stat(final_path) + 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"]).decode("utf-8") + 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: + binary_path = self.download() + else: + 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 ab6e091f136f19adc0df3dd897bd6011280018ab Mon Sep 17 00:00:00 2001 From: Nels Chantarotwong <43188172+nchantarotwong@users.noreply.github.com> Date: Thu, 23 Mar 2023 10:59:13 -0700 Subject: [PATCH 3/4] switch back to spaces --- browserstack/local_binary.py | 244 +++++++++++++++++------------------ 1 file changed, 122 insertions(+), 122 deletions(-) diff --git a/browserstack/local_binary.py b/browserstack/local_binary.py index 556dc58..36005eb 100644 --- a/browserstack/local_binary.py +++ b/browserstack/local_binary.py @@ -10,125 +10,125 @@ from browserstack.bserrors import BrowserStackLocalError class LocalBinary: - """BrowserStack LocalBinary class to download to local, also supporting proxies.""" - def __init__(self, proxy=None): - is_64bits = sys.maxsize > 2 ** 32 - self.is_windows = False - self.proxy = proxy - osname = platform.system() - if osname == 'Darwin': - self.http_path = "https://bstack-local-prod.s3.amazonaws.com/BrowserStackLocal-darwin-x64" - elif osname == 'Linux': - if self.is_alpine(): - self.http_path = "https://bstack-local-prod.s3.amazonaws.com/BrowserStackLocal-alpine" - else: - if is_64bits: - self.http_path = "https://bstack-local-prod.s3.amazonaws.com/BrowserStackLocal-linux-x64" - else: - self.http_path = "https://bstack-local-prod.s3.amazonaws.com/BrowserStackLocal-linux-ia32" - else: - self.is_windows = True - self.http_path = "https://bstack-local-prod.s3.amazonaws.com/BrowserStackLocal.exe" - - self.ordered_paths = [ - os.path.join(os.path.expanduser('~'), '.browserstack'), - os.getcwd(), - tempfile.gettempdir() - ] - self.path_index = 0 - - - def __available_dir(self): - while self.path_index < len(self.ordered_paths): - if self.__make_path(self.ordered_paths[self.path_index]): - final_path = self.ordered_paths[self.path_index] - self.path_index += 1 - return final_path - else: - self.path_index += 1 - raise BrowserStackLocalError('Error trying to download BrowserStack Local binary') - - - def is_alpine(self): - grepOutput = subprocess.run("gfrep -w NAME /etc/os-release", capture_output=True, shell=True) - if grepOutput.stdout.decode('utf-8').find('Alpine') > -1: - return True - return False - - - def __make_path(self, dest_path): - try: - if not os.path.exists(dest_path): - os.makedirs(dest_path) - return True - except: - return False - - - def download(self, chunk_size=8192, progress_hook=None): - print(f'Downloading BrowserStack Local binary, proxy ({self.proxy}).', flush=True) - if self.proxy: - proxy = urllib3.ProxyManager(self.proxy) - response = proxy.request('GET', self.http_path, preload_content=False) - else: - http = urllib3.PoolManager() - response = http.request('GET', self.http_path, preload_content=False) - - total_size = int(response.headers['Content-Length'].strip()) - bytes_so_far = 0 - - dest_parent_dir = self.__available_dir() - dest_binary_name = 'BrowserStackLocal' - if self.is_windows: - dest_binary_name += '.exe' - - with open(os.path.join(dest_parent_dir, dest_binary_name), 'wb') as local_file: - for chunk in response.stream(chunk_size): - bytes_so_far += len(chunk) - if not chunk: - break - - if progress_hook: - progress_hook(bytes_so_far, chunk_size, total_size) - - try: - local_file.write(chunk) - except: - return self.download(chunk_size, progress_hook) - - final_path = os.path.join(dest_parent_dir, dest_binary_name) - st = os.stat(final_path) - 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"]).decode("utf-8") - 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: - binary_path = self.download() - else: - 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') + """BrowserStack LocalBinary class to download to local, also supporting proxies.""" + def __init__(self, proxy=None): + is_64bits = sys.maxsize > 2 ** 32 + self.is_windows = False + self.proxy = proxy + osname = platform.system() + if osname == 'Darwin': + self.http_path = "https://bstack-local-prod.s3.amazonaws.com/BrowserStackLocal-darwin-x64" + elif osname == 'Linux': + if self.is_alpine(): + self.http_path = "https://bstack-local-prod.s3.amazonaws.com/BrowserStackLocal-alpine" + else: + if is_64bits: + self.http_path = "https://bstack-local-prod.s3.amazonaws.com/BrowserStackLocal-linux-x64" + else: + self.http_path = "https://bstack-local-prod.s3.amazonaws.com/BrowserStackLocal-linux-ia32" + else: + self.is_windows = True + self.http_path = "https://bstack-local-prod.s3.amazonaws.com/BrowserStackLocal.exe" + + self.ordered_paths = [ + os.path.join(os.path.expanduser('~'), '.browserstack'), + os.getcwd(), + tempfile.gettempdir() + ] + self.path_index = 0 + + + def __available_dir(self): + while self.path_index < len(self.ordered_paths): + if self.__make_path(self.ordered_paths[self.path_index]): + final_path = self.ordered_paths[self.path_index] + self.path_index += 1 + return final_path + else: + self.path_index += 1 + raise BrowserStackLocalError('Error trying to download BrowserStack Local binary') + + + def is_alpine(self): + grepOutput = subprocess.run("gfrep -w NAME /etc/os-release", capture_output=True, shell=True) + if grepOutput.stdout.decode('utf-8').find('Alpine') > -1: + return True + return False + + + def __make_path(self, dest_path): + try: + if not os.path.exists(dest_path): + os.makedirs(dest_path) + return True + except: + return False + + + def download(self, chunk_size=8192, progress_hook=None): + print(f'Downloading BrowserStack Local binary, proxy ({self.proxy}).', flush=True) + if self.proxy: + proxy = urllib3.ProxyManager(self.proxy) + response = proxy.request('GET', self.http_path, preload_content=False) + else: + http = urllib3.PoolManager() + response = http.request('GET', self.http_path, preload_content=False) + + total_size = int(response.headers['Content-Length'].strip()) + bytes_so_far = 0 + + dest_parent_dir = self.__available_dir() + dest_binary_name = 'BrowserStackLocal' + if self.is_windows: + dest_binary_name += '.exe' + + with open(os.path.join(dest_parent_dir, dest_binary_name), 'wb') as local_file: + for chunk in response.stream(chunk_size): + bytes_so_far += len(chunk) + if not chunk: + break + + if progress_hook: + progress_hook(bytes_so_far, chunk_size, total_size) + + try: + local_file.write(chunk) + except: + return self.download(chunk_size, progress_hook) + + final_path = os.path.join(dest_parent_dir, dest_binary_name) + st = os.stat(final_path) + 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"]).decode("utf-8") + 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: + binary_path = self.download() + else: + 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 fa53353919d9a7b9733bb012e16a2a635a7d4d7c Mon Sep 17 00:00:00 2001 From: Nels Chantarotwong Date: Thu, 28 Sep 2023 12:07:51 -0700 Subject: [PATCH 4/4] support proxy to download the actual BS local binary -fixed the indents --- browserstack/local_binary.py | 104 ++++++++++++----------------------- 1 file changed, 35 insertions(+), 69 deletions(-) diff --git a/browserstack/local_binary.py b/browserstack/local_binary.py index 8f3373d..2fcdf1f 100644 --- a/browserstack/local_binary.py +++ b/browserstack/local_binary.py @@ -10,33 +10,34 @@ from browserstack.bserrors import BrowserStackLocalError class LocalBinary: - def __init__(self): - is_64bits = sys.maxsize > 2**32 - self.is_windows = False - osname = platform.system() - if osname == 'Darwin': - self.http_path = "https://www.browserstack.com/local-testing/downloads/binaries/BrowserStackLocal-darwin-x64" - elif osname == 'Linux': - if self.is_alpine(): - self.http_path = "https://www.browserstack.com/local-testing/downloads/binaries/BrowserStackLocal-alpine" - else: - if is_64bits: - self.http_path = "https://www.browserstack.com/local-testing/downloads/binaries/BrowserStackLocal-linux-x64" + def __init__(self, proxy=None): + is_64bits = sys.maxsize > 2 ** 32 + self.is_windows = False + self.proxy = proxy + osname = platform.system() + if osname == 'Darwin': + self.http_path = "https://www.browserstack.com/local-testing/downloads/binaries/BrowserStackLocal-darwin-x64" + elif osname == 'Linux': + if self.is_alpine(): + self.http_path = "https://www.browserstack.com/local-testing/downloads/binaries/BrowserStackLocal-alpine" + else: + if is_64bits: + self.http_path = "https://www.browserstack.com/local-testing/downloads/binaries/BrowserStackLocal-linux-x64" + else: + self.http_path = "https://www.browserstack.com/local-testing/downloads/binaries/BrowserStackLocal-linux-ia32" else: - self.http_path = "https://www.browserstack.com/local-testing/downloads/binaries/BrowserStackLocal-linux-ia32" - else: - self.is_windows = True - self.http_path = "https://www.browserstack.com/local-testing/downloads/binaries/BrowserStackLocal.exe" + self.is_windows = True + self.http_path = "https://www.browserstack.com/local-testing/downloads/binaries/BrowserStackLocal.exe" - self.ordered_paths = [ + self.ordered_paths = [ os.path.join(os.path.expanduser('~'), '.browserstack'), os.getcwd(), tempfile.gettempdir() - ] - self.path_index = 0 - + ] + self.path_index = 0 def __available_dir(self): + """Need to re-define this, otherwise the namespace is wrong when calling it.""" while self.path_index < len(self.ordered_paths): if self.__make_path(self.ordered_paths[self.path_index]): final_path = self.ordered_paths[self.path_index] @@ -46,15 +47,14 @@ def __available_dir(self): self.path_index += 1 raise BrowserStackLocalError('Error trying to download BrowserStack Local binary') - def is_alpine(self): grepOutput = subprocess.run("gfrep -w NAME /etc/os-release", capture_output=True, shell=True) if grepOutput.stdout.decode('utf-8').find('Alpine') > -1: return True return False - def __make_path(self, dest_path): + """Need to re-define this, otherwise the namespace is wrong when calling it.""" try: if not os.path.exists(dest_path): os.makedirs(dest_path) @@ -62,55 +62,22 @@ def __make_path(self, dest_path): except: return False + def download(self): + print(f'Downloading BrowserStack Local binary ({self.http_path}), proxy ({self.proxy}).', flush=True) + dest_parent_dir = self.__available_dir() + dest_binary_name = 'BrowserStackLocal' + if self.is_windows: + dest_binary_name += '.exe' - def download(self, chunk_size=8192, progress_hook=None): - print(f'Downloading BrowserStack Local binary, proxy ({self.proxy}).', flush=True) - if self.proxy: - proxy = urllib3.ProxyManager(self.proxy) - response = proxy.request('GET', self.http_path, preload_content=False) - else: - http = urllib3.PoolManager() - response = http.request('GET', self.http_path, preload_content=False) - - total_size = int(response.headers['Content-Length'].strip()) - bytes_so_far = 0 - - 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) - binary_name = 'BrowserStackLocal.exe' if self.is_windows else 'BrowserStackLocal' - bsfiles = [f for f in os.listdir(dest_parent_dir) if f == binary_name] - - if len(bsfiles) == 0: - binary_path = self.download() - else: - binary_path = os.path.join(dest_parent_dir, bsfiles[0]) - - dest_parent_dir = self.__available_dir() - dest_binary_name = 'BrowserStackLocal' - if self.is_windows: - dest_binary_name += '.exe' - - with open(os.path.join(dest_parent_dir, dest_binary_name), 'wb') as local_file: - for chunk in response.stream(chunk_size): - bytes_so_far += len(chunk) - if not chunk: - break + final_path = os.path.join(dest_parent_dir, dest_binary_name) + with requests.get(self.http_path, proxies=self.proxy, headers={'User-Agent': 'Chrome'}, stream=True) as r: + with open(final_path, 'wb') as f: + shutil.copyfileobj(r.raw, f) - if progress_hook: - progress_hook(bytes_so_far, chunk_size, total_size) - - try: - local_file.write(chunk) - except: - return self.download(chunk_size, progress_hook) - - final_path = os.path.join(dest_parent_dir, dest_binary_name) - st = os.stat(final_path) - os.chmod(final_path, st.st_mode | stat.S_IXUSR) - return final_path + st = os.stat(final_path) + os.chmod(final_path, st.st_mode | stat.S_IXUSR) + return final_path def __verify_binary(self, path): try: @@ -120,7 +87,6 @@ def __verify_binary(self, path): 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): pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy