forked from Toblerity/Fiona
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.py
274 lines (235 loc) · 9.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
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
from distutils.command.sdist import sdist
import logging
import os
import shutil
import subprocess
import sys
from setuptools import setup
from setuptools.extension import Extension
# Use Cython if available.
try:
from Cython.Build import cythonize
except ImportError:
cythonize = None
logging.basicConfig()
log = logging.getLogger()
# python -W all setup.py ...
if 'all' in sys.warnoptions:
log.level = logging.DEBUG
def check_output(cmd):
# since subprocess.check_output doesn't exist in 2.6
# we wrap it here.
try:
out = subprocess.check_output(cmd)
return out.decode('utf')
except AttributeError:
# For some reasone check_output doesn't exist
# So fall back on Popen
p = subprocess.Popen(cmd, stdout=subprocess.PIPE)
out, err = p.communicate()
return out
def copy_data_tree(datadir, destdir):
try:
shutil.rmtree(destdir)
except OSError:
pass
shutil.copytree(datadir, destdir)
# Parse the version from the fiona module.
with open('fiona/__init__.py', 'r') as f:
for line in f:
if line.find("__version__") >= 0:
version = line.split("=")[1].strip()
version = version.strip('"')
version = version.strip("'")
break
# Fiona's auxiliary files are UTF-8 encoded and we'll specify this when
# reading with Python 3+
open_kwds = {}
if sys.version_info > (3,):
open_kwds['encoding'] = 'utf-8'
with open('VERSION.txt', 'w', **open_kwds) as f:
f.write(version)
with open('README.rst', **open_kwds) as f:
readme = f.read()
with open('CREDITS.txt', **open_kwds) as f:
credits = f.read()
with open('CHANGES.txt', **open_kwds) as f:
changes = f.read()
# Set a flag for builds where the source directory is a repo checkout.
source_is_repo = os.path.exists("MANIFEST.in")
def copy_gdalapi_pyx(gdalversion):
if gdalversion[0] == u'1':
log.info("Building Fiona for gdal 1.x: {0}".format(gdalversion))
shutil.copy('fiona/ogrext1.pyx', 'fiona/ogrext.pyx')
else:
log.info("Building Fiona for gdal 2.x: {0}".format(gdalversion))
shutil.copy('fiona/ogrext2.pyx', 'fiona/ogrext.pyx')
def copy_gdalapi_c(gdalversion):
if gdalversion[0] == u'1':
log.info("Building Fiona for gdal 1.x: {0}".format(gdalversion))
shutil.copy('fiona/ogrext1.c', 'fiona/ogrext.c')
print("Copied 'fiona/ogrext1.c' to'fiona/ogrext.c'")
else:
log.info("Building Fiona for gdal 2.x: {0}".format(gdalversion))
shutil.copy('fiona/ogrext2.c', 'fiona/ogrext.c')
print("Copied 'fiona/ogrext2.c' to 'fiona/ogrext.c'")
if '--gdalversion' in sys.argv and 'clean' not in sys.argv:
index = sys.argv.index('--gdalversion')
sys.argv.pop(index)
gdalversion = sys.argv.pop(index)
# Extend distutil's sdist command to generate C extension sources from
# both `ogrext`.pyx` and `ogrext2.pyx` for GDAL 1.x and 2.x.
class sdist_multi_gdal(sdist):
def run(self):
shutil.copy('fiona/ogrext1.pyx', 'fiona/ogrext.pyx')
_ = check_output(['cython', '-v', '-f', 'fiona/ogrext.pyx',
'-o', 'fiona/ogrext1.c'])
print(_)
shutil.copy('fiona/ogrext2.pyx', 'fiona/ogrext.pyx')
_ = check_output(['cython', '-v', '-f', 'fiona/ogrext.pyx',
'-o', 'fiona/ogrext2.c'])
print(_)
sdist.run(self)
# By default we'll try to get options via gdal-config. On systems without,
# options will need to be set in setup.cfg or on the setup command line.
include_dirs = []
library_dirs = []
libraries = []
extra_link_args = []
gdal_output = [None] * 4
if 'clean' not in sys.argv:
try:
gdal_config = os.environ.get('GDAL_CONFIG', 'gdal-config')
for i, flag in enumerate(
["--cflags", "--libs", "--datadir", "--version"]):
gdal_output[i] = check_output([gdal_config, flag]).strip()
for item in gdal_output[0].split():
if item.startswith("-I"):
include_dirs.extend(item[2:].split(":"))
for item in gdal_output[1].split():
if item.startswith("-L"):
library_dirs.extend(item[2:].split(":"))
elif item.startswith("-l"):
libraries.append(item[2:])
else:
# e.g. -framework GDAL
extra_link_args.append(item)
gdalversion = gdal_output[3]
except Exception as e:
if os.name == "nt":
log.info("Building on Windows requires extra options to setup.py "
"to locate needed GDAL files.\nMore information is "
"available in the README.")
else:
log.warning("Failed to get options via gdal-config: %s", str(e))
if os.environ.get('PACKAGE_DATA'):
destdir = 'fiona/gdal_data'
if gdal_output[2]:
log.info("Copying gdal data from %s" % gdal_output[2])
copy_data_tree(gdal_output[2], destdir)
else:
# check to see if GDAL_DATA is defined
gdal_data = os.environ.get('GDAL_DATA', None)
if gdal_data:
log.info("Copying gdal data from %s" % gdal_data)
copy_data_tree(gdal_data, destdir)
# Conditionally copy PROJ.4 data.
projdatadir = os.environ.get('PROJ_LIB', '/usr/local/share/proj')
if os.path.exists(projdatadir):
log.info("Copying proj data from %s" % projdatadir)
copy_data_tree(projdatadir, 'fiona/proj_data')
ext_options = dict(
include_dirs=include_dirs,
library_dirs=library_dirs,
libraries=libraries,
extra_link_args=extra_link_args)
# When building from a repo, Cython is required.
ext_modules = []
if source_is_repo and "clean" not in sys.argv:
log.info("MANIFEST.in found, presume a repo, cythonizing...")
if not cythonize:
log.critical(
"Cython.Build.cythonize not found. "
"Cython is required to build from a repo.")
sys.exit(1)
copy_gdalapi_pyx(gdalversion)
ext_modules = cythonize([
Extension('fiona._geometry', ['fiona/_geometry.pyx'], **ext_options),
Extension('fiona._transform', ['fiona/_transform.pyx'], **ext_options),
Extension('fiona._crs', ['fiona/_crs.pyx'], **ext_options),
Extension('fiona._drivers', ['fiona/_drivers.pyx'], **ext_options),
Extension('fiona._err', ['fiona/_err.pyx'], **ext_options),
Extension('fiona.ogrext', ['fiona/ogrext.pyx'], **ext_options)])
# If there's no manifest template, as in an sdist, we just specify .c files.
elif "clean" not in sys.argv:
copy_gdalapi_c(gdalversion)
ext_modules = [
Extension('fiona._transform', ['fiona/_transform.cpp'], **ext_options),
Extension('fiona._geometry', ['fiona/_geometry.c'], **ext_options),
Extension('fiona._crs', ['fiona/_crs.c'], **ext_options),
Extension('fiona._drivers', ['fiona/_drivers.c'], **ext_options),
Extension('fiona._err', ['fiona/_err.c'], **ext_options),
Extension('fiona.ogrext', ['fiona/ogrext.c'], **ext_options)]
requirements = [
'cligj',
'click-plugins',
'six',
'munch'
]
if sys.version_info < (2, 7):
requirements.append('argparse')
requirements.append('ordereddict')
setup_args = dict(
cmdclass={'sdist': sdist_multi_gdal},
metadata_version='1.2',
name='Fiona',
version=version,
requires_python='>=2.6',
requires_external='GDAL (>=1.8)',
description="Fiona reads and writes spatial data files",
license='BSD',
keywords='gis vector feature data',
author='Sean Gillies',
author_email='sean.gillies@gmail.com',
maintainer='Sean Gillies',
maintainer_email='sean.gillies@gmail.com',
url='http://github.com/Toblerity/Fiona',
long_description=readme + "\n" + changes + "\n" + credits,
package_dir={'': '.'},
packages=['fiona', 'fiona.fio'],
entry_points='''
[console_scripts]
fio=fiona.fio.main:main_group
[fiona.fio_commands]
bounds=fiona.fio.bounds:bounds
calc=fiona.fio.calc:calc
cat=fiona.fio.cat:cat
collect=fiona.fio.collect:collect
distrib=fiona.fio.distrib:distrib
dump=fiona.fio.dump:dump
env=fiona.fio.env:env
filter=fiona.fio.filter:filter
info=fiona.fio.info:info
insp=fiona.fio.insp:insp
load=fiona.fio.load:load
ls=fiona.fio.ls:ls
''',
install_requires=requirements,
extras_require={
'calc': ['shapely'],
'test': ['nose']},
tests_require=['nose'],
test_suite='nose.collector',
ext_modules=ext_modules,
classifiers=[
'Development Status :: 5 - Production/Stable',
'Intended Audience :: Developers',
'Intended Audience :: Science/Research',
'License :: OSI Approved :: BSD License',
'Operating System :: OS Independent',
'Programming Language :: Python :: 2',
'Programming Language :: Python :: 3',
'Topic :: Scientific/Engineering :: GIS'])
if os.environ.get('PACKAGE_DATA'):
setup_args['package_data'] = {'fiona': ['gdal_data/*', 'proj_data/*']}
setup(**setup_args)