-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathprebuild.py
More file actions
151 lines (122 loc) · 3.84 KB
/
prebuild.py
File metadata and controls
151 lines (122 loc) · 3.84 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
"""
This script generates files required for source and wheel distributions, and
legacy installations.
"""
import argparse
import configparser
import re
import sys
from pathlib import (
Path)
try:
import tomllib # Added in 3.11.
except ModuleNotFoundError:
import tomli as tomllib
CHANGES_0_IN_RST = Path("CHANGES_0.in.rst")
CHANGES_1_IN_RST = Path("CHANGES_1.in.rst")
CHANGES_RST = Path("CHANGES.rst")
PYPROJECT_IN_TOML = Path("pyproject.in.toml")
PYPROJECT_TOML = Path("pyproject.toml")
README_DIST_RST = Path("README-dist.rst")
README_RST = Path("README.rst")
SETUP_CFG = Path("setup.cfg")
VERSION_PY = Path("pathspec/_version.py")
def generate_changes_rst() -> None:
"""
Generate the "CHANGES.rst" file from "CHANGES_0.in.rst" and
"CHANGES_1.in.rst".
"""
output: list[str] = []
output.append("Change History\n")
output.append("=" * 14)
output.append("\n\n")
print(f"Read: {CHANGES_1_IN_RST}")
output.append(CHANGES_1_IN_RST.read_text())
print(f"Read: {CHANGES_0_IN_RST}")
output.append("\n")
output.append(CHANGES_0_IN_RST.read_text())
print(f"Write: {CHANGES_RST}")
CHANGES_RST.write_text("".join(output))
def generate_pyproject_toml() -> None:
"""
Generate the "pyproject.toml" file from "pyproject.in.toml".
"""
# Flit will only statically extract the version from a predefined list of
# files for an editable install for some odd reason.
# - See <https://github.com/pypa/flit/issues/386>.
output: list[str] = []
output.append("# GENERATED FILE: Edit 'pyproject.in.toml' instead.\n")
print(f"Read: {PYPROJECT_IN_TOML}")
text = PYPROJECT_IN_TOML.read_text()
print(f"Read: {VERSION_PY}")
version_input = VERSION_PY.read_text()
version = re.search(
'^__version__\\s*=\\s*["\'](.+)["\']', version_input, re.M,
).group(1)
# Replace version.
text = text.replace("__VERSION__", version)
output.append(text)
print(f"Write: {PYPROJECT_TOML}")
PYPROJECT_TOML.write_text("".join(output))
def generate_readme_dist() -> None:
"""
Generate the "README-dist.rst" file from "README.rst" and "CHANGES_1.in.rst".
"""
output: list[str] = []
print(f"Read: {README_RST}")
output.append(README_RST.read_text())
print(f"Read: {CHANGES_1_IN_RST}")
output.append("\n\n")
output.append("Change History\n")
output.append("=" * 14)
output.append("\n\n")
output.append(CHANGES_1_IN_RST.read_text())
print(f"Write: {README_DIST_RST}")
README_DIST_RST.write_text("".join(output))
def generate_setup_cfg() -> None:
"""
Generate the "setup.cfg" file from "pyproject.toml" in order to support legacy
installation with "setup.py".
"""
print(f"Read: {PYPROJECT_TOML}")
with PYPROJECT_TOML.open('rb') as fh:
config = tomllib.load(fh)
print(f"Write: {SETUP_CFG}")
output = configparser.ConfigParser()
output['metadata'] = {
'author': config['project']['authors'][0]['name'],
'author_email': config['project']['authors'][0]['email'],
'classifiers': "\n" + "\n".join(config['project']['classifiers']),
'description': config['project']['description'],
'license': config['project']['license']['text'],
'long_description': f"file: {config['project']['readme']}",
'long_description_content_type': "text/x-rst",
'name': config['project']['name'],
'url': config['project']['urls']['Source Code'],
'version': "attr: pathspec._version.__version__",
}
output['options'] = {
'packages': "find:",
'python_requires': config['project']['requires-python'],
'setup_requires': "setuptools>=40.8.0",
'test_suite': "tests",
}
output['options.packages.find'] = {
'include': "pathspec, pathspec.*",
}
with SETUP_CFG.open('w') as fh:
output.write(fh)
def main() -> int:
"""
Run the script.
"""
# Parse command-line arguments.
parser = argparse.ArgumentParser(description=__doc__)
parser.parse_args()
generate_changes_rst()
generate_readme_dist()
generate_pyproject_toml()
generate_setup_cfg()
return 0
if __name__ == '__main__':
sys.exit(main())