-
Notifications
You must be signed in to change notification settings - Fork 8
/
setup.py
312 lines (269 loc) · 11.8 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#!/usr/bin/env python
# coding: utf-8
from __future__ import print_function
from setuptools import setup, find_packages
from setuptools.command.build_py import build_py
from setuptools.command.sdist import sdist
import glob
import hashlib
import re
import sys
import os
import tarfile
import shutil
import platform
try:
from io import BytesIO
from urllib.request import urlopen, URLError
except ImportError:
from StringIO import StringIO as BytesIO
from urllib2 import urlopen, URLError
import vcversioner
sys.path.insert(0, os.path.abspath('gr'))
import runtime_helper
sys.path.pop(0)
_runtime_version = os.environ.get('GR_VERSION', runtime_helper.required_runtime_version())
if _runtime_version == 'master':
# allow 'master' as alias for 'latest'
_runtime_version = 'latest'
__author__ = "Florian Rhiem <f.rhiem@fz-juelich.de>, Christian Felder <c.felder@fz-juelich.de>"
__version__ = vcversioner.find_version(version_module_paths=[os.path.join("gr", "_version.py")]).version
__copyright__ = """Copyright (c) 2012-2017: Josef Heinen, Florian Rhiem,
Christian Felder and other contributors:
http://gr-framework.org/credits.html
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
The GR framework can be built with plugins that use code from the
following projects, which have their own licenses:
- MuPDF - a lightweight PDF and XPS viewer (AFPL)
- Ghostscript - an interpreter for the PostScript language and for PDF (AFPL)
- FFmpeg - a multimedia framework (LGPL / GPLv2)
"""
_long_description = None
try:
with open("README.rst", 'r') as fd:
_long_description = fd.read()
except IOError as e:
print("WARNING: long_description could not be read from file. Error message was:\n",
e, file=sys.stderr)
def find_packages_py2_or_py3(*args, **kwargs):
kwds = dict(kwargs)
if sys.version_info[0] == 2:
kwds.setdefault('exclude', []).extend(['grm'])
return find_packages(*args, **kwds)
try:
from wheel.bdist_wheel import bdist_wheel as _bdist_wheel
class bdist_wheel(_bdist_wheel):
def finalize_options(self):
_bdist_wheel.finalize_options(self)
self.root_is_pure = False
except ImportError:
bdist_wheel = None
class DownloadHashes(sdist):
def run(self):
"""
Download hashes for binary distributions during sdist creation
This step prepares the actual downloading of the binary distribution
during the build_py step by making sure the hashes of the individual
tar files will be available.
"""
DownloadBinaryDistribution.get_expected_hashes(_runtime_version)
sdist.run(self)
class DownloadBinaryDistribution(build_py):
SUPPORTED_LINUX_DISTRIBUTIONS = (
'ArchLinux',
'CentOS',
'Debian',
'Ubuntu',
)
@staticmethod
def get_file_from_mirrors(file_name, version, schema):
mirrors = [
'{schema}://gr-framework.org/downloads/'.format(schema=schema)
]
if version != 'latest':
# GitHub only hosts release builds
# GitHub should be preferred
# GitHub enforces HTTPS
mirrors.insert(0, 'https://github.com/sciapp/gr/releases/download/v{version}/'.format(version=version))
urls = []
for mirror in mirrors:
urls.append(mirror + file_name)
for url in urls:
try:
response = urlopen(url)
except Exception:
continue
if response.getcode() == 200:
return response.read()
raise URLError('Failed to download file from: ' + ', '.join(urls))
@staticmethod
def detect_os():
if sys.platform == 'darwin':
return 'Darwin'
if sys.platform == 'win32':
return 'Windows'
if sys.platform.startswith('linux'):
release_file_names = glob.glob('/etc/*-release')
release_file_names = [release_file_name for release_file_name in release_file_names if
os.path.isfile(release_file_name) and os.access(release_file_name, os.R_OK)]
release_info = '\n'.join([open(release_file_name).read() for release_file_name in release_file_names])
if '/etc/os-release' in release_file_names:
if 'ID=ubuntu' in release_info or 'ID=linuxmint' in release_info:
return 'Ubuntu'
if 'ID=debian' in release_info or 'ID=raspbian' in release_info:
return 'Debian'
if 'ID=arch' in release_info:
return 'ArchLinux'
if 'ID="opensuse-tumbleweed"' in release_info:
return 'CentOS'
if '/etc/redhat-release' in release_file_names:
if 'release 7' in release_info:
return 'CentOS'
return 'Linux'
if sys.platform.startswith('freebsd'):
if os.access('/etc/os-release', os.R_OK):
with open('/etc/os-release', 'r') as f:
release_info = {key: value for key, value in (re.split(r"=[\"']?", line)[:2] for line in f)}
if 'VERSION' in release_info:
major_version_str = release_info['VERSION'].split('.')[0]
if major_version_str.isdigit():
major_version = int(major_version_str)
if major_version >= 13:
return 'FreeBSD'
return None
@staticmethod
def detect_architecture():
if 'armv7' in platform.machine():
return 'armhf'
if 'aarch64' in platform.machine() or 'arm64' in platform.machine():
return 'aarch64'
is_64bits = sys.maxsize > 2**32
if is_64bits:
return 'x86_64'
return 'i686'
@staticmethod
def get_expected_hashes(version):
hash_file_name = 'gr-{version}.sha512.txt'.format(version=version)
local_hash_file_name = os.path.join(os.path.dirname(__file__), hash_file_name)
try:
with open(local_hash_file_name, 'r') as hash_file:
hash_file_content = hash_file.read()
except IOError:
hash_file_content = DownloadBinaryDistribution.get_file_from_mirrors(hash_file_name, version, 'https').decode('utf-8')
# store hashes for later use
with open(local_hash_file_name, 'w') as hash_file:
hash_file.write(hash_file_content)
expected_hashes = {}
for line in hash_file_content.split('\n'):
if ' *' in line:
expected_hash, hashed_file_name = line.split(' *')
expected_hashes[hashed_file_name] = expected_hash
return expected_hashes
@staticmethod
def get_expected_hash(version, file_name):
expected_hashes = DownloadBinaryDistribution.get_expected_hashes(version)
if file_name not in expected_hashes:
raise RuntimeError('No hash known for file: ' + file_name)
return expected_hashes[file_name]
def finalize_options(self):
build_py.finalize_options(self)
self.root_is_pure = False
def run(self):
"""
Downloads, unzips and installs GKS, GR and GR3 binaries.
"""
build_py.run(self)
base_path = os.path.realpath(self.build_lib)
if os.environ.get('GR_FORCE_DOWNLOAD') or runtime_helper.load_runtime(silent=True) is None:
version = _runtime_version
operating_system = DownloadBinaryDistribution.detect_os()
if operating_system is not None:
arch = DownloadBinaryDistribution.detect_architecture()
if arch == 'i686' and (
operating_system == 'Linux' or operating_system in self.SUPPORTED_LINUX_DISTRIBUTIONS
):
operating_system, arch = 'Linux', 'i386'
# check for desired build variant (e.g. "mingw" or "msvc")
variant = os.environ.get('GR_BUILD_VARIANT', '')
if operating_system == 'Windows' and variant == '':
variant = 'msvc'
suffix = ('-' + variant) if variant and variant != 'mingw' else ''
# download binary distribution for system
file_name = 'gr-{version}-{os}-{arch}{suffix}.tar.gz'.format(
version=version,
os=operating_system,
arch=arch,
suffix=suffix
)
tar_gz_data = DownloadBinaryDistribution.get_file_from_mirrors(file_name, version, 'http')
# wrap response as file-like object
tar_gz_data = BytesIO(tar_gz_data)
expected_hash = DownloadBinaryDistribution.get_expected_hash(version, file_name)
calculated_hash = hashlib.sha512(tar_gz_data.read()).hexdigest()
tar_gz_data.seek(0)
if calculated_hash != expected_hash:
raise RuntimeError("Downloaded binary distribution of GR runtime does not match expected hash")
# extract shared libraries from downloaded .tar.gz archive
tar_gz_file = tarfile.open(fileobj=tar_gz_data)
try:
for member in tar_gz_file.getmembers():
tar_gz_file.extract(member, base_path)
finally:
tar_gz_file.close()
if sys.platform == 'win32':
search_dir = os.path.join(base_path, 'gr', 'bin')
else:
search_dir = os.path.join(base_path, 'gr', 'lib')
if runtime_helper.load_runtime(search_dirs=[search_dir], silent=False) is None:
raise RuntimeError("Unable to install GR runtime")
setup(
name="gr",
version=__version__,
description="Python visualization framework",
author="Scientific IT Systems",
author_email="j.heinen@fz-juelich.de",
maintainer="Josef Heinen",
license="MIT License",
keywords="gr",
url="http://gr-framework.org",
platforms=["Linux", "OS X", "Windows"],
install_requires=[
'numpy >= 1.6',
],
packages=find_packages_py2_or_py3(exclude=["tests", "tests.*"]),
include_package_data=True,
long_description=_long_description,
classifiers=[
'Framework :: IPython',
'Intended Audience :: Science/Research',
'License :: OSI Approved :: MIT License',
'Operating System :: MacOS :: MacOS X',
'Operating System :: Microsoft :: Windows',
'Operating System :: POSIX :: Linux',
'Programming Language :: Python',
'Programming Language :: Python :: 2',
'Programming Language :: Python :: 3',
'Topic :: Multimedia :: Graphics',
'Topic :: Scientific/Engineering :: Visualization',
],
cmdclass={
'sdist': DownloadHashes,
'build_py': DownloadBinaryDistribution,
'bdist_wheel': bdist_wheel,
}
)