-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.py
329 lines (257 loc) · 9.24 KB
/
build.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
# build.py
import os
import codecs
import pathlib
from setuptools import setup as _setup, find_namespace_packages
from typing import Iterable, Any
__all__ = [
"Dependencies",
"parse_requirements",
"unfold",
"get_dependencies",
"build_manifest",
"build_pyproject",
"collect_files",
"setup"
]
class Dependencies:
def __init__(
self,
requirements: Iterable[str] = None,
links: Iterable[str] = None
) -> None:
if requirements is None:
requirements = []
if links is None:
links = []
self.requirements = requirements
self.links = links
def parse_requirements(
source: str | pathlib.Path,
excluded: Iterable[str] = None
) -> Dependencies:
if excluded is None:
excluded = []
links = []
requirements = []
with codecs.open(source, 'r') as requirements_txt:
for line in requirements_txt.readlines():
line = line.replace("\n", "")
if not line or line.replace(" ", "").startswith("#"):
continue
line = line[:line.find("#")] if "#" in line else line
requirement = line.split(";")
for i, part in enumerate(parts := requirement[0].split()):
name = parts[0]
for char in ['=', '<', ">"]:
name = name[:name.find(char)] if char in name else name
# end
if name in excluded:
continue
if (
(len(parts) > 2) and
(part in ("--extra-index-url", "--index-url", "--find-links"))
):
if i == 0:
name = parts[-1]
else:
name = parts[0]
if name in excluded:
continue
links.append(f"{parts[1]}#egg={name}")
requirements.append(";".join([name] + requirement[1:]))
break
if len(parts) == 1 and name not in excluded:
requirements.append(line)
return Dependencies(
requirements=requirements,
links=links
)
def unfold(obj: Any, /, indentation: int = None) -> str:
try:
if isinstance(obj, str):
raise TypeError
string = str(obj)
if not string:
raise TypeError
if indentation is None:
indentation = 1
return (
string[0] + "\n" + ("\t" * indentation) +
(
(",\n" + ("\t" * indentation)).join(
(
unfold(element, indentation=indentation + 1)
for element in iter(obj)
)
if not isinstance(obj, dict) else
(
(
f"{unfold(key, indentation=indentation + 1)}: "
f"{unfold(value, indentation=indentation + 1)}"
)
for key, value in obj.items()
)
)
) + "\n" + ("\t" * (indentation - 1)) +
string[-1]
)
except TypeError:
if isinstance(obj, bool):
return repr(obj).lower()
return repr(obj)
def get_dependencies(
*,
requirements: str | pathlib.Path = None,
dev_requirements: str | pathlib.Path = None
) -> tuple[Dependencies, Dependencies]:
if requirements is not None and os.path.exists(requirements):
dependencies = parse_requirements(source=requirements)
if dev_requirements is not None and os.path.exists(dev_requirements):
extra_dependencies = parse_requirements(source=dev_requirements)
with codecs.open(requirements, 'r') as req_file:
reqs = req_file.read()
with codecs.open(dev_requirements, 'r') as req_dev_file:
dev_reqs = req_dev_file.read()
if reqs not in dev_reqs:
with codecs.open(dev_requirements, 'a') as req_dev_file:
req_dev_file.write(f"\n\n{reqs}")
else:
extra_dependencies = Dependencies()
else:
dependencies = Dependencies()
extra_dependencies = Dependencies()
return dependencies, extra_dependencies
def build_pyproject(
project: str | pathlib.Path = None, **kwargs: Any
) -> None:
if project is not None:
content = "\n".join(
[
f"{key} = {unfold(value, indentation=1)}"
for key, value in kwargs.items() if not (
isinstance(value, dict) or
(
key in (
"long_description",
"license",
'include_package_data',
'dependency_links',
'url',
'install_requires',
'extras_require',
'author',
'packages',
'author_email',
"data_files",
"long_description_content_type"
)
)
)
]
)
if os.path.exists(project):
with codecs.open(project, 'r') as project_file:
project_content = project_file.read()
else:
project_content = ""
if content not in project_content:
with codecs.open(project, 'w') as req_dev_file:
req_dev_file.write(
'[build-system]\n'
'requires = ["setuptools"]\n'
'build-backend = "setuptools.build_meta"\n'
f'\n[project]\n{content}'
)
def build_manifest(
include: Iterable[str] = None, manifest: bool = None
) -> None:
if not os.path.exists("MANIFEST.in") or not manifest:
with codecs.open("MANIFEST.in", 'w') as include_file:
include_file.write("")
if os.path.exists("MANIFEST.in"):
with codecs.open("MANIFEST.in", 'r') as include_file:
include_content = include_file.read()
with codecs.open("MANIFEST.in", 'a') as include_file:
for line in include:
if line not in include_content:
include_file.write(f"include {line}\n")
def collect_files(location: str | pathlib.Path, levels: int = None) -> list[str]:
paths = []
if levels == 0:
return paths
for name in os.listdir(location):
path = pathlib.Path(location) / pathlib.Path(name)
if path.is_file():
paths.append(str(path))
elif path.is_dir():
paths.extend(
collect_files(
str(path), levels=(
levels - 1 if levels is not None else levels
)
)
)
return paths
def setup(
package: str | pathlib.Path, *,
readme: str | bool | pathlib.Path = None,
exclude: Iterable[str | pathlib.Path] = None,
include: Iterable[str | pathlib.Path] = None,
requirements: str | pathlib.Path = None,
dev_requirements: str | pathlib.Path = None,
project: str | pathlib.Path = None,
manifest: bool = None,
**kwargs: Any
) -> None:
if include is None:
include = []
else:
include = list(include)
if readme in (None, True):
readme = 'README.md' if os.path.exists('README.md') else None
if exclude is None:
exclude = ()
if isinstance(readme, (str, pathlib.Path)):
with codecs.open(str(readme), 'r') as desc_file:
long_description = desc_file.read()
else:
long_description = None
dependencies, extra_dependencies = get_dependencies(
requirements=requirements, dev_requirements=dev_requirements
)
include = list(
set(
include + [
file for file in
[requirements, dev_requirements, project, "build.py"]
if file and os.path.exists(str(file))
]
)
)
for included in list(include):
if included is not None and os.path.isdir(str(included)):
include += list(set(collect_files(location=included)))
include = [str(pathlib.Path(path)) for path in include]
packages = [
f"{'.'.join(pathlib.Path(str(package)).parts)}.{content}"
for content in find_namespace_packages(package, exclude=exclude)
] + [package]
kwargs.setdefault("extras_require", {}).setdefault(
"dev", [
req for req in extra_dependencies.requirements
if req not in dependencies.requirements
]
)
build_manifest(include=include, manifest=manifest)
if project:
build_pyproject(project=project, **kwargs)
_setup(
**(dict(project=project) if project else {}),
packages=packages,
long_description=long_description,
include_package_data=True,
dependency_links=dependencies.links,
install_requires=dependencies.requirements,
**kwargs
)