forked from NCPP/ocgis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.py
149 lines (126 loc) · 5.16 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
from distutils.core import setup, Command
import sys
from subprocess import check_call
import os
import tempfile
VERSION = '0.08b'
class UninstallCommand(Command):
description = "information on how to uninstall OCGIS"
user_options = []
def initialize_options(self): pass
def finalize_options(self): pass
def run(self):
try:
import ocgis
print('To uninstall, manually remove the Python package folder located here: {0}'.format(os.path.split(ocgis.__file__)[0]))
except ImportError:
raise(ImportError("Either OpenClimateGIS is not installed or not available on the Python path."))
class InstallDependenciesUbuntu(Command):
description = "install on Ubuntu systems"
user_options = []
def run(self):
cwd = os.getcwd()
out = 'install_out.log'
err = 'install_err.log'
odir = tempfile.mkdtemp()
stdout = open(out,'w')
stderr = open(err,'w')
def call(args):
check_call(args,stdout=stdout,stderr=stderr)
def install_dependency(odir,url,tarball,edir,config_flags=None,custom_make=None):
path = tempfile.mkdtemp(dir=odir)
os.chdir(path)
print('downloading {0}...'.format(edir))
call(['wget',url])
print('extracting {0}...'.format(edir))
call(['tar','-xzvf',tarball])
os.chdir(edir)
if custom_make is None:
print('configuring {0}...'.format(edir))
call(['./configure']+config_flags)
print('building {0}...'.format(edir))
call(['make'])
print('installing {0}...'.format(edir))
call(['make','install'])
else:
print('installing {0}...'.format(edir))
custom_make()
print('installing apt packages...')
call(['apt-get','update'])
call(['apt-get','-y','install','g++','libz-dev','curl','wget','python-dev','python-setuptools','python-gdal'])
print('installing shapely...')
call(['easy_install','shapely'])
call(['easy_install','fiona'])
prefix = '/usr/local'
hdf5 = 'hdf5-1.8.10-patch1'
hdf5_tarball = '{0}.tar.gz'.format(hdf5)
hdf5_url = 'http://www.hdfgroup.org/ftp/HDF5/current/src/{0}'.format(hdf5_tarball)
hdf5_flags = ['--prefix={0}'.format(prefix),'--enable-shared','--enable-hl']
install_dependency(odir,hdf5_url,hdf5_tarball,hdf5,hdf5_flags)
nc4 = 'netcdf-4.2.1'
nc4_tarball = '{0}.tar.gz'.format(nc4)
nc4_url = 'ftp://ftp.unidata.ucar.edu/pub/netcdf/{0}'.format(nc4_tarball)
nc4_flags = ['--prefix={0}'.format(prefix),'--enable-shared','--enable-dap','--enable-netcdf-4']
os.putenv('LDFLAGS','-L{0}/lib'.format(prefix))
os.putenv('CPPFLAGS','-I{0}/include'.format(prefix))
install_dependency(odir,nc4_url,nc4_tarball,nc4,nc4_flags)
os.unsetenv('LDFLAGS')
os.unsetenv('CPPFLAGS')
nc4p = 'netCDF4-1.0.4'
nc4p_tarball = '{0}.tar.gz'.format(nc4p)
nc4p_url = 'http://netcdf4-python.googlecode.com/files/{0}'.format(nc4p_tarball)
call(['ldconfig'])
def nc4p_make():
call(['python','setup.py','install'])
install_dependency(odir,nc4p_url,nc4p_tarball,nc4p,custom_make=nc4p_make)
stdout.close()
stderr.close()
#shutil.rmtree(odir)
os.chdir(cwd)
print('dependencies installed.')
## check python version
python_version = float(sys.version_info[0]) + float(sys.version_info[1])/10
if python_version != 2.7:
raise(ImportError(
'This software requires Python version 2.7.x. You have {0}.x'.format(python_version)))
## attempt package imports
pkgs = ['numpy','netCDF4','osgeo','shapely','fiona']
for pkg in pkgs:
try:
__import__(pkg)
except ImportError:
msg = 'Unable to import required Python package: "{0}".'.format(pkg)
raise(ImportError(msg))
## get package structure
def _get_dot_(path,root='src'):
ret = []
path_parse = path
while True:
path_parse,tail = os.path.split(path_parse)
if tail == root:
break
else:
ret.append(tail)
ret.reverse()
return('.'.join(ret))
package_dir = {'':'src'}
src_path = os.path.join(package_dir.keys()[0],package_dir.values()[0],'ocgis')
packages = []
for dirpath,dirnames,filenames in os.walk(src_path):
if '__init__.py' in filenames:
package = _get_dot_(dirpath)
packages.append(package)
## run the installation
setup(name='ocgis',
version=VERSION,
author='NESII/CIRES/NOAA-ESRL',
author_email='[email protected]',
url='http://ncpp.github.io/ocgis/install.html#installing-openclimategis',
license='NCSA License',
platforms=['all'],
packages=packages,
package_dir=package_dir,
cmdclass={'uninstall':UninstallCommand,
'install_dependencies_ubuntu':InstallDependenciesUbuntu}, requires=['numpy', 'netCDF4', 'fiona',
'shapely']
)