forked from kuralabs/ninjecto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.py
executable file
·373 lines (284 loc) · 9.4 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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Copyright (C) 2017-2020 KuraLabs S.R.L
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
"""
Usage:
::
./setup.py sdist
./setup.py bdist_wheel
./setup.py bdist_wheel --binary
"""
from pathlib import Path
from setuptools import setup
def check_directory():
"""
You must always change directory to the parent of this file before
executing the setup.py script. setuptools will fail reading files,
including and excluding files from the MANIFEST.in, defining the library
path, etc, if not.
"""
from os import chdir
here = Path(__file__).parent.resolve()
if Path.cwd().resolve() != here:
print('Changing path to {}'.format(here))
chdir(str(here))
check_directory()
#####################
# Utilities #
#####################
def read(filename):
"""
Read the content of a file.
:param str filename: The file to read.
:return: The content of the file.
:rtype: str
"""
with open(filename, mode='r', encoding='utf-8') as fd:
return fd.read()
def find_files(search_path, include=('*', ), exclude=('.*', )):
"""
Find files in a directory.
:param str search_path: Path to search for files.
:param tuple include: Patterns of files to include.
:param tuple exclude: Patterns of files to exclude.
:return: List of files found that matched the include collection but didn't
matched the exclude collection.
:rtype: list
"""
from os import walk
from fnmatch import fnmatch
from os.path import join, normpath
def included_in(fname, patterns):
return any(fnmatch(fname, pattern) for pattern in patterns)
files_collected = []
for root, folders, files in walk(search_path):
files_collected.extend(
normpath(join(root, fname)) for fname in files
if included_in(fname, include) and not included_in(fname, exclude)
)
files_collected.sort()
return files_collected
#####################
# Finders #
#####################
def find_version(filename):
"""
Find version of a package.
This will read and parse a Python module that has defined a __version__
variable. This function does not import the file.
::
setup(
...
version=find_version('lib/package/__init__.py'),
...
)
:param str filename: Path to a Python module with a __version__ variable.
:return: The version of the package.
:rtype: str
"""
import re
content = read(filename)
version_match = re.search(
r"^__version__ = ['\"]([^'\"]*)['\"]", content, re.M
)
if not version_match:
raise RuntimeError('Unable to find version string.')
version = version_match.group(1)
print('Version found:')
print(' {}'.format(version))
print('--')
return version
def find_requirements(filename):
"""
Finds PyPI compatible requirements in a pip requirements.txt file.
In this way requirements needs to be specified only in one, centralized
place:
::
setup(
...
install_requires=find_requirements('requirements.txt'),
...
)
Supports comments and non PyPI requirements (which are ignored).
:param str filename: Path to a requirements.txt file.
:return: List of requirements with version.
:rtype: list
"""
import string
content = read(filename)
requirements = []
ignored = []
for line in content.splitlines():
line = line.strip()
# Comments
if line.startswith('#') or not line:
continue
if line[:1] not in string.ascii_letters:
ignored.append(line)
continue
requirements.append(line)
print('Requirements found:')
for requirement in requirements:
print(' {}'.format(requirement))
print('--')
print('Requirements ignored:')
for requirement in ignored:
print(' {}'.format(requirement))
print('--')
return requirements
def find_data(package_path, data_path, **kwargs):
"""
Find data files in a package.
::
setup(
...
package_data={
'package': find_data(
'lib/package',
'data',
)
},
...
)
:param str package_path: Path to the root of the package.
:param str data_path: Relative path from the root of the package to the
data directory.
:param kwargs: Function supports inclusion and exclusion patterns as
specified in the find_files function.
:return: List of data files found.
:rtype: list
"""
from os.path import join, relpath
files_collected = find_files(join(package_path, data_path), **kwargs)
data_files = [relpath(fname, package_path) for fname in files_collected]
print('Data files found:')
for data_file in data_files:
print(' {}'.format(data_file))
print('--')
return data_files
def find_packages(lib_directory):
"""
Determine packaging options depending on the packaging mode and flags.
Supports:
- Source distribution.
- Pure Python wheels.
- Cythonized binary wheels.
::
setup(
...
**find_packages('lib')
)
:param str lib_directory: Path to the location of the packages.
:return: A dictionary with options for the setup() call depending on
calling parameters.
:rtype: dict
"""
from sys import argv
from os.path import join
if 'bdist_wheel' in argv and '--binary' in argv:
argv.remove('--binary')
from Cython.Build import cythonize
packaging = {
'ext_modules': cythonize(
join(lib_directory, '**/*.py')
),
}
print('Extension modules found:')
for ext_module in packaging['ext_modules']:
print(' {}'.format(ext_module.name))
print('--')
else:
from setuptools import find_packages as find_source_packages
packaging = {
'package_dir': {'': lib_directory},
'packages': find_source_packages(lib_directory),
}
print('Packages found (under {}):'.format(lib_directory))
for package in sorted(packaging['packages']):
print(' {}'.format(package))
print('--')
return packaging
setup(
name='ninjecto',
version=find_version('lib/ninjecto/__init__.py'),
# Dependencies
install_requires=find_requirements('requirements.txt'),
# Optional dependencies
extras_require={
'vault': ['hvac'],
},
# Data files
package_data={
'ninjecto': find_data(
'lib/ninjecto',
'data',
)
},
# Metadata
author='KuraLabs S.R.L',
author_email='info@kuralabs.io',
description=(
'Ninja Injection Tool. '
'Template rendering and variable injection made easy.'
),
long_description=read('README.rst'),
url='https://docs.kuralabs.io/ninjecto/',
keywords='ninjecto',
classifiers=[
'Development Status :: 4 - Beta',
'Intended Audience :: Developers',
'License :: OSI Approved :: Apache Software License',
'Natural Language :: English',
'Operating System :: POSIX :: Linux',
'Programming Language :: Python :: 3',
],
# Sphinx autodoc cannot extract the documentation of zipped eggs with the
# ``.. autodata::`` directive, causing chaos in AutoAPI. This disables zip
# installation for correct AutoAPI generation.
zip_safe=False,
# Entry points
entry_points={
'console_scripts': [
'ninjecto = ninjecto.__main__:main',
],
'ninjecto_plugins_filters_1_0': [
'comment = ninjecto.plugins.filters.comment:filter_comment',
'quote = ninjecto.plugins.filters.quote:filter_quote',
'read = ninjecto.plugins.filters.read:filter_read',
# Support for inflection functions
'camelize = inflection:camelize',
'dasherize = inflection:dasherize',
'humanize = inflection:humanize',
'ordinal = inflection:ordinal',
'ordinalize = inflection:ordinalize',
'parameterize = inflection:parameterize',
'pluralize = inflection:pluralize',
'singularize = inflection:singularize',
'tableize = inflection:tableize',
'titleize = inflection:titleize',
'transliterate = inflection:transliterate',
'underscore = inflection:underscore',
],
'ninjecto_plugins_namespaces_1_0': [
'env = ninjecto.plugins.namespaces.env:namespace_env',
'git = ninjecto.plugins.namespaces.git:namespace_git',
'vault = ninjecto.plugins.namespaces.vault:namespace_vault',
],
},
# Multiple packaging options
**find_packages('lib')
)