This repository was archived by the owner on Aug 25, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathupdate.py
More file actions
executable file
·131 lines (113 loc) · 4.04 KB
/
update.py
File metadata and controls
executable file
·131 lines (113 loc) · 4.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/usr/bin/env python3
# coding: utf-8
'''
Author: Park Lam <lqmonline@gmail.com>
Copyright: Copyright 2019, unipark.io
'''
import os
import re
import requests
import json
from decouple import config
from zipfile import ZipFile
from io import BytesIO
PACKAGE_NAME = 'pychromedriver'
PACKAGE_URL = 'https://test.pypi.org/pypi/pychromedriver/json' \
if config('DEBUG', default=False, cast=bool) \
else 'https://pypi.org/pypi/pychromedriver/json'
CHROMEDRIVER_URL = 'https://chromedriver.storage.googleapis.com/'
OS_TYPES = [ 'win', 'mac', 'linux' ]
ARCHITECTURE = [ '32', '64' ]
FILE_NAME = 'chromedriver_'
FILE_EXT = '.zip'
DOWNLOAD_DIR = './pychromedriver/'
VERSION_FILE = './VERSION.txt'
MINI_VERSION = '69.0'
def compare_version(alpha, beta):
alpha_arr = alpha.split('.')
beta_arr = beta.split('.')
while alpha_arr and beta_arr:
m = int(alpha_arr.pop(0))
n = int(beta_arr.pop(0))
if m > n:
return 1
elif m < n:
return -1
while alpha_arr:
m = int(alpha_arr.pop(0))
if m > 0:
return 1
while beta_arr:
n = int(beta_arr.pop(0))
if n > 0:
return -1
return 0
def get_pip_version():
page = requests.get(PACKAGE_URL)
return json.loads(page.text)['info']['version']
def get_next_version(prev_version):
version_lst = get_chromedriver_versions()
while version_lst:
next_version = version_lst.pop(0)
if compare_version(prev_version, next_version) == -1:
return next_version
return None
def get_chromedriver_versions():
import lxml.etree as et
version_lst = []
ver_regex = r'(\w+\.?)*/chromedriver_(win32|linux32|mac32|linux64|mac64).\w{3}'
cont = requests.get(CHROMEDRIVER_URL).text
doc = et.fromstring(cont.encode('utf8'))
ns = { 's3': doc.xpath('namespace-uri(.)') }
for node in doc.xpath('//s3:Contents', namespaces=ns):
info = node.xpath('./s3:Key', namespaces=ns)[0]
result = re.match(ver_regex, info.text)
if result:
version, _ = info.text.split('/')
if len(version.split('.')) > 2:
version = '.'.join(version.split('.')[0:2])
if version not in version_lst:
version_lst.append(version)
sorted(version_lst, key=lambda v: float(v))
return version_lst
def download_chromedriver(version):
import lxml.etree as et
cont = requests.get(CHROMEDRIVER_URL).text
doc = et.fromstring(cont.encode('utf8'))
ns = { 's3': doc.xpath('namespace-uri(.)') }
ver_regex = r'%s(\.+\w+)*/chromedriver_(win32|linux32|mac32|linux64|mac64).\w{3}' % version
for node in doc.xpath('//s3:Contents/s3:Key', namespaces=ns):
if re.match(ver_regex, node.text):
url = os.path.join(CHROMEDRIVER_URL, node.text)
print("Download file: ", url)
filename = node.text.split('/').pop().split('.').pop(0)
if 'win32' in filename:
filename += '.exe'
zf = ZipFile(BytesIO(requests.get(url).content))
for f in zf.namelist():
zf.extract(f, DOWNLOAD_DIR)
os.rename(os.path.join(DOWNLOAD_DIR, f), \
os.path.join(DOWNLOAD_DIR, filename))
os.chmod(os.path.join(DOWNLOAD_DIR, filename), 0o755)
if __name__ == '__main__':
version = os.environ.get('VERSION')
force_upload = False
if version:
force_upload = True
if not force_upload:
pip_version = get_pip_version()
print('Current PyPI version:', pip_version)
if compare_version(pip_version, MINI_VERSION) == -1:
pip_version = MINI_VERSION
version = get_next_version(pip_version)
print('Version to update:', version)
if not version:
print('Latest version already.')
exit(1)
download_chromedriver(version)
with open(VERSION_FILE, 'w') as f:
f.write(version)
else:
download_chromedriver(version)
with open(VERSION_FILE, 'w') as f:
f.write(version)