-
Notifications
You must be signed in to change notification settings - Fork 142
/
setup.py
318 lines (263 loc) · 9.89 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
313
314
315
316
317
318
from __future__ import print_function
from distutils import log
import glob
import json
import os
import shutil
from subprocess import check_call
import sys
import tempfile
from setuptools import Command, find_packages, setup
from setuptools.command.build_py import build_py
from setuptools.command.develop import develop
from setuptools.command.egg_info import egg_info
from setuptools.command.install import install
from setuptools.command.sdist import sdist
PY3 = (sys.version_info[0] >= 3)
if PY3:
try:
import mapnik # noqa
except ImportError:
print("""
Python bindings for Mapnik (https://github.com/mapnik/python-mapnik) are
required to run GeoNotebook. Unfortunately there are no pip install-able
versions of the bindings for Python 3. The Mapnik bindings will compile
under Python 3 but they must be compiled and installed from source.
Please follow the instructions on the mapnik/python-mapnik repository,
ensuring you can import mapnik before continuing to install GeoNotebook
using Python 3.
""")
sys.exit(1)
def post_install(func, **kwargs):
def command_wrapper(command_subclass):
# Keep a reference to the command subclasses 'run' function
_run = command_subclass.run
def run(self):
_run(self)
log.info("running post install function {}".format(func.__name__))
func(self, **kwargs)
command_subclass.run = run
return command_subclass
return command_wrapper
def install_kernel(cmd):
# Install the kernel spec when we install the package
from ipykernel import kernelspec
from jupyter_client.kernelspec import KernelSpecManager
kernel_name = 'geonotebook%i' % sys.version_info[0]
path = os.path.join(tempfile.mkdtemp(suffix='_kernels'), kernel_name)
try:
os.makedirs(path)
except OSError:
pass
kernel_dict = {
'argv': kernelspec.make_ipkernel_cmd(mod='geonotebook'),
'display_name': 'Geonotebook (Python %i)' % sys.version_info[0],
'language': 'python',
}
with open(os.path.join(path, 'kernel.json'), 'w') as fh:
json.dump(kernel_dict, fh, indent=1)
ksm = KernelSpecManager()
ksm.install_kernel_spec(
path, kernel_name=kernel_name, user=False, prefix=sys.prefix)
shutil.rmtree(path)
# shamelessly taken from ipyleaflet: https://github.com/ellisonbg/ipyleaflet
# Copyright (c) 2014 Brian E. Granger
here = os.path.dirname(os.path.abspath(__file__))
node_root = os.path.join(here, 'js')
is_repo = os.path.exists(os.path.join(here, '.git'))
npm_path = os.pathsep.join([
os.path.join(node_root, 'node_modules', '.bin'),
os.environ.get('PATH', os.defpath),
])
log.set_verbosity(log.DEBUG)
log.info('setup.py entered')
log.info('$PATH=%s' % os.environ['PATH'])
def js_prerelease(command, strict=False):
"""Decorator for building minified js/css prior to another command."""
class DecoratedCommand(command):
def run(self):
jsdeps = self.distribution.get_command_obj('jsdeps')
if not is_repo and all(os.path.exists(t) for t in jsdeps.targets):
# sdist, nothing to do
command.run(self)
return
try:
self.distribution.run_command('jsdeps')
except Exception as e:
missing = [t for t in jsdeps.targets if not os.path.exists(t)]
if strict or missing:
log.warn('rebuilding js and css failed')
if missing:
log.error('missing files: %s' % missing)
raise e
else:
log.warn('rebuilding js and css failed (not a problem)')
log.warn(str(e))
command.run(self)
update_package_data(self.distribution)
return DecoratedCommand
def update_package_data(distribution):
"""Update package_data to catch changes during setup."""
build_py = distribution.get_command_obj('build_py')
# distribution.package_data = find_package_data()
# re-init build_py options which load package_data
build_py.finalize_options()
class NPM(Command):
description = 'install package.json dependencies using npm'
user_options = []
node_modules = os.path.join(node_root, 'node_modules')
targets = [
os.path.join(here, 'geonotebook', 'static', 'index.js'),
os.path.join(here, 'geonotebook', 'static', 'styles.css')
]
def initialize_options(self):
pass
def finalize_options(self):
pass
def has_npm(self):
try:
check_call(['npm', '--version'])
return True
except:
return False
def should_run_npm_install(self):
return self.has_npm()
def run(self):
has_npm = self.has_npm()
if not has_npm:
log.error(
"`npm` unavailable. If you're running this command using "
"sudo, make sure `npm` is available to sudo"
)
env = os.environ.copy()
env['PATH'] = npm_path
if self.should_run_npm_install():
log.info(
"Installing build dependencies with npm. "
"This may take a while..."
)
check_call(
['npm', 'install'],
cwd=node_root, stdout=sys.stdout, stderr=sys.stderr
)
log.info(
"Building static assets. "
)
check_call(
['npm', 'run', 'build'],
cwd=node_root, stdout=sys.stdout, stderr=sys.stderr
)
os.utime(self.node_modules, None)
for t in self.targets:
if not os.path.exists(t):
msg = 'Missing file: %s' % t
if not has_npm:
msg += '\nnpm is required to build a development version'
raise ValueError(msg)
# update package data in case this created new files
update_package_data(self.distribution)
def install_geonotebook_ini(cmd, link=False):
# cmd.dist is a pkg_resources.Distribution class, See:
# http://setuptools.readthedocs.io/en/latest/pkg_resources.html#distribution-objects
# pkg_resources.Distribution.location can be a lot of things, but in the
# case of a develop install it will be the path to the source tree. This
# will not work if applied to more exotic install targets
# (e.g. pip install -e [email protected]:OpenGeoscience/geonotebook.git)
base_dir = cmd.dist.location
# cmd.distribution is a setuptools.dist.Distribution class, See:
# https://github.com/pypa/setuptools/blob/master/setuptools/dist.py#L215
for sys_path, local_paths in cmd.distribution.data_files:
try:
os.makedirs(os.path.join(sys.prefix, sys_path))
except OSError:
pass
for local_path in local_paths:
for l in glob.glob(local_path):
src = os.path.join(base_dir, l)
dest = os.path.join(sys_path, os.path.basename(src))\
if sys_path.startswith("/") else \
os.path.join(sys.prefix, sys_path, os.path.basename(src))
if os.path.lexists(dest):
os.remove(dest)
if link:
log.info("linking {} to {}".format(src, dest))
os.symlink(src, dest)
else:
log.info("copying {} to {}".format(src, dest))
shutil.copyfile(src, dest)
@post_install(install_kernel)
class CustomInstall(install):
pass
@post_install(install_geonotebook_ini, link=True)
@post_install(install_kernel)
class CustomDevelop(develop):
pass
setup(
name='geonotebook',
version='0.0.0',
description='A Jupyter notebook extension for Geospatial Analysis',
long_description='A Jupyter notebook extension for Geospatial Analysis',
url='https://github.com/OpenDataAnalytics',
author='Kitware Inc',
author_email='[email protected]',
license='Apache License 2.0',
setup_requires=[
"numpy",
"jupyter-client",
"ipykernel",
"notebook"
],
install_requires=[
"futures",
"promise",
"ModestMaps",
"rasterio",
"Shapely",
"requests",
"ipykernel",
"jupyter_client",
"notebook",
"fiona",
"mapnik",
"shapely",
"tornado<5.0"
],
cmdclass={
'install': CustomInstall,
'develop': CustomDevelop,
'build_py': js_prerelease(build_py),
'egg_info': js_prerelease(egg_info),
'sdist': js_prerelease(sdist, strict=True),
'jsdeps': NPM
},
packages=find_packages(exclude=['contrib', 'docs', 'tests']),
data_files=[
('etc', ['config/geonotebook.ini']),
('share/jupyter/nbextensions/geonotebook', [
'geonotebook/static/index.js',
'geonotebook/static/index.js.map',
'geonotebook/static/styles.css',
'geonotebook/static/styles.css.map'
])
],
package_data={'geonotebook': ['templates/*.html']},
entry_points={
'geonotebook.wrappers.raster_schema': [
'file = geonotebook.wrappers.file_reader:FileIOReader'
],
'geonotebook.wrappers.raster.file': [
'geotiff = geonotebook.wrappers.file_reader:RasterIOReader',
'tiff = geonotebook.wrappers.file_reader:RasterIOReader',
'tif = geonotebook.wrappers.file_reader:RasterIOReader',
'nc = geonotebook.wrappers.file_reader:RasterIOReader',
'vrt = geonotebook.wrappers.file_reader:VRTReader',
],
'geonotebook.handlers.default': [
'/log = geonotebook.logging_utils:LoggingRequestHandler'
],
'geonotebook.vis.server': [
"geoserver = geonotebook.vis.geoserver:Geoserver",
"ktile = geonotebook.vis.ktile:Ktile"
],
}
)