/
setup.py
209 lines (167 loc) · 7.06 KB
/
setup.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
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
from distutils.command.install import install
from distutils.command.build_scripts import build_scripts
from distutils.core import setup
from distutils.version import StrictVersion
import os
import platform
import re
import tempfile
import sys
import zipfile
import tarfile
try:
from urllib import request
except ImportError:
import urllib as request
def read(filename):
path = os.path.join(os.path.dirname(__file__), filename)
with open(path, 'r') as f:
return f.read()
GECKODRIVER_INFO_URL = (
'https://api.github.com/repos/mozilla/geckodriver/releases'
)
GECKODRIVER_URL_TEMPLATE = (
'https://github.com/mozilla/geckodriver/releases/download/v{version}/'
'geckodriver-v{version}-{os_}{architecture}.{compression}'
)
GECKODRIVER_RELEASES_VERSION_PATTERN = re.compile(r'\"tag_name\":\s?\"v(\d+\.\d+\.\d+)\"')
GECKODRIVER_VERSION_PATTERN = re.compile(r'(\d+\.\d+\.\d+)')
OLDEST_SUPPORTED_VERSION = '0.11.1'
# Global variables
geckodriver_version = None
def get_geckodriver_version():
"""Retrieves the most recent geckodriver version."""
global geckodriver_version
response = request.urlopen(GECKODRIVER_INFO_URL)
content = response.read()
match = GECKODRIVER_RELEASES_VERSION_PATTERN.search(str(content))
if match:
return match.group(1)
else:
raise Exception('Unable to get latest geckodriver version from {0}'
.format(GECKODRIVER_INFO_URL))
class BuildScripts(build_scripts):
"""Downloads and unzips the requested geckodriver executable."""
def _download(self, zip_path):
compression = 'tar.gz'
architecture = platform.architecture()[0][:-3]
plat = platform.platform().lower()
if plat.startswith('darwin'):
os_ = 'macos'
# Only 64 bit architecture is available for mac since version 2.23
architecture = ''
elif plat.startswith('linux'):
os_ = 'linux'
elif plat.startswith('win'):
os_ = 'win'
compression = 'zip'
else:
raise Exception('Unsupported platform: {0}'.format(plat))
url = GECKODRIVER_URL_TEMPLATE.format(version=geckodriver_version,
os_=os_,
architecture=architecture,
compression=compression)
download_report_template = ("\t - downloading from '{0}' to '{1}'"
.format(url, zip_path))
def reporthoook(x, y, z):
global download_ok
percent_downloaded = '{0:.0%}'.format((x * y) / float(z))
sys.stdout.write('\r')
sys.stdout.write("{0} [{1}]".format(download_report_template,
percent_downloaded))
download_ok = percent_downloaded == '100%'
if download_ok:
sys.stdout.write(' OK')
sys.stdout.flush()
request.urlretrieve(url, zip_path, reporthoook)
print('')
if not download_ok:
print('\t - download failed!')
def _unzip(self, zip_path):
if zip_path.endswith('zip'):
zf = zipfile.ZipFile(zip_path)
else:
zf = tarfile.open(zip_path)
print("\t - extracting '{0}' to '{1}'."
.format(zip_path, self.build_dir))
zf.extractall(self.build_dir)
def _get_compression_suffix(self):
result = 'tar.gz'
if platform.platform().lower().startswith('win'):
result = 'zip'
return result
def run(self):
global geckodriver_version, geckodriver_checksums
if not geckodriver_version:
geckodriver_version = get_geckodriver_version()
file_name = 'geckodriver_{version}.{compression}'.format(version=geckodriver_version, compression=self._get_compression_suffix())
zip_path = os.path.join(tempfile.gettempdir(), file_name)
self._download(zip_path)
self._unzip(zip_path)
self.scripts = [os.path.join(self.build_dir, script) for script in
os.listdir(self.build_dir)]
build_scripts.run(self)
def finalize_options(self):
build_scripts.finalize_options(self)
self.scripts = []
class Install(install):
"""Used to get geckodriver version and checksums from install options"""
# Fix an error when pip calls setup.py with the
# --single-version-externally-managed and it is not supported due to
# old setuptools version.
_svem = list(filter(lambda x: x[0] == 'single-version-externally-managed',
install.user_options))
if not _svem:
single_version_externally_managed = None
install.user_options.append(('single-version-externally-managed',
None, ""))
user_options = install.user_options + [
('geckodriver-version=', None, 'Geckodriver version'),
('geckodriver-checksums=', None, 'Geckodriver checksums'),
]
def initialize_options(self):
self.geckodriver_version = None
install.initialize_options(self)
def run(self):
global geckodriver_version
if self.geckodriver_version:
if not GECKODRIVER_VERSION_PATTERN.match(self.geckodriver_version):
raise Exception('Invalid --geckodriver-version={0}! '
'Must match /{1}/'
.format(self.geckodriver_version,
GECKODRIVER_VERSION_PATTERN.pattern))
if not StrictVersion(self.geckodriver_version) >= StrictVersion(OLDEST_SUPPORTED_VERSION):
raise Exception('Invalid --geckodriver-version={0}! '
'Minimum supported version is {1}'
.format(self.geckodriver_version,
OLDEST_SUPPORTED_VERSION))
geckodriver_version = self.geckodriver_version
install.run(self)
setup(
name='geckodriver_installer',
version='0.0.2',
author='Tyler Austen',
author_email='tyler@springahead.com',
description='Geckodriver Installer',
long_description=read('README.rst'),
keywords='geckodriver installer',
url='https://github.com/tausten/geckodriver_installer',
classifiers=[
'Development Status :: 3 - Alpha',
'Environment :: Web Environment',
'Intended Audience :: Developers',
'License :: OSI Approved :: MIT License',
'Operating System :: OS Independent',
'Programming Language :: Python',
'Topic :: Internet :: WWW/HTTP',
'Topic :: Software Development :: Libraries :: Python Modules',
'Topic :: Software Development :: Libraries :: Python Modules',
'Topic :: System :: Installation/Setup',
],
license='MIT',
package_data={'': ['*.txt', '*.rst']},
# If packages is empty, contents of ./build/lib will not be copied!
packages=['geckodriver_installer'],
scripts=['blank.py'],
cmdclass=dict(build_scripts=BuildScripts, install=Install)
)