| 1 |
""" |
| 2 |
This script generates files required for source and wheel distributions, and |
| 3 |
legacy installations. |
| 4 |
""" |
| 5 |
|
| 6 |
import argparse |
| 7 |
import configparser |
| 8 |
import re |
| 9 |
import sys |
| 10 |
from pathlib import ( |
| 11 |
Path) |
| 12 |
try: |
| 13 |
import tomllib # Added in 3.11. |
| 14 |
except ModuleNotFoundError: |
| 15 |
import tomli as tomllib |
| 16 |
|
| 17 |
CHANGES_0_IN_RST = Path("CHANGES_0.in.rst") |
| 18 |
CHANGES_1_IN_RST = Path("CHANGES_1.in.rst") |
| 19 |
CHANGES_RST = Path("CHANGES.rst") |
| 20 |
PYPROJECT_IN_TOML = Path("pyproject.in.toml") |
| 21 |
PYPROJECT_TOML = Path("pyproject.toml") |
| 22 |
README_DIST_RST = Path("README-dist.rst") |
| 23 |
README_RST = Path("README.rst") |
| 24 |
SETUP_CFG = Path("setup.cfg") |
| 25 |
VERSION_PY = Path("pathspec/_version.py") |
| 26 |
|
| 27 |
|
| 28 |
def generate_changes_rst() -> None: |
| 29 |
""" |
| 30 |
Generate the "CHANGES.rst" file from "CHANGES_0.in.rst" and |
| 31 |
"CHANGES_1.in.rst". |
| 32 |
""" |
| 33 |
output: list[str] = [] |
| 34 |
output.append("Change History\n") |
| 35 |
output.append("=" * 14) |
| 36 |
output.append("\n\n") |
| 37 |
|
| 38 |
print(f"Read: {CHANGES_1_IN_RST}") |
| 39 |
output.append(CHANGES_1_IN_RST.read_text()) |
| 40 |
|
| 41 |
print(f"Read: {CHANGES_0_IN_RST}") |
| 42 |
output.append("\n") |
| 43 |
output.append(CHANGES_0_IN_RST.read_text()) |
| 44 |
|
| 45 |
print(f"Write: {CHANGES_RST}") |
| 46 |
CHANGES_RST.write_text("".join(output)) |
| 47 |
|
| 48 |
|
| 49 |
def generate_pyproject_toml() -> None: |
| 50 |
""" |
| 51 |
Generate the "pyproject.toml" file from "pyproject.in.toml". |
| 52 |
""" |
| 53 |
# Flit will only statically extract the version from a predefined list of |
| 54 |
# files for an editable install for some odd reason. |
| 55 |
# - See <https://github.com/pypa/flit/issues/386>. |
| 56 |
|
| 57 |
output: list[str] = [] |
| 58 |
output.append("# GENERATED FILE: Edit 'pyproject.in.toml' instead.\n") |
| 59 |
|
| 60 |
print(f"Read: {PYPROJECT_IN_TOML}") |
| 61 |
text = PYPROJECT_IN_TOML.read_text() |
| 62 |
|
| 63 |
print(f"Read: {VERSION_PY}") |
| 64 |
version_input = VERSION_PY.read_text() |
| 65 |
version = re.search( |
| 66 |
'^__version__\\s*=\\s*["\'](.+)["\']', version_input, re.M, |
| 67 |
).group(1) |
| 68 |
|
| 69 |
# Replace version. |
| 70 |
text = text.replace("__VERSION__", version) |
| 71 |
output.append(text) |
| 72 |
|
| 73 |
print(f"Write: {PYPROJECT_TOML}") |
| 74 |
PYPROJECT_TOML.write_text("".join(output)) |
| 75 |
|
| 76 |
|
| 77 |
def generate_readme_dist() -> None: |
| 78 |
""" |
| 79 |
Generate the "README-dist.rst" file from "README.rst" and "CHANGES_1.in.rst". |
| 80 |
""" |
| 81 |
output: list[str] = [] |
| 82 |
|
| 83 |
print(f"Read: {README_RST}") |
| 84 |
output.append(README_RST.read_text()) |
| 85 |
|
| 86 |
print(f"Read: {CHANGES_1_IN_RST}") |
| 87 |
output.append("\n\n") |
| 88 |
output.append("Change History\n") |
| 89 |
output.append("=" * 14) |
| 90 |
output.append("\n\n") |
| 91 |
output.append(CHANGES_1_IN_RST.read_text()) |
| 92 |
|
| 93 |
print(f"Write: {README_DIST_RST}") |
| 94 |
README_DIST_RST.write_text("".join(output)) |
| 95 |
|
| 96 |
|
| 97 |
def generate_setup_cfg() -> None: |
| 98 |
""" |
| 99 |
Generate the "setup.cfg" file from "pyproject.toml" in order to support legacy |
| 100 |
installation with "setup.py". |
| 101 |
""" |
| 102 |
print(f"Read: {PYPROJECT_TOML}") |
| 103 |
with PYPROJECT_TOML.open('rb') as fh: |
| 104 |
config = tomllib.load(fh) |
| 105 |
|
| 106 |
print(f"Write: {SETUP_CFG}") |
| 107 |
output = configparser.ConfigParser() |
| 108 |
output['metadata'] = { |
| 109 |
'author': config['project']['authors'][0]['name'], |
| 110 |
'author_email': config['project']['authors'][0]['email'], |
| 111 |
'classifiers': "\n" + "\n".join(config['project']['classifiers']), |
| 112 |
'description': config['project']['description'], |
| 113 |
'license': config['project']['license']['text'], |
| 114 |
'long_description': f"file: {config['project']['readme']}", |
| 115 |
'long_description_content_type': "text/x-rst", |
| 116 |
'name': config['project']['name'], |
| 117 |
'url': config['project']['urls']['Source Code'], |
| 118 |
'version': "attr: pathspec._version.__version__", |
| 119 |
} |
| 120 |
output['options'] = { |
| 121 |
'packages': "find:", |
| 122 |
'python_requires': config['project']['requires-python'], |
| 123 |
'setup_requires': "setuptools>=40.8.0", |
| 124 |
'test_suite': "tests", |
| 125 |
} |
| 126 |
output['options.packages.find'] = { |
| 127 |
'include': "pathspec, pathspec.*", |
| 128 |
} |
| 129 |
|
| 130 |
with SETUP_CFG.open('w') as fh: |
| 131 |
output.write(fh) |
| 132 |
|
| 133 |
|
| 134 |
def main() -> int: |
| 135 |
""" |
| 136 |
Run the script. |
| 137 |
""" |
| 138 |
# Parse command-line arguments. |
| 139 |
parser = argparse.ArgumentParser(description=__doc__) |
| 140 |
parser.parse_args() |
| 141 |
|
| 142 |
generate_changes_rst() |
| 143 |
generate_readme_dist() |
| 144 |
generate_pyproject_toml() |
| 145 |
generate_setup_cfg() |
| 146 |
|
| 147 |
return 0 |
| 148 |
|
| 149 |
|
| 150 |
if __name__ == '__main__': |
| 151 |
sys.exit(main()) |