From 63ce9b4b07964a9369ffe0dcac973944799c852b Mon Sep 17 00:00:00 2001 From: Jonathan Poelen Date: Wed, 8 Nov 2023 14:38:12 +0100 Subject: [PATCH] add add DIST_CODENAME with --load-distribution-infos --- wallix_packager/distroinfo.py | 33 +++++++++++++++++++++++++++++++++ wallix_packager/packager.py | 21 +++++++++++++++++---- 2 files changed, 50 insertions(+), 4 deletions(-) create mode 100644 wallix_packager/distroinfo.py diff --git a/wallix_packager/distroinfo.py b/wallix_packager/distroinfo.py new file mode 100644 index 0000000..23ecfe0 --- /dev/null +++ b/wallix_packager/distroinfo.py @@ -0,0 +1,33 @@ +class DistroInfo: + _id = '' + _name = '' + _version = '' + _codename = '' + + def __init__(self): + with open('/etc/os-release') as f: + s = f.read() + patt = r'(?:^|\n)(NAME|VERSION_CODENAME|ID|VERSION_ID)="?([^\n"]+)' + for m in re.finditer(patt, s): + k = m.group(1) + v = m.group(2) + if k == 'NAME': + self._name = v + elif k == 'ID': + self._id = v + elif k == 'VERSION_ID': + self._version = v + elif k == 'VERSION_CODENAME': + self._codename = v + + def id(self) -> str: + return self._id + + def name(self) -> str: + return self._name + + def version(self) -> str: + return self._version + + def codename(self) -> str: + return self._codename diff --git a/wallix_packager/packager.py b/wallix_packager/packager.py index bf8e243..fad6b16 100644 --- a/wallix_packager/packager.py +++ b/wallix_packager/packager.py @@ -50,25 +50,35 @@ class DistributionInfos(NamedTuple): distribution_id: str distribution_name: str distribution_version: str + distribution_codename: str def distribution_infos(load_infos: bool, distribution_id: Optional[str], distribution_name: Optional[str], - distribution_version: Optional[str]) -> DistributionInfos: + distribution_version: Optional[str], + distribution_codename: Optional[str]) -> DistributionInfos: if load_infos: - import distro + try: + import distro + except ModuleNotFoundError: + from .distroinfo import DistroInfo + distro = DistroInfo() + if distribution_id is None: distribution_id = distro.id() if distribution_name is None: distribution_name = distro.name() if distribution_version is None: distribution_version = distro.version() + if distribution_codename is None: + distribution_codename = distro.codename() return DistributionInfos( distribution_id=distribution_id or '', distribution_name=distribution_name or '', - distribution_version=distribution_version or '' + distribution_version=distribution_version or '', + distribution_codename=distribution_codename or '', ) @@ -311,6 +321,7 @@ def add_arguments_for_show_config_command(parser: argparse.ArgumentParser) -> No parser.add_argument('--distribution-id') parser.add_argument('--distribution-name') parser.add_argument('--distribution-version') + parser.add_argument('--distribution-codename') # py-3.9: action=argparse.BooleanOptionalAction parser.add_argument('--load-distribution-infos', action='store_true') @@ -430,7 +441,8 @@ def make_config(args: argparse.Namespace) -> Dict[str, str]: dist_infos = distribution_infos(load_infos=args.load_distribution_infos, distribution_id=args.distribution_id, distribution_name=args.distribution_name, - distribution_version=args.distribution_version) + distribution_version=args.distribution_version, + distribution_codename=args.distribution_codename) config = dict(filter( lambda t: t[1] is not None, @@ -438,6 +450,7 @@ def make_config(args: argparse.Namespace) -> Dict[str, str]: ('DIST_ID', dist_infos.distribution_id), ('DIST_NAME', dist_infos.distribution_name), ('DIST_VERSION', dist_infos.distribution_version), + ('DIST_CODENAME', dist_infos.distribution_codename), ('PKG_DISTRIBUTION', args.package_distribution), ('PROJECT_VERSION', args.project_version), ('PROJECT_NAME', args.project_name),