forked from roskakori/cutplace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.py
263 lines (213 loc) · 8.66 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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Setup file for cutplace.
This file was generated with PyScaffold 2.2.1, a tool that easily
puts up a scaffold for your new Python project. Learn more under:
http://pyscaffold.readthedocs.org/
"""
# Copyright (C) 2009-2015 Thomas Aglassinger
#
# This program 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 3 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, see <http://www.gnu.org/licenses/>.
import inspect
import glob
import os
import sys
from subprocess import check_call
from distutils.cmd import Command
from distutils.filelist import FileList
import setuptools
from setuptools import setup
from setuptools.command.build_py import build_py
# For Python 2/3 compatibility, pity we can't use six.moves here
try: # try Python 3 imports first
import configparser
except ImportError: # then fall back to Python 2
import ConfigParser as configparser
__location__ = os.path.join(os.getcwd(), os.path.dirname(
inspect.getfile(inspect.currentframe())))
# determine root package and package path if namespace package is used
pyscaffold_version = "2.2.1"
package = "ytec-cutplace"
namespace = []
root_pkg = namespace[0] if namespace else package
if namespace:
pkg_path = os.path.join(*namespace[-1].split('.') + [package])
else:
pkg_path = package
def version2str(version):
if version.exact or not version.distance > 0:
return version.format_with('{tag}')
else:
distance = version.distance
version = str(version.tag)
if '.dev' in version:
version, tail = version.rsplit('.dev', 1)
assert tail == '0', 'own dev numbers are unsupported'
return '{0}.post0.dev{1}'.format(version, distance)
def local_version2str(version):
if version.exact:
if version.dirty:
return version.format_with('+dirty')
else:
return ''
else:
if version.dirty:
return version.format_with('+{node}.dirty')
else:
return version.format_with('+{node}')
class ObjKeeper(type):
instances = {}
def __init__(cls, name, bases, dct):
cls.instances[cls] = []
def __call__(cls, *args, **kwargs):
cls.instances[cls].append(super(ObjKeeper, cls).__call__(*args,
**kwargs))
return cls.instances[cls][-1]
def capture_objs(cls):
from six import add_metaclass
module = inspect.getmodule(cls)
name = cls.__name__
keeper_class = add_metaclass(ObjKeeper)(cls)
setattr(module, name, keeper_class)
cls = getattr(module, name)
return keeper_class.instances[cls]
def get_install_requirements(path):
with open(os.path.join(__location__, path)) as fh:
content = fh.read()
return [req for req in content.splitlines() if req != '']
def read(fname):
with open(os.path.join(__location__, fname)) as fh:
content = fh.read()
return content
def str2bool(val):
return val.lower() in ("yes", "true")
def get_items(parser, section):
try:
items = parser.items(section)
except configparser.NoSectionError:
return []
return items
def prepare_console_scripts(dct):
return ['{cmd} = {func}'.format(cmd=k, func=v) for k, v in dct.items()]
def prepare_extras_require(dct):
return dict([(k, [r.strip() for r in v.split(',')]) for k, v in dct.items()])
def prepare_data_files(dct):
def get_files(pattern):
filelist = FileList()
if '**' in pattern:
pattern = pattern.replace('**', '*')
anchor = False
else:
anchor = True
filelist.include_pattern(pattern, anchor)
return filelist.files
return [(k, [f for p in v.split(',') for f in get_files(p.strip())])
for k, v in dct.items()]
def read_setup_cfg():
config = configparser.SafeConfigParser()
config_file = os.path.join(__location__, 'setup.cfg')
with open(config_file, 'r') as f:
config.readfp(f)
metadata = dict(config.items('metadata'))
classifiers = metadata.get('classifiers', '')
metadata['classifiers'] = [item.strip() for item in classifiers.split(',')]
console_scripts = dict(get_items(config, 'console_scripts'))
console_scripts = prepare_console_scripts(console_scripts)
extras_require = dict(get_items(config, 'extras_require'))
extras_require = prepare_extras_require(extras_require)
data_files = dict(get_items(config, 'data_files'))
data_files = prepare_data_files(data_files)
package_data = metadata.get('package_data', '')
package_data = [item.strip() for item in package_data.split(',') if item]
metadata['package_data'] = package_data
return metadata, console_scripts, extras_require, data_files
def build_cmd_docs():
try:
from sphinx.setup_command import BuildDoc
except ImportError:
class NoSphinx(Command):
user_options = []
def initialize_options(self):
raise RuntimeError("Sphinx documentation is not installed, "
"run: pip install sphinx")
return NoSphinx
class cmd_docs(BuildDoc):
def set_version(self):
from setuptools_scm import get_version
self.release = get_version()
self.version = self.release.split('-', 1)[0]
def run(self):
self.set_version()
if self.builder == "doctest":
import sphinx.ext.doctest as doctest
# Capture the DocTestBuilder class in order to return the total
# number of failures when exiting
ref = capture_objs(doctest.DocTestBuilder)
BuildDoc.run(self)
errno = ref[-1].total_failures
sys.exit(errno)
else:
BuildDoc.run(self)
return cmd_docs
class BuildPyCommand(build_py):
def run(self):
build_py.run(self)
for po in glob.glob('build/lib/*/locale/*/*/*.po'):
mo = po[:-3] + '.mo'
check_call(['msgfmt', po, '-o', mo])
# Assemble everything and call setup(...)
def setup_package():
docs_path = os.path.join(__location__, "docs")
docs_build_path = os.path.join(docs_path, "_build")
needs_pytest = set(['pytest', 'test', 'ptr']).intersection(sys.argv)
pytest_runner = ['pytest-runner==5.3.2'] if needs_pytest else []
pytest = ['pytest==7.0.1'] if needs_pytest else []
install_reqs = get_install_requirements("requirements.txt")
metadata, console_scripts, extras_require, data_files = read_setup_cfg()
command_options = {
'docs': {'project': ('setup.py', package),
'build_dir': ('setup.py', docs_build_path),
'config_dir': ('setup.py', docs_path),
'source_dir': ('setup.py', docs_path)},
'doctest': {'project': ('setup.py', package),
'build_dir': ('setup.py', docs_build_path),
'config_dir': ('setup.py', docs_path),
'source_dir': ('setup.py', docs_path),
'builder': ('setup.py', 'doctest')}
}
setup(name=package,
description=metadata['description'],
author=metadata['author'],
author_email=metadata['author_email'],
license=metadata['license'],
long_description=read('README.rst'),
classifiers=metadata['classifiers'],
test_suite='tests',
packages=setuptools.find_packages(exclude=['tests', 'tests.*']),
namespace_packages=namespace,
install_requires=install_reqs,
setup_requires=['six', 'setuptools_scm<7'] + pytest_runner,
extras_require=extras_require,
cmdclass={'docs': build_cmd_docs(), 'doctest': build_cmd_docs(), 'build_py': BuildPyCommand},
tests_require=['pytest-cov'] + pytest,
package_data={package: metadata['package_data']},
data_files=data_files,
command_options=command_options,
entry_points={'console_scripts': console_scripts},
version=pyscaffold_version,
include_package_data=True,
zip_safe=False) # do not zip egg file after setup.py install
if __name__ == "__main__":
setup_package()