| 1 |
""" |
| 2 |
This script mangles the version in "pyproject.toml" to work around deficiencies |
| 3 |
with TestPyPI. |
| 4 |
""" |
| 5 |
|
| 6 |
import argparse |
| 7 |
import copy |
| 8 |
import re |
| 9 |
import subprocess |
| 10 |
import sys |
| 11 |
from pathlib import ( |
| 12 |
Path) |
| 13 |
|
| 14 |
from packaging.version import ( |
| 15 |
Version) |
| 16 |
|
| 17 |
PYPROJECT_TOML = Path("pyproject.toml") |
| 18 |
VERSION_PY = Path("pathspec/_version.py") |
| 19 |
|
| 20 |
|
| 21 |
def update_pyproject_toml() -> None: |
| 22 |
""" |
| 23 |
Update "pyproject.toml" by mangling its version. |
| 24 |
""" |
| 25 |
print("Get last tag.") |
| 26 |
tag = subprocess.check_output([ |
| 27 |
'git', 'rev-list', '--tags', '--max-count=1', |
| 28 |
], text=True).strip() |
| 29 |
|
| 30 |
print("Get commit count.") |
| 31 |
count = int(subprocess.check_output([ |
| 32 |
'git', 'rev-list', f'{tag}..HEAD', '--count', |
| 33 |
], text=True).strip()) |
| 34 |
|
| 35 |
print(f"Read: {VERSION_PY}") |
| 36 |
version_input = VERSION_PY.read_text() |
| 37 |
version = Version(re.search( |
| 38 |
'^__version__\\s*=\\s*["\'](.+)["\']', version_input, re.M, |
| 39 |
).group(1)) |
| 40 |
if not version.is_postrelease: |
| 41 |
version = copy.replace(version, post=1) |
| 42 |
|
| 43 |
version = copy.replace(version, dev=count) |
| 44 |
|
| 45 |
print(f"Read: {PYPROJECT_TOML}") |
| 46 |
output = PYPROJECT_TOML.read_text() |
| 47 |
|
| 48 |
# Mangle version. |
| 49 |
output = re.sub( |
| 50 |
'^version\\s*=\\s*".+"', f'version = "{version}"', output, count=1, flags=re.M, |
| 51 |
) |
| 52 |
|
| 53 |
print(f"Write: {PYPROJECT_TOML}") |
| 54 |
PYPROJECT_TOML.write_text(output) |
| 55 |
|
| 56 |
|
| 57 |
def main() -> int: |
| 58 |
""" |
| 59 |
Run the script. |
| 60 |
""" |
| 61 |
# Parse command-line arguments. |
| 62 |
parser = argparse.ArgumentParser(description=__doc__) |
| 63 |
parser.parse_args() |
| 64 |
|
| 65 |
update_pyproject_toml() |
| 66 |
|
| 67 |
return 0 |
| 68 |
|
| 69 |
|
| 70 |
if __name__ == '__main__': |
| 71 |
sys.exit(main()) |