Repository navigation
Expand file tree
/
Copy pathsetup.py
More file actions
297 lines (275 loc) · 6.86 KB
/
Copy pathsetup.py
File metadata and controls
297 lines (275 loc) · 6.86 KB
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
# -*- coding: utf-8 -*-
from os import environ
from json import dumps
from pathlib import Path
from types import TracebackType
from typing import (
Mapping,
Optional,
)
from setuptools import (
find_packages,
setup,
)
from setuptools.command.build import build
from setuptools.command.editable_wheel import editable_wheel
from setuptools.command.egg_info import egg_info
from setuptools.command.sdist import sdist
from setuptools_scm import get_version
from wheel.wheelfile import WheelFile
PACKAGE = 'DefStream'
SOURCE = 'src'
IMPLEMENTATIONS = {
'RabbitMQ': 'Broker.Implements.RabbitMQ',
'Redis': 'Broker.Implements.Redis',
}
DEPENDENCIES = {
'RabbitMQ': ['pika>=1.3.2'],
'Redis': ['redis>=5.1.1'],
}
TARGET_FILE = Path('.distribution-target')
ARCHIVE_TARGET: Optional[str] = (
TARGET_FILE.read_text(encoding='utf-8').strip()
if TARGET_FILE.is_file() else None
)
TARGET: str = environ.get(
'DEF_BUILD_TARGET',
ARCHIVE_TARGET or 'core',
)
EDITABLE: bool = 'DEF_EDITABLE_EXTRAS' in environ
EXTRAS: list[str] = [
target for target in environ.get('DEF_EDITABLE_EXTRAS', '').split(',') if target
]
if any(target not in IMPLEMENTATIONS for target in EXTRAS):
raise ValueError('Unknown editable EXTRAS selection')
if TARGET not in (
'core',
*IMPLEMENTATIONS,
):
raise ValueError('Unknown DEF_BUILD_TARGET: {!r}'.format(TARGET))
if ARCHIVE_TARGET is not None and TARGET != ARCHIVE_TARGET:
raise ValueError('Cannot change the build target of a source distribution')
VERSION: str = get_version(
dist_name=PACKAGE,
tag_regex=r'^v?(?P<version>[0-9]+\.[0-9]+\.[0-9]+(?:\.[0-9]+)?(?:(?:rc|\.post)[0-9]+)?)$',
)
NAME = PACKAGE if TARGET == 'core' else '{}-{}'.format(
PACKAGE,
TARGET,
)
PACKAGES = [PACKAGE] if TARGET == 'core' else []
PACKAGE_DIR = {PACKAGE: SOURCE} if TARGET == 'core' else {}
for package in find_packages(SOURCE):
selected = (
not any(
(package == path or package.startswith(path + '.'))
and target not in EXTRAS
for target, path in IMPLEMENTATIONS.items()
)
if TARGET == 'core' else (
package == IMPLEMENTATIONS[TARGET]
or package.startswith(IMPLEMENTATIONS[TARGET] + '.')
)
)
if not selected:
continue
qualified = '{}.{}'.format(
PACKAGE,
package,
)
PACKAGES.append(qualified)
PACKAGE_DIR[qualified] = '{}/{}'.format(
SOURCE,
package.replace(
'.',
'/',
),
)
class TargetBuild(build):
def initialize_options(self) -> None:
super().initialize_options()
self.build_base = 'build/{}'.format(TARGET)
return
class TargetEggInfo(egg_info):
def find_sources(self) -> None:
super().find_sources()
self.filelist.extend([
'.dev/scripts/editable_view.py',
'.github/scripts/build_backend.py',
'.github/scripts/build_distributions.py',
])
self.filelist.sort()
self.filelist.remove_duplicates()
self.write_file(
'manifest file',
str(Path(self.egg_info) / 'SOURCES.txt'),
'\n'.join(self.filelist.files) + '\n',
)
return
def finalize_options(self) -> None:
if self.egg_base is None:
self.egg_base = 'build/{}'.format(TARGET)
Path(self.egg_base).mkdir(
parents=True,
exist_ok=True,
)
super().finalize_options()
return
class TargetSdist(sdist):
def make_release_tree(
self,
base_dir: str,
files: list[str],
) -> None:
super().make_release_tree(
base_dir,
files,
)
(Path(base_dir) / TARGET_FILE).write_text(
TARGET + '\n',
encoding='utf-8',
)
return
class EditableView:
def __init__(
self,
name: str,
build_lib: str,
) -> None:
self.name = name
self.build_lib = Path(build_lib).resolve()
return
def __enter__(self) -> 'EditableView':
return self
def __exit__(
self,
exception_type: Optional[type[BaseException]],
exception: Optional[BaseException],
traceback: Optional[TracebackType],
) -> None:
return
def __call__(
self,
wheel: WheelFile,
files: list[str],
mapping: Mapping[str, str],
) -> None:
sources: dict[str, str] = {
Path(output).resolve().relative_to(self.build_lib).as_posix(): str(Path(source).resolve())
for output, source in mapping.items()
}
for output in files:
relative: str = Path(output).resolve().relative_to(self.build_lib).as_posix()
if relative not in sources:
raise ValueError('Editable output has no persistent source: {}'.format(output))
helper: str = '__editable_{}'.format(NAME.lower().replace(
'-',
'_',
))
wheel.writestr(
helper + '.py',
Path('.dev/scripts/editable_view.py').read_bytes(),
)
wheel.writestr(
self.name + '.dist-info/editable-files.json',
dumps(sources),
)
wheel.writestr(
self.name + '.dist-info/editable-selection.json',
dumps(EXTRAS),
)
names: tuple[str, ...] = (
PACKAGE,
*('{}-{}'.format(
PACKAGE,
target,
) for target in IMPLEMENTATIONS),
)
wheel.writestr(
'__editable__.{}.pth'.format(self.name),
'import {0}; {0}.activate({1!r}, {2!r})\n__editable_{3}_view\n'.format(
helper,
PACKAGE,
names,
PACKAGE.lower(),
),
)
return
class TargetEditableWheel(editable_wheel):
def finalize_options(self) -> None:
super().finalize_options()
mode = 'strict'
if self.mode not in (
None,
mode,
):
raise ValueError('This build target requires {} editable mode'.format(mode))
self.mode = mode
return
def _select_strategy(
self,
name: str,
tag: str,
build_lib: str,
) -> EditableView:
return EditableView(
name,
build_lib,
)
setup(
name=NAME,
version=VERSION,
description='Synchronous message processing with pluggable broker adapters',
long_description=Path('README.setup.md').read_text(encoding='utf-8'),
long_description_content_type='text/markdown',
author='Heo Yongseon',
url='https://github.com/yong5eon/DefStream',
project_urls={
'Source': 'https://github.com/yong5eon/DefStream',
'Issues': 'https://github.com/yong5eon/DefStream/issues',
'Releases': 'https://github.com/yong5eon/DefStream/releases',
},
cmdclass={
'build': TargetBuild,
'egg_info': TargetEggInfo,
'sdist': TargetSdist,
'editable_wheel': TargetEditableWheel,
},
packages=PACKAGES,
package_dir=PACKAGE_DIR,
include_package_data=False,
package_data={
'DefStream.Inspector.Extensions': [
'Play.html',
'Status.html',
],
} if TARGET == 'core' else {},
classifiers=[
'Programming Language :: Python :: 3.9',
'Programming Language :: Python :: 3.10',
'Programming Language :: Python :: 3.11',
'Programming Language :: Python :: 3.12',
'Programming Language :: Python :: 3.13',
'Programming Language :: Python :: 3.14',
],
install_requires=[
dependency for target in EXTRAS for dependency in DEPENDENCIES[target]
] if EDITABLE else [] if TARGET == 'core' else [
'{}=={}'.format(
PACKAGE,
VERSION,
),
*DEPENDENCIES[TARGET],
],
extras_require={
target: DEPENDENCIES[target] for target in IMPLEMENTATIONS
} if EDITABLE else {
target: ['{}-{}=={}'.format(
PACKAGE,
target,
VERSION,
)]
for target in IMPLEMENTATIONS
} if TARGET == 'core' else {},
python_requires='>=3.9',
)