Skip to content

Instantly share code, notes, and snippets.

@breuerfelix
Last active September 22, 2018 14:27
Show Gist options
  • Save breuerfelix/e55b2b618ea2e6b1c2006b473e6ad755 to your computer and use it in GitHub Desktop.
Save breuerfelix/e55b2b618ea2e6b1c2006b473e6ad755 to your computer and use it in GitHub Desktop.
routine which updates chromedriver-py package on its own
# constants
CHROME_DL_URL = 'https://chromedriver.storage.googleapis.com'
CHROME_URL = 'http://chromedriver.chromium.org/downloads'
REPO_PATH = './GitHub/chromedriver-py/'
VERSION_FILE = './GitHub/chromedriver-py/version.txt'
DOWNLOAD_DIR = './'
CURRENT_VERSION = []
UPDATE_SCRIPT = './update_chromedriver.sh'
# chromedriver versions for downloading only
DRIVER = 'chromedriver_'
ZIP = '.zip'
vMAC = DRIVER + 'mac64' + ZIP
vWINDOWS = DRIVER + 'win32' + ZIP
vLINUX = DRIVER + 'linux64' + ZIP
PLATFORMS = [ vMAC, vLINUX, vWINDOWS ]
import os
if not CURRENT_VERSION:
with open(VERSION_FILE) as f:
file_content = f.read()
if file_content:
CURRENT_VERSION = file_content.strip().split('.')
else:
CURRENT_VERSION = [0, 0]
print(CURRENT_VERSION)
import requests
from bs4 import BeautifulSoup as bs
import urllib
page = requests.get(CHROME_URL)
html = page.content
soup = bs(html, 'html.parser')
latest = soup.find(id='sites-canvas-main-content').table.tbody.tr.td.div.find_all('h2')[1].b.a
new_version = latest.text.split(' ')[1].split('.')
print(str(new_version))
# update logic
length = len(new_version)
if len(CURRENT_VERSION) < length:
length = len(CURRENT_VERSION)
update = False
for i in range(length):
new = int(new_version[i])
old = int(CURRENT_VERSION[i])
if new > old:
update = True
break
if update:
for pf in PLATFORMS:
url = CHROME_DL_URL + '/' + '.'.join(new_version) + '/' + pf
print(url)
# downloads the files
file = urllib.request.urlopen(url)
path = os.path.join(DOWNLOAD_DIR, pf)
with open(path,'wb') as output:
output.write(file.read())
# unzip file
import zipfile
with zipfile.ZipFile(path,"r") as zip_ref:
zip_ref.extractall(DOWNLOAD_DIR)
# rename file
if 'linux' in pf:
os.rename(os.path.join(DOWNLOAD_DIR, 'chromedriver'), os.path.join(DOWNLOAD_DIR, 'chromedriver_linux'))
elif 'mac' in pf:
os.rename(os.path.join(DOWNLOAD_DIR, 'chromedriver'), os.path.join(DOWNLOAD_DIR, 'chromedriver_mac'))
# delete zip file
os.remove(path)
# update version file
with open(VERSION_FILE, 'w') as vFile:
vFile.write('.'.join(new_version))
# start shell script to update package
import subprocess
subprocess.call(UPDATE_SCRIPT, shell=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment