refactor(setup.py): Introduce get_debian_version

Move getting the Debian package version into a separate function and
fail in case it cannot find it or fails parsing it.

Signed-off-by: Benjamin Drung <benjamin.drung@canonical.com>
This commit is contained in:
Benjamin Drung 2023-01-30 21:56:37 +01:00
parent a685368ae9
commit 8692bc2b1c

View File

@ -1,12 +1,23 @@
#!/usr/bin/python3
import glob
import os
import pathlib
import re
from setuptools import setup
def get_debian_version() -> str:
"""Look what Debian version we have."""
changelog = pathlib.Path(__file__).parent / "debian" / "changelog"
with changelog.open("r", encoding="utf-8") as changelog_f:
head = changelog_f.readline()
match = re.compile(r".*\((.*)\).*").match(head)
if not match:
raise ValueError(f"Failed to extract Debian version from '{head}'.")
return match.group(1)
def make_pep440_compliant(version: str) -> str:
"""Convert the version into a PEP440 compliant version."""
public_version_re = re.compile(r"^([0-9][0-9.]*(?:(?:a|b|rc|.post|.dev)[0-9]+)*)\+?")
@ -19,14 +30,6 @@ def make_pep440_compliant(version: str) -> str:
return pep440_version
# look/set what version we have
changelog = "debian/changelog"
if os.path.exists(changelog):
head = open(changelog, "r", encoding="utf-8").readline()
match = re.compile(r".*\((.*)\).*").match(head)
if match:
version = match.group(1)
scripts = [
"backportpackage",
"bitesize",
@ -81,7 +84,7 @@ data_files = [
if __name__ == "__main__":
setup(
name="ubuntu-dev-tools",
version=make_pep440_compliant(version),
version=make_pep440_compliant(get_debian_version()),
scripts=scripts,
packages=[
"ubuntutools",