-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.py
executable file
·289 lines (254 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# black --line-length 100
#
# TODO: it is possible to override distutils.dist.Distribution and pass within
# setup `distclass=MyDistribution`. However, it's not clear how to
# remove unsupported commands from possible commands, e.g. `upload_docs`
# and other unused commands. They clutter the `--help`.
"""
Python setup_tools setup.py for goto_http_redirect_server.
Based on sample https://github.com/pypa/sampleproject/blob/master/setup.py
and instructions at
https://packaging.python.org/guides/distributing-packages-using-setuptools/
https://setuptools.readthedocs.io/en/latest/setuptools.html#automatic-script-creation
"""
import abc
from distutils.cmd import Command
import platform
import os
import sys
import subprocess
from setuptools import setup
from goto_http_redirect_server.goto_http_redirect_server import (
__version__,
__author__,
__url_github__,
__url_azure__,
__url_circleci__,
__url_pypi__,
__url_issues__,
__doc__,
)
# XXX: these defaults should match those in tools/ci/service-*.sh files
GOTO_FILE_REDIRECTS = "/usr/local/share/goto_http_redirect_server.csv"
GOTO_CONFIG = "/etc/goto_http_redirect_server.conf"
_HERED = os.path.abspath(os.path.dirname(__file__))
GOTO_SERVICE_FILES = [
os.path.join(_HERED, "service", file_)
for file_ in (
"goto_http_redirect_server.conf",
"goto_http_redirect_server.service",
"goto_http_redirect_server.sh",
"service-install.sh",
"service-uninstall.sh",
)
]
PACKAGE_DATA = GOTO_SERVICE_FILES + [
os.path.join(_HERED, "setup.py"),
os.path.join(_HERED, "README.md"),
]
# Python version >3.6 ?
PYVER_GT36 = sys.version_info.major >= 3 and sys.version_info.minor > 6
# Python version >3.7 ?
PYVER_GT37 = sys.version_info.major >= 3 and sys.version_info.minor > 7
class GotoSetupCommand(Command, abc.ABC):
"""
Base class for goto_http_redirect_server extra commands.
Child classes should define attribute string `description` to overwrite
the setuptools default.
"""
# override user_options for `--help` output and command-line parsing
user_options = ()
def finalize_options(self):
pass
def run_print(self, cmd):
"""
wrap subprocess call with helpful printing
:param cmd: sequence of strings that is an OS command
"""
# XXX: self.verbose default is 1, increments to max 2 if `--verbose`
# is passed. See
# https://github.com/python/cpython/blob/8837dd092fe5ad5184889104e8036811ed839f98/Lib/distutils/dist.py#L148
# https://github.com/python/cpython/blob/8837dd092fe5ad5184889104e8036811ed839f98/Lib/distutils/dist.py#L477
# https://github.com/python/cpython/blob/8837dd092fe5ad5184889104e8036811ed839f98/Lib/distutils/log.py#L69
verbose = self.verbose >= 2
output = None
try:
if verbose:
print(" ".join(cmd), file=sys.stderr)
output = subprocess.check_output(cmd, stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as cpe:
print(str(cpe.output, errors="backslashreplace"), file=sys.stderr)
print("Command (%s) returned %s" % (" ".join(cmd), cpe.returncode), file=sys.stderr)
finally:
if output and verbose:
print(str(output, errors="backslashreplace"), file=sys.stderr)
@abc.abstractmethod
def run(self):
if "linux" not in platform.system().lower():
raise NotImplementedError(
"systemd services are for a Linux system,"
" this is a %s system." % platform.system()
)
class systemd_install(GotoSetupCommand):
"""
install the systemd service
XXX: uses non-PEP8 class name to avoid different naming in
python setup.py --help-commands
and
python setup.py systemd_install --help
The first refers to key in setup.cmdclass.
The second refers to class.__name__.
Force these to match.
"""
script = os.path.join(_HERED, "service", "service-install.sh")
description = (
"install systemd service files for"
+ " goto_http_redirect_server.service (Linux only) - calls %s" % script
)
# these should match service-install.sh
user_options = [
("enable", "e", "enable the systemd service"),
("start", "s", "start the systemd service (requires --enable)"),
]
enable = None
start = None
def initialize_options(self):
self.enable = None
self.start = None
def run(self):
super().run()
# passed to service-install.sh
opts = []
opts += ["--enable"] if self.enable else []
opts += ["--start"] if self.start else []
self.run_print([self.script] + opts)
class systemd_uninstall(GotoSetupCommand):
"""
uninstall the systemd service
XXX: see message about class name in systemd_install
"""
script = os.path.join(_HERED, "service", "service-uninstall.sh")
description = (
"uninstall systemd service files for"
+ " goto_http_redirect_server.service (Linux only) - calls %s" % script
)
# these should match service-uninstall.sh
user_options = [
("reload", "r", "reload the systemd service after service removal"),
("wipe", "w", "remove configuration and csv files"),
]
reload = None
wipe = None
def initialize_options(self):
self.reload = None
self.wipe = None
def run(self):
super().run()
# passed to service-uninstall.sh
opts = []
opts += ["--reload"] if self.reload else []
opts += ["--wipe"] if self.wipe else []
self.run_print([self.script] + opts)
# Get the long description from the README.md file
with open(os.path.join(_HERED, "README.md"), encoding="utf-8") as f_:
long_description = f_.read()
setup(
# `setup` arguments are listed at
# https://github.com/python/cpython/blob/8837dd092fe5ad5184889104e8036811ed839f98/Lib/distutils/dist.py#L1023
name="goto_http_redirect_server",
version=__version__,
author=__author__,
url=__url_pypi__,
# https://packaging.python.org/en/latest/guides/distributing-packages-using-setuptools/#project-urls
project_urls={
"Source": __url_github__,
"Bug Reports": __url_issues__,
"CI (Azure)": __url_azure__,
"CI (CircleCI)": __url_circleci__,
},
description=__doc__.splitlines()[0],
long_description_content_type="text/markdown",
long_description=long_description,
license="MIT License",
install_requires=[],
setup_requires=["wheel"],
extras_require={
# install these locally with command:
# python -m pip install --user -e '.[development]'
"development": [
"flake8==6.0.0" if PYVER_GT37 else "flake8==5.0.4",
"mypy==0.991" if PYVER_GT36 else "mypy==0.971",
"pytest==7.2.0" if PYVER_GT36 else "pytest==6.2.5",
"pytest-cov==4.0.0",
"pytest-timeout==2.1.0",
"yamllint==1.28.0",
],
# subsets of 'development' for faster `pip install` in CI stages
"development-flake8": [
"flake8==6.0.0" if PYVER_GT37 else "flake8==5.0.4",
],
"development-mypy": [
"mypy==0.991" if PYVER_GT36 else "mypy==0.971",
],
"development-pytest": [
"pytest==7.2.0" if PYVER_GT36 else "pytest==6.2.5",
"pytest-cov==4.0.0",
"pytest-timeout==2.1.0",
],
"development-yamllint": [
"yamllint==1.28.0",
],
# install these locally with command:
# python -m pip install --user -e '.[build]'
"build": [
"pip",
"setuptools>=44",
"twine>=3.3",
"wheel",
],
# for CLI update of README.md table of contents
"readme": [
"md_toc",
],
},
# see https://pypi.org/classifiers/
classifiers=[
"Development Status :: 5 - Production/Stable",
"Environment :: Console",
"Operating System :: OS Independent",
"Natural Language :: English",
"License :: OSI Approved :: MIT License",
"Programming Language :: Python :: 3.6",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3 :: Only",
"Topic :: Internet :: WWW/HTTP :: HTTP Servers",
],
# keywords should match "topics" listed at github project
keywords="http-server redirect-urls shortcuts shorturl shorturl-services"
"shorturls url-shortener",
python_requires=">=3.6",
packages=["goto_http_redirect_server"],
# enables `python -m goto-http-redirect-server`
py_modules=["goto-http-redirect-server"],
entry_points={
"console_scripts": [
"goto_http_redirect_server=goto_http_redirect_server.goto_http_redirect_server:main",
],
},
# viewable from `python setup.py --help-commands`
cmdclass={
"systemd_install": systemd_install,
"systemd_uninstall": systemd_uninstall,
},
package_data={
"goto_http_redirect_server": PACKAGE_DATA,
},
include_package_data=True,
)