Repository navigation
Expand file tree
/
Copy pathupdater.py
More file actions
215 lines (155 loc) · 5.51 KB
/
Copy pathupdater.py
File metadata and controls
215 lines (155 loc) · 5.51 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import os
import sys
import urllib
import zipfile
import requests
from bs4 import BeautifulSoup as bs
# constants
CHROMEDRIVER_URL = 'https://chromedriver.storage.googleapis.com/'
PLATFORMS = [ 'mac', 'win', 'linux' ]
BIT_VERSIONS = [ '64', '32' ]
CHROMEDRIVER_FILE_NAME = 'chromedriver_'
CHROMEDRIVER_EXTENSION = '.zip'
DOWNLOAD_DIR = './instapy_chromedriver/'
VERSION_FILE = './CURRENT_VERSION.txt'
def compare_int_arrays(old, new):
if len(old) != len(new): return False
for i in range(len(old)):
if new[i] == old[i]:
continue
if new[i] > old[i]:
return True
break
return False
def check_for_update(old_version_param = None):
if not old_version_param: old_version_param = '0.0'
old_version = []
try:
old_version_param = old_version_param.split('.')
for v in old_version_param:
old_version.append(int(v))
except:
print('error parsing old version!')
return None;
print('old version: ' + str(old_version))
page = requests.get(CHROMEDRIVER_URL)
html = page.content
soup = bs(html, 'lxml')
versions_to_update = set()
checked_keys = []
highest_version = []
for content in soup.select('contents'):
key = content.select('key')
if not key:
# key not found
continue
key = key[0]
key = key.get_text()
version = key.split('/')
# key is not a version
if not version:
continue
version = version[0]
# continue if version is already checked
if version in checked_keys:
continue
checked_keys.append(version)
version_list = version.split('.')
# key is no version either
if not version_list:
print('no valid version key: ' + version)
continue
# try parse version to int
versions = []
for v in version_list:
try:
versions.append(int(v))
except:
print('failed parsing to version number: ' + version)
versions = []
break
# continue if version number got non-int
if not versions:
continue
# continue if version format is different
if len(old_version) != len(versions):
print('version format does not match: ' + str(versions))
continue
# continue if version is really high, these are nightly releases
if versions[0] >= 70:
print('version to high: ' + str(versions[0]))
continue
if compare_int_arrays(old_version, versions):
versions_to_update.add('.'.join(str(x) for x in versions))
# check for highest possible version
if not highest_version:
highest_version = versions
elif compare_int_arrays(highest_version, versions):
highest_version = versions
highest_version = '.'.join(str(x) for x in highest_version)
print('versions to update: ' + str(versions_to_update))
print('highest version to update: ' + highest_version)
return highest_version, versions_to_update
def update_version(version):
for p in PLATFORMS:
for b in BIT_VERSIONS:
filename = CHROMEDRIVER_FILE_NAME + p + b
filename_version = version + '/' + filename + CHROMEDRIVER_EXTENSION
url = CHROMEDRIVER_URL + filename_version
print(url)
# downloads the files
try:
file = urllib.request.urlopen(url)
except:
print('could not get file: ' + filename)
continue
# save file to system
path = os.path.join(DOWNLOAD_DIR, filename + CHROMEDRIVER_EXTENSION)
with open(path, 'wb') as output:
print('write to: ' + path)
output.write(file.read())
# unzip file
print('unzip file: ' + path)
with zipfile.ZipFile(path, 'r') as zip_ref:
zip_ref.extractall(DOWNLOAD_DIR)
# rename file
extracted_name = 'chromedriver'
if p == 'win':
extracted_name += '.exe'
filename += '.exe'
print('rename file to: ' + filename)
final_path = os.path.join(DOWNLOAD_DIR, filename)
os.rename(os.path.join(DOWNLOAD_DIR, extracted_name), final_path)
# give execute permission
print('setting permissions')
os.chmod(final_path, 0o755)
# delete zip file
print('removing file: ' + path)
os.remove(path)
return version
def get_version_from_file(path):
try:
with open(path, 'r') as f:
current_version = f.read().strip()
return current_version
except: return None
return None
# get environment version for custom travis builds
env_version = os.environ.get('VERSION')
if not env_version:
current_version = get_version_from_file(VERSION_FILE)
print('current version: ' + str(current_version))
version, _ = check_for_update(current_version)
if not version:
# exit with code 1 to prevent auto deploy
sys.exit(1)
else:
print('got version from environment: ' + env_version)
version = env_version
version = update_version(version)
print('version updated: ' + version)
# update version file
with open(VERSION_FILE, 'w') as f:
f.write(version)
# exit with code 0 to enable auto-deploy
sys.exit(0)