| 1 |
#!/usr/bin/python3 |
| 2 |
|
| 3 |
import re |
| 4 |
import sys |
| 5 |
|
| 6 |
|
| 7 |
def make_pep440_compliant(version: str) -> str: |
| 8 |
"""Convert the version into a PEP440 compliant version.""" |
| 9 |
public_version_re = re.compile( |
| 10 |
r"^([0-9][0-9.]*(?:(?:a|b|rc|.post|.dev)[0-9]+)*)\+?" |
| 11 |
) |
| 12 |
_, public, local = public_version_re.split(version, maxsplit=1) |
| 13 |
if not local: |
| 14 |
return version |
| 15 |
sanitized_local = re.sub("[+~]+", ".", local).strip(".") |
| 16 |
pep440_version = f"{public}+{sanitized_local}" |
| 17 |
assert re.match( |
| 18 |
"^[a-zA-Z0-9.]+$", sanitized_local |
| 19 |
), f"'{pep440_version}' not PEP440 compliant" |
| 20 |
return pep440_version |
| 21 |
|
| 22 |
|
| 23 |
if __name__ == "__main__": |
| 24 |
print(make_pep440_compliant(sys.argv[1])) |