-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsetup.py
292 lines (230 loc) · 9.11 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
# -*- coding: utf-8 -*-
# Copyright (c) 2018 Christiaan Frans Rademan.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# * Neither the name of the copyright holders nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
# THE POSSIBILITY OF SUCH DAMAGE.
import os
import sys
import imp
import glob
import shutil
from distutils import cmd
if not sys.version_info >= (3, 5):
print('Requires python version 3.5 or higher')
exit()
try:
from setuptools import setup, Extension, find_packages
from setuptools.command.test import test as TestCommand
except ImportError:
print('Requires `setuptools` to be installed')
print('`pip install setuptools`')
exit()
# DEFINE ROOT PACKAGE NAME
PACKAGE = 'tutorials'
###############################################################################
# DO NOT EDIT CODE BELOW THIS POINT ###########################################
###############################################################################
cmdclass = {}
MYDIR = os.path.abspath(os.path.dirname(__file__))
CODE_DIRECTORY = os.path.join(MYDIR, PACKAGE)
DOCS_DIRECTORY = os.path.join(MYDIR, 'docs')
TESTS_DIRECTORY = os.path.join(MYDIR, 'tests')
PYTEST_FLAGS = ['--doctest-modules']
# Add the source directory to the module search path.
sys.path.insert(0, MYDIR)
# Load Metadata from PACKAGE
metadata = imp.load_source(
'metadata', os.path.join(MYDIR, CODE_DIRECTORY, 'metadata.py'))
# Miscellaneous helper functions
def requirements(path):
dependency = []
if os.path.exists(os.path.join(os.path.dirname(__file__), path)):
with open(os.path.join(os.path.dirname(__file__), path)) as req:
dependency = req.read().splitlines()
return dependency
def list_modules(path, ext='py'):
filenames = glob.glob(os.path.join(path, '*.%s' % ext))
module_names = []
for name in filenames:
module, ext = os.path.splitext(os.path.basename(name))
if module != '__init__':
module_names.append(module)
return module_names
def list_packages(package):
path = os.path.join(MYDIR, package)
packages = []
scan_dir = os.path.join(path)
top_dir = "/" + "/".join(scan_dir.strip('/').split('/')[:-1])
for directory, directories, files in os.walk(scan_dir):
if '__init__.py' in files:
packages.append((os.path.relpath(directory,
top_dir).replace('/', '.')))
return packages
def read(filename):
"""Return the contents of a file.
:param filename: file path
:type filename: :class:`str`
:return: the file's content
:rtype: :class:`str`
"""
with open(os.path.join(os.path.dirname(__file__), filename)) as f:
return f.read()
class PyTestCommand(TestCommand):
user_options = [('pytest-args=', 'a', "Arguments to pass to pytest")]
def initialize_options(self):
TestCommand.initialize_options(self)
self.pytest_args = ''
def run_tests(self):
# import here, cause outside the eggs aren't loaded
import pytest
errno = pytest.main(PYTEST_FLAGS + [TESTS_DIRECTORY])
sys.exit(errno)
cmdclass['test'] = PyTestCommand
class CleanCommand(cmd.Command):
"""A custom command to run Pylint on all Python source files."""
description = 'run source clean-up'
user_options = []
def initialize_options(self):
"""Set default values for options."""
pass
def finalize_options(self):
"""Post-process options."""
pass
def run(self):
"""Run command."""
if os.path.exists(os.path.join(MYDIR, 'build')):
print("Removing Build")
shutil.rmtree(os.path.join(MYDIR, 'build'))
if os.path.exists(os.path.join(MYDIR, '.eggs')):
print("Removing .eggs")
shutil.rmtree(os.path.join(MYDIR, '.eggs'))
def clean(diretory, files):
# __pyc__
filenames = glob.glob(os.path.join(directory, '%s' %
(files,)))
for filename in filenames:
print("Removing %s" % filename)
if os.path.isdir(filename):
shutil.rmtree(filename)
else:
os.remove(filename)
for directory, directories, files in os.walk(os.path.join(MYDIR,
PACKAGE)):
clean(directory, '__pycache__')
clean(directory, '*.pyc')
clean(directory, '*.so')
# Add Clean command
cmdclass['clean'] = CleanCommand
class CythonizeCommand(cmd.Command):
"""A custom command to run Pylint on all Python source files."""
description = 'build cython source c files'
user_options = []
def initialize_options(self):
"""Set default values for options."""
pass
def finalize_options(self):
"""Post-process options."""
pass
def run(self):
"""Run command."""
try:
from Cython.Build import cythonize
files = []
for package in list_packages(PACKAGE):
for module in list_modules(os.path.join(MYDIR,
*package.split('.'))):
files.append(os.path.join(*(package.split('.') +
[module + '.py'])))
cythonize(files)
except ImportError:
print('No Cython installed')
# Add Clean command
cmdclass['cythonize'] = CythonizeCommand
# CYTHON / C sources compile
try:
from Cython.Distutils import build_ext
ext_modules = [
Extension(
package + '.' + module,
[os.path.join(*(package.split('.') + [module + '.py']))]
)
for package in list_packages(PACKAGE)
for module in list_modules(os.path.join(MYDIR, *package.split('.')))
]
cmdclass['build_ext'] = build_ext
except ImportError:
ext_modules = []
print('\nNOTE: Cython not installed. '
'Luxon will still work fine, but may run '
'slower.\n')
# define install_requires for specific Python versions
python_version_specific_requires = []
# As of Python >= 2.7 and >= 3.2, the argparse module is maintained within
# the Python standard library, we install it as a separate package
python_version_specific_requires.append('argparse')
# install-requires.txt as install_requires
# minimal dependencies to run.
install_requires = requirements('install-requires.txt')
# docs-requires.txt as docs_requires
# minimal dependencies to run.
docs_requires = requirements('docs-requires.txt')
# tests-requires.txt as tests_requires
# minimal dependencies to run.
tests_requires = requirements('tests-requires.txt')
# dependency-links.txt as dependency_links
# locations of where to find dependencies within
# install-requires.txt ie github.
# setuptools does work with url format for pip.
dependency_links = requirements('dependency-links.txt')
# See here for more options:
# <http://pythonhosted.org/setuptools/setuptools.html>
setup_dict = dict(
name=metadata.package,
version=metadata.version,
author=metadata.author,
author_email=metadata.email,
maintainer=metadata.author,
maintainer_email=metadata.email,
license=metadata.license,
url=metadata.url,
description=metadata.description,
long_description=read('README.rst'),
include_package_data=True,
classifiers=metadata.classifiers,
packages=find_packages(exclude=(TESTS_DIRECTORY,)),
install_requires=[] + python_version_specific_requires + install_requires,
dependency_links=dependency_links,
# Allow tests to be run with `python setup.py test'.
tests_require=install_requires + tests_requires,
cmdclass=cmdclass,
ext_modules=ext_modules,
zip_safe=False, # don't use eggs
)
def main():
setup(**setup_dict)
if __name__ == '__main__':
main()