/
setup.py
executable file
·351 lines (305 loc) · 11.6 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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#!/usr/bin/env python
#
# (C) Copyright 2004 Hewlett-Packard Development Company, L.P.
# Copyright 2017 IBM Corp.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
"""
Setup script for pywbem project.
"""
import sys
import os
import io
import re
import fnmatch
# setuptools needs to be imported before distutils in order to work.
import setuptools
from distutils import log # pylint: disable=wrong-import-order
# Just in case, we keep Cython usage conditional on package availability
try:
from Cython.Build import cythonize
from Cython.Compiler.Errors import CompileError
except ImportError:
cythonize = None
from wheel.bdist_wheel import bdist_wheel as bdist_wheel_base
def get_version(version_file):
"""
Execute the specified version file and return the value of the __version__
global variable that is set in the version file.
Note: Make sure the version file does not depend on any packages in the
requirements list of this package (otherwise it cannot be executed in
a fresh Python environment).
"""
with io.open(version_file, 'r', encoding='utf-8') as fp:
version_source = fp.read()
_globals = {}
exec(version_source, _globals) # pylint: disable=exec-used
return _globals['__version__']
def get_requirements(requirements_file):
"""
Parse the specified requirements file and return a list of its non-empty,
non-comment lines. The returned lines are without any trailing newline
characters.
"""
with io.open(requirements_file, 'r', encoding='utf-8') as fp:
lines = fp.readlines()
reqs = []
for line in lines:
line = line.strip(' \n')
if not line.startswith('#') and not line.startswith('-r ') and \
line != '':
reqs.append(line)
return reqs
def read_file(a_file):
"""
Read the specified file and return its content as one string.
"""
with io.open(a_file, 'r', encoding='utf-8') as fp:
content = fp.read()
return content
def find_files(dir_path, fn_pattern):
"""
Find files in a directory and its subdirectories that match a file name
pattern.
Returns a list of path names of the found files. The path names begin
with the specified directory path name, so if that was absolute or relative,
the returned file path names will be absolute or relative, respectively.
"""
found_files = []
for dirpath, _, filenames in os.walk(dir_path):
for filename in fnmatch.filter(filenames, fn_pattern):
found_files.append(os.path.join(dirpath, filename))
return found_files
def cythonizable_source_files():
"""
Return the list of path names of files to be cythonized (relative to
current directory).
"""
setup_dir = os.path.dirname(__file__)
# os.path.dirname() returns the empty string on Python 2.7 when run from
# the work directory, and the empty string is rejected by os.path.relpath().
if os.path.isabs(setup_dir):
setup_dir = os.path.relpath(setup_dir)
py_files = []
py_files += find_files(os.path.join(setup_dir, 'pywbem'), '*.py')
py_files += find_files(os.path.join(setup_dir, 'pywbem_mock'), '*.py')
return py_files
class PytestCommand(setuptools.Command):
"""
Base class for setup.py commands for executing tests for this package
using pytest.
Note on the class name: Because distutils.dist._show_help() shows the class
name for the setup.py command name instead of invoking get_command_name(),
the classes that get registered as commands must have the command name.
"""
description = None # Set by subclass
my_test_dirs = None # Set by subclass
user_options = [
(
'pytest-options=', # '=' indicates it requires an argument
None, # no short option
"additional options for pytest, as one argument"
),
]
def initialize_options(self):
"""
Standard method called by setup to initialize options for the command.
"""
# pylint: disable=attribute-defined-outside-init
self.test_opts = None
self.test_dirs = None
self.pytest_options = None
# pylint: enable=attribute-defined-outside-init
def finalize_options(self):
"""
Standard method called by setup to finalize options for the command.
"""
# pylint: disable=attribute-defined-outside-init
self.test_opts = [
'--color=yes',
'-s',
'-W', 'default',
'-W', 'ignore::PendingDeprecationWarning',
]
if sys.version_info[0] == 3:
self.test_opts.extend([
'-W', 'ignore::ResourceWarning',
])
self.test_dirs = self.my_test_dirs
# pylint: enable=attribute-defined-outside-init
def run(self):
"""
Standard method called by setup to execute the command.
"""
# deferred import so install does not depend on it
import pytest # pylint: disable=import-outside-toplevel
args = self.test_opts
if self.pytest_options:
args.extend(self.pytest_options.split(' '))
args.extend(self.test_dirs)
if self.dry_run:
self.announce("Dry-run: pytest {}".format(' '.join(args)),
level=log.INFO)
return 0
self.announce("pytest {}".format(' '.join(args)),
level=log.INFO)
rc = pytest.main(args)
return rc
class test(PytestCommand):
# pylint: disable=invalid-name
"""
Setup.py command for executing unit and function tests.
"""
description = "pywbem: Run unit and function tests using pytest"
my_test_dirs = ['tests/unittest', 'tests/functiontest']
class leaktest(PytestCommand):
# pylint: disable=invalid-name
"""
Setup.py command for executing leak tests.
"""
description = "pywbem: Run leak tests using pytest"
my_test_dirs = ['tests/leaktest']
class end2endtest(PytestCommand):
# pylint: disable=invalid-name
"""
Setup.py command for executing end2end tests.
"""
description = "pywbem: Run end2end tests using pytest"
my_test_dirs = ['tests/end2endtest']
def finalize_options(self):
PytestCommand.finalize_options(self) # old-style class
self.test_opts.extend([
'-v', '--tb=short',
])
class bdist_wheel(bdist_wheel_base):
# pylint: disable=invalid-name
"""
Replacement for setup.py command 'bdist_wheel' that provides a
'--cythonized' option that causes the wheel archive to be built as
a cythonized version.
The handling of the option is in the module level code because it needs
to change the setuptools.setup() arguments.
"""
user_options = bdist_wheel_base.user_options + [
# (long option, short option, description)
('cythonized', None,
'pywbem: Make a cythonized wheel (default: uncythonized wheel)'),
]
# pylint: disable=invalid-name
requirements = get_requirements('requirements.txt')
install_requires = [req for req in requirements
if req and not re.match(r'[^:]+://', req)]
dependency_links = [req for req in requirements
if req and re.match(r'[^:]+://', req)]
test_requirements = get_requirements('test-requirements.txt')
package_version = get_version(os.path.join('pywbem', '_version.py'))
# pylint: enable=invalid-name
# Docs on setup():
# * https://docs.python.org/2.7/distutils/apiref.html?
# highlight=setup#distutils.core.setup
# * https://setuptools.readthedocs.io/en/latest/setuptools.html#
# new-and-changed-setup-keywords
setup_options = dict(
name='pywbem',
version=package_version,
packages=[
'pywbem',
'pywbem_mock'
],
include_package_data=True, # Includes MANIFEST.in files into sdist (only)
scripts=[
'mof_compiler', 'mof_compiler.bat'
],
requires=['setuptools', 'wheel'],
install_requires=install_requires,
dependency_links=dependency_links,
extras_require={
"test": test_requirements,
},
cmdclass={
'test': test,
'leaktest': leaktest,
'end2endtest': end2endtest,
'bdist_wheel': bdist_wheel,
},
description='pywbem - A WBEM client',
long_description=read_file('README_PYPI.rst'),
long_description_content_type='text/x-rst',
license='LGPL version 2.1, or (at your option) any later version',
author='Tim Potter',
author_email='tpot@hp.com',
maintainer='Andreas Maier, Karl Schopmeyer',
maintainer_email='maiera@de.ibm.com, k.schopmeyer@swbell.net',
url='https://pywbem.github.io/pywbem/',
project_urls={
'Bug Tracker': 'https://github.com/pywbem/pywbem/issues',
'Documentation': 'https://pywbem.readthedocs.io/en/latest/',
'Source Code': 'https://github.com/pywbem/pywbem',
},
options={'bdist_wheel': {'universal': True}},
zip_safe=True, # This package can safely be installed from a zip file
platforms='any',
# Keep these Python versions in sync with pywbem/__init__.py
python_requires='>=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*',
classifiers=[
'Development Status :: 5 - Production/Stable',
'Environment :: Console',
'Intended Audience :: Developers',
'Intended Audience :: System Administrators',
'License :: OSI Approved :: '
'GNU Lesser General Public License v2 or later (LGPLv2+)',
'Operating System :: OS Independent',
'Programming Language :: Python :: 2',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7',
'Programming Language :: Python :: 3.8',
'Programming Language :: Python :: 3.9',
'Topic :: Software Development :: Libraries :: Python Modules',
'Topic :: System :: Systems Administration',
]
)
CYTHONIZED = False
if cythonize and '--cythonized' in sys.argv:
sys.argv.remove('--cythonized')
if 'bdist_wheel' in sys.argv:
CYTHONIZED = True
if CYTHONIZED:
src_files = cythonizable_source_files()
print("Cythonizing pywbem source files")
if '--dry-run' in sys.argv:
for fn in src_files:
print("Dry-run: Cythonizing: {}".format(fn))
cython_ext_modules = []
else:
try:
cython_ext_modules = cythonize(
src_files,
build_dir='build_cythonize',
annotate=False,
compiler_directives=dict(
language_level=str(sys.version_info[0])))
except CompileError as exc:
print("Error: Cython compile error: {}".format(exc))
sys.exit(1)
setup_options['requires'] += ['Cython']
setup_options['ext_modules'] = cython_ext_modules
setup_options['options']['build'] = {'build_lib': 'build_build_ext'}
setup_options['zip_safe'] = False
setuptools.setup(**setup_options)