From 4521e30c672a593e9d57d1e3473620ba7eab79e6 Mon Sep 17 00:00:00 2001 From: Filip Rechtoris Date: Tue, 26 Jan 2016 10:44:17 +0100 Subject: [PATCH] Installation of Chromedriver via HTTP proxy Option proxy-settings was added to installation options of ChromeDriver_installer --- setup.py | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/setup.py b/setup.py index ce42342..545277c 100644 --- a/setup.py +++ b/setup.py @@ -30,16 +30,23 @@ r'Latest-Release:-ChromeDriver-(\d+\.\d+)' ) +SOCKET_PATTERN = re.compile(r'[0-9]+(?:\.[0-9]+){3}:[0-9]+') + # Global variables chromedriver_version = None chromedriver_checksums = None +http_proxy = None def get_chromedriver_version(): """Retrieves the most recent chromedriver version.""" - global chromedriver_version + global chromedriver_version, http_proxy + - response = request.urlopen(CHROMEDRIVER_INFO_URL) + if http_proxy: + response = urllib.urlopen(CHROMEDRIVER_INFO_URL, proxies=http_proxy) + else: + response = request.urlopen(CHROMEDRIVER_INFO_URL) content = response.read() match = CROMEDRIVER_LATEST_VERSION_PATTERN.search(str(content)) if match: @@ -84,7 +91,12 @@ def reporthoook(x, y, z): sys.stdout.write(' OK') sys.stdout.flush() - request.urlretrieve(url, zip_path, reporthoook) + if http_proxy: + opener = urllib.URLopener(proxies=http_proxy) + else: + opener = urllib.URLopener() + + opener.retrieve(url, zip_path, reporthoook) print('') if not download_ok: @@ -107,7 +119,7 @@ def _validate(self, zip_path): return checksum in chromedriver_checksums def run(self): - global chromedriver_version, chromedriver_checksums + global chromedriver_version, chromedriver_checksums, http_proxy validate = False @@ -162,15 +174,17 @@ class Install(install): user_options = install.user_options + [ ('chromedriver-version=', None, 'Chromedriver version'), ('chromedriver-checksums=', None, 'Chromedriver checksums'), + ('proxy-settings=', None, 'HTTP proxy for downloading ChromeDriver'), ] def initialize_options(self): self.chromedriver_version = None self.chromedriver_checksums = [] + self.proxy_settings = None install.initialize_options(self) def run(self): - global chromedriver_version, chromedriver_checksums + global chromedriver_version, chromedriver_checksums, http_proxy if self.chromedriver_version: if not CHROMEDRIVER_VERSION_PATTERN.match(self.chromedriver_version): @@ -185,8 +199,16 @@ def run(self): chromedriver_checksums = [ch.strip() for ch in self.chromedriver_checksums.split(',')] - install.run(self) + if self.proxy_settings != None: + if not SOCKET_PATTERN.match(self.proxy_settings): + raise Exception('Invalid --proxy_settings={0}! ' + 'Must match /{1}/' + .format(self.proxy_settings, + SOCKET_PATTERN.pattern)) + else: + http_proxy = {'http': 'http://'+self.proxy_settings} + install.run(self) setup( name='chromedriver_installer',