-
Notifications
You must be signed in to change notification settings - Fork 3
/
update.py
executable file
·135 lines (116 loc) · 4.42 KB
/
update.py
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
132
133
134
135
#!/usr/bin/env python3
# coding: utf-8
'''
Author: Park Lam <[email protected]>
Copyright: Copyright 2019, unipark.io
'''
import os
import re
import requests
import json
import tarfile
import gzip
from decouple import config
from zipfile import ZipFile
from io import BytesIO
PACKAGE_NAME = 'pygeckodriver'
PACKAGE_URL = 'https://test.pypi.org/pypi/pygeckodriver/json' \
if config('DEBUG', default=False, cast=bool) \
else 'https://pypi.org/pypi/pygeckodriver/json'
GECKODRIVER_URL = 'https://api.github.com/repos/mozilla/geckodriver/releases'
OS_TYPES = [ 'win', 'mac', 'linux' ]
ARCHITECTURE = [ '32', '64' ]
FILE_NAME = 'geckodriver_'
DOWNLOAD_DIR = './pygeckodriver/'
VERSION_FILE = './VERSION.txt'
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_geckodriver_versions()
while version_lst:
next_version = version_lst.pop(0)['tag_name'].strip('v')
if compare_version(prev_version, next_version) == -1:
return next_version
return None
def get_geckodriver_versions():
cont = requests.get(GECKODRIVER_URL).text
data = [ i for i in json.loads(cont) if 'geckodriver' in i['assets'][0]['name'] ]
return list(reversed(data))
def download_geckodriver(version):
cont = requests.get(GECKODRIVER_URL).text
data = json.loads(cont)
for v in data:
if version == v['tag_name'].strip().strip('v'):
for asset in v['assets']:
url = asset['browser_download_url']
print("Download file: ", url)
platform = asset['name'].split('-').pop().split('.').pop(0)
if platform.lower() in ('osx', 'macos'):
filename = 'geckodriver_macos'
else:
filename = 'geckodriver_{}'.format(platform)
if 'win32' in filename \
or 'win64' in filename:
filename += '.exe'
if '.tar.gz' in asset['name']:
tf = tarfile.open(fileobj=BytesIO(requests.get(url).content))
for f in tf.getnames():
if 'geckodriver' in f:
tf.extract(f, path=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)
elif '.gz' in asset['name']:
gf = gzip.GzipFile(fileobj=BytesIO(requests.get(url).content))
with open(os.path.join(DOWNLOAD_DIR, filename), 'wb') as f:
f.write(gf.read())
os.chmod(os.path.join(DOWNLOAD_DIR, filename), 0o755)
elif '.zip' in asset['name']:
zf = ZipFile(BytesIO(requests.get(url).content))
for f in zf.namelist():
if 'geckodriver' in f:
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)
break
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)
version = get_next_version(pip_version)
print('Version to update:', version)
if not version:
print('Latest version.')
exit(1)
download_geckodriver(version)
with open(VERSION_FILE, 'w') as f:
f.write(version)
else:
download_geckodriver(version)
with open(VERSION_FILE, 'w') as f:
f.write(version)