From 8692bc2b1c152b13136a6e25f71597f2605c1f4a Mon Sep 17 00:00:00 2001 From: Benjamin Drung Date: Mon, 30 Jan 2023 21:56:37 +0100 Subject: [PATCH] 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 --- setup.py | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/setup.py b/setup.py index 4e4fb98..22d4451 100755 --- a/setup.py +++ b/setup.py @@ -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",