forked from skonfig/skonfig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.py
329 lines (258 loc) · 9.84 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
import os
import collections
import re
import subprocess
import glob
# Set locale
try:
import locale
locale.setlocale(locale.LC_ALL, "C")
except:
pass
import logging
log = logging.getLogger("setup.py")
class Version:
"""this class is a stripped down version of distutils.version.LooseVersion
before it was removed from CPython.
"""
component_re = re.compile(r'(\d+ | [a-z]+ | \.)', re.VERBOSE)
def __eq__(self, other):
return self._cmp(other) == 0
def __lt__(self, other):
return self._cmp(other) < 0
def __le__(self, other):
return self._cmp(other) <= 0
def __gt__(self, other):
return self._cmp(other) > 0
def __ge__(self, other):
return self._cmp(other) >= 0
def __init__(self, vstring):
self.parse(vstring)
def parse(self, vstring):
components = [x for x in self.component_re.split(vstring)
if x and x != '.']
for i, obj in enumerate(components):
try:
components[i] = int(obj)
except ValueError:
pass
self.version = components
def _cmp(self, other):
if isinstance(other, str):
other = Version(other)
if self.version == other.version:
return 0
if self.version < other.version:
return -1
if self.version > other.version:
return 1
# Import setuptools / distutils
try:
import setuptools
from setuptools import setup
using_setuptools = True
if Version(setuptools.__version__) < Version("38.6.0"):
# setuptools 38.6.0 is required for connections to PyPI.
# cf. also _pypi_can_connect()
log.warn("You are running an old version of setuptools: %s. "
"Consider upgrading." % (setuptools.__version__))
except ImportError:
from distutils.core import setup
using_setuptools = False
log.warn("You are running %s using distutils. "
"Please consider installing setuptools." % (__file__))
import distutils.command.build
import distutils.command.build_py
import distutils.command.clean
import distutils.command.sdist
class ManPages:
man_dir = os.path.join(os.path.dirname(__file__), "docs", "man")
rst_glob = glob.glob(os.path.join(man_dir, "man*", "*.rst"))
@classmethod
def _render_manpage(cls, rst_path, dest_path):
from docutils.core import publish_file
from docutils.writers import manpage
publish_file(
source_path=rst_path,
destination_path=dest_path,
writer=manpage.Writer(),
settings_overrides={
"language_code": "en",
"report_level": 1, # info
"halt_level": 1, # info
"smart_quotes": True,
"syntax_highlight": "none",
"traceback": True
})
@classmethod
def build(cls, distribution, dry_run=False):
try:
import docutils # noqa
except ImportError:
log.warn(
"docutils is not available, no man pages will be generated")
return
log.info("generating man pages")
man_pages = collections.defaultdict(list)
# convert man pages
for path in cls.rst_glob:
pagename = os.path.splitext(os.path.basename(path))[0]
section_dir = os.path.basename(os.path.dirname(path))
section = section_dir.replace("man", "", 1)
destpath = os.path.join(
os.path.dirname(path), "%s.%s" % (pagename, section))
log.info("generating man page %s" % (destpath))
if not dry_run:
cls._render_manpage(path, destpath)
man_pages[section_dir].append(destpath)
# add man pages to data_files so that they are installed
cls.update_data_files(distribution.data_files)
@classmethod
def find(cls, group_by_section=False):
for section_path in glob.glob(os.path.join(cls.man_dir, "man*/")):
section_name = os.path.basename(section_path.rstrip("/"))
manglob = os.path.join(
section_path, "*." + section_name.replace("man", "", 1))
if group_by_section:
yield (section_name, list(glob.glob(manglob)))
else:
for manpage in glob.glob(manglob):
yield manpage
@classmethod
def update_data_files(cls, data_files):
for section_name, manpages in cls.find(group_by_section=True):
inst_dir = os.path.join("share/man", section_name)
for d, files in data_files:
if d == inst_dir:
break
else:
files = []
data_files.append((inst_dir, files))
for manpage in manpages:
if manpage not in files:
files.append(manpage)
@classmethod
def clean(cls, distribution, dry_run=False):
for manpage in cls.find(group_by_section=False):
log.info("removing %s" % (manpage))
if not dry_run:
os.remove(manpage)
def hardcode_version(file):
log.info("injecting version number into %s", file)
with open(file, "w") as f:
f.write('VERSION = "%s"\n' % (__import__("skonfig").__version__))
class skonfig_build(distutils.command.build.build):
def run(self):
distutils.command.build.build.run(self)
# Build man pages
ManPages.build(self.distribution, dry_run=self.dry_run)
class skonfig_build_py(distutils.command.build_py.build_py):
def build_module(self, module, module_file, package):
(dest_name, copied) = super().build_module(
module, module_file, package)
if dest_name == os.path.join(self.build_lib, "skonfig", "version.py") \
and not self.dry_run:
# Hard code generated version number into source distribution
if os.path.exists(dest_name):
os.remove(dest_name)
hardcode_version(dest_name)
return (dest_name, copied)
class skonfig_build_manpages(distutils.command.build.build):
def run(self):
ManPages.build(self.distribution, dry_run=self.dry_run)
class skonfig_sdist(distutils.command.sdist.sdist):
def make_release_tree(self, base_dir, files):
distutils.command.sdist.sdist.make_release_tree(self, base_dir, files)
# Hard code generated version number into source distribution
version_file = os.path.join(base_dir, "skonfig", "version.py")
if os.path.exists(version_file):
os.remove(version_file)
hardcode_version(version_file)
class skonfig_clean(distutils.command.clean.clean):
def run(self):
ManPages.clean(self.distribution, dry_run=self.dry_run)
distutils.command.clean.clean.run(self)
def _pypi_can_connect():
# PyPI requires an SSL client with SNI and TLSv1.2 support.
# https://github.com/pypi/warehouse/issues/3411
# https://github.com/pypa/pypi-support/issues/978
# for setup_require (which uses easy_install in the back),
# setuptools >= 36.8.0 is also required, older versions produce:
# distutils.errors.DistutilsError: Could not find suitable distribution for Requirement.parse('docutils')
import ssl
try:
# Python >= 3.7
supports_tlsv1_2 = hasattr(ssl.TLSVersion, "TLSv1_2")
except AttributeError:
# Python < 3.10
supports_tlsv1_2 = hasattr(ssl, "PROTOCOL_TLSv1_2")
return \
ssl.HAS_SNI \
and supports_tlsv1_2 \
and Version(setuptools.__version__) >= Version("36.8.0")
if using_setuptools:
# setuptools specific setup() keywords
kwargs = dict(
python_requires=">=3.2",
)
if _pypi_can_connect():
kwargs["setup_requires"] = ["docutils"]
else:
# distutils specific setup() keywords
kwargs = dict()
data_files = [
("share/doc/skonfig", [
os.path.join(os.path.dirname(__file__), s) for s in [
"README.md",
"docs/src/config.skeleton",
"docs/changelog"
]
]),
]
# include examples recursively
for dirname, _, files in os.walk(
os.path.join(os.path.dirname(__file__), "docs", "examples")):
data_files.append(
(os.path.join(
"share/doc/skonfig/examples",
dirname.split("/examples", 1)[1].lstrip("/")).rstrip("/"),
[os.path.join(dirname, f) for f in files]))
# include man pages
ManPages.update_data_files(data_files)
setup(
name="skonfig",
packages=["skonfig", "cdist", "cdist.core", "cdist.exec", "cdist.util"],
scripts=glob.glob(os.path.join(os.path.dirname(__file__), "bin", "*")),
version=__import__("skonfig").__version__,
description="system configuration framework",
author="skonfig nerds",
url="https://skonfig.li",
data_files=data_files,
cmdclass={
"build": skonfig_build,
"build_py": skonfig_build_py,
"sdist": skonfig_sdist,
"clean": skonfig_clean,
# custom commands
"build_manpages": skonfig_build_manpages,
},
classifiers=[
"Development Status :: 6 - Mature",
"Environment :: Console",
"Intended Audience :: System Administrators",
"License :: OSI Approved :: GNU General Public License v3 or later (GPLv3+)", # noqa
"Operating System :: MacOS :: MacOS X",
"Operating System :: POSIX",
"Operating System :: POSIX :: BSD",
"Operating System :: POSIX :: Linux",
"Operating System :: Unix",
"Programming Language :: Python",
"Programming Language :: Python :: 3",
"Topic :: System :: Boot",
"Topic :: System :: Installation/Setup",
"Topic :: System :: Operating System",
"Topic :: System :: Software Distribution",
"Topic :: Utilities"
],
**kwargs
)