forked from SciTools/iris
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.py
230 lines (184 loc) · 6.85 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
from __future__ import print_function
import contextlib
from distutils.command import build_ext, build_py
from distutils.core import setup, Command
from distutils.sysconfig import get_config_var
from distutils.util import convert_path
import fnmatch
import multiprocessing
import os
import sys
import numpy as np
import setuptools
# Add full path so Python doesn't load any __init__.py in the intervening
# directories, thereby saving setup.py from additional dependencies.
sys.path.append('lib/iris/tests/runner')
from _runner import TestRunner
# Returns the package and all its sub-packages
def find_package_tree(root_path, root_package):
root_path = root_path.replace('/', os.path.sep)
packages = [root_package]
root_count = len(root_path.split(os.path.sep))
for (dir_path, dir_names, file_names) in os.walk(convert_path(root_path)):
# Prune dir_names *in-place* to prevent unwanted directory recursion
for dir_name in list(dir_names):
contains_init_file = os.path.isfile(os.path.join(dir_path,
dir_name,
'__init__.py'))
if not contains_init_file:
dir_names.remove(dir_name)
# Exclude compiled PyKE rules, but keep associated unit tests.
if dir_name == 'compiled_krb' and 'tests' not in dir_path:
dir_names.remove(dir_name)
if dir_names:
prefix = dir_path.split(os.path.sep)[root_count:]
packages.extend(['.'.join([root_package] + prefix + [dir_name])
for dir_name in dir_names])
return packages
def file_walk_relative(top, remove=''):
"""
Returns a generator of files from the top of the tree, removing
the given prefix from the root/file result.
"""
top = top.replace('/', os.path.sep)
remove = remove.replace('/', os.path.sep)
for root, dirs, files in os.walk(top):
for file in files:
yield os.path.join(root, file).replace(remove, '')
def std_name_cmd(target_dir):
script_path = os.path.join('tools', 'generate_std_names.py')
xml_path = os.path.join('etc', 'cf-standard-name-table.xml')
module_path = os.path.join(target_dir, 'iris', 'std_names.py')
cmd = (sys.executable, script_path, xml_path, module_path)
return cmd
class SetupTestRunner(TestRunner, setuptools.Command):
pass
class CleanSource(Command):
"""
Removes orphaned pyc/pyo files from the sources.
"""
description = 'clean orphaned pyc/pyo files from sources'
user_options = []
def initialize_options(self):
pass
def finalize_options(self):
pass
def run(self):
for root_path, dir_names, file_names in os.walk('lib'):
for file_name in file_names:
if file_name.endswith('pyc') or file_name.endswith('pyo'):
compiled_path = os.path.join(root_path, file_name)
source_path = compiled_path[:-1]
if not os.path.exists(source_path):
print('Cleaning', compiled_path)
os.remove(compiled_path)
class MakeStdNames(Command):
"""
Generates the CF standard name module containing mappings from
CF standard name to associated metadata.
"""
description = "generate CF standard name module"
user_options = []
def initialize_options(self):
pass
def finalize_options(self):
pass
def run(self):
cmd = std_name_cmd('lib')
self.spawn(cmd)
class MakePykeRules(Command):
"""
Compile the PyKE CF-NetCDF loader rule base.
"""
description = "compile CF-NetCDF loader rule base"
user_options = []
def initialize_options(self):
pass
def finalize_options(self):
pass
@staticmethod
def _pyke_rule_compile():
"""Compile the PyKE rule base."""
from pyke import knowledge_engine
import iris.fileformats._pyke_rules
knowledge_engine.engine(iris.fileformats._pyke_rules)
def run(self):
# Compile the PyKE rules.
MakePykeRules._pyke_rule_compile()
class MissingHeaderError(Exception):
"""
Raised when one or more files do not have the required copyright
and licence header.
"""
pass
class BuildPyWithExtras(build_py.build_py):
"""
Adds the creation of the CF standard names module and compilation
of the PyKE rules to the standard "build_py" command.
"""
@contextlib.contextmanager
def temporary_path(self):
"""
Context manager that adds and subsequently removes the build
directory to the beginning of the module search path.
"""
sys.path.insert(0, self.build_lib)
try:
yield
finally:
del sys.path[0]
def run(self):
# Run the main build command first to make sure all the target
# directories are in place.
build_py.build_py.run(self)
# Now build the std_names module.
cmd = std_name_cmd(self.build_lib)
self.spawn(cmd)
# Compile the PyKE rules.
with self.temporary_path():
MakePykeRules._pyke_rule_compile()
def extract_version():
version = None
fdir = os.path.dirname(__file__)
fnme = os.path.join(fdir, 'lib', 'iris', '__init__.py')
with open(fnme) as fd:
for line in fd:
if (line.startswith('__version__')):
_, version = line.split('=')
version = version.strip()[1:-1] # Remove quotation characters
break
return version
setup(
name='Iris',
version=extract_version(),
url='http://scitools.org.uk/iris/',
author='UK Met Office',
packages=find_package_tree('lib/iris', 'iris'),
package_dir={'': 'lib'},
package_data={
'iris': list(file_walk_relative('lib/iris/etc', remove='lib/iris/')) + \
list(file_walk_relative('lib/iris/tests/results',
remove='lib/iris/')) + \
['fileformats/_pyke_rules/*.k?b'] + \
['tests/stock*.npz']
},
data_files=[('iris', ['CHANGES', 'COPYING', 'COPYING.LESSER'])],
tests_require=['nose'],
features={
'unpack': setuptools.Feature(
"use of UKMO unpack library",
standard=False,
ext_modules=[
setuptools.Extension(
'iris.fileformats._old_pp_packing',
['src/iris/fileformats/pp_packing/pp_packing.c'],
libraries=['mo_unpack'],
include_dirs=[np.get_include()]
)
]
)
},
cmdclass={'test': SetupTestRunner, 'build_py': BuildPyWithExtras,
'std_names': MakeStdNames, 'pyke_rules': MakePykeRules,
'clean_source': CleanSource},
)