Skip to content

Commit

Permalink
Split $releasever to $releasever_major and $releasever_minor
Browse files Browse the repository at this point in the history
Whenever the `releasever` substitution variable is set, automatically
derive and set the `releasever_major` and `releasever_minor` vars by
splitting `releasever` on the first ".".

Companion to the DNF 5 implementation here: rpm-software-management/dnf5#800

DNF 5 issue: rpm-software-management/dnf5#710

For https://bugzilla.redhat.com/show_bug.cgi?id=1789346
  • Loading branch information
evan-goode authored and Conan-Kudo committed Oct 11, 2023
1 parent 9e73055 commit 2c1a388
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
31 changes: 31 additions & 0 deletions dnf/conf/substitutions.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@
import re

from dnf.i18n import _
from dnf.exceptions import ReadOnlyVariableError

ENVIRONMENT_VARS_RE = re.compile(r'^DNF_VAR_[A-Za-z0-9_]+$')
READ_ONLY_VARIABLES = frozenset(("releasever_major", "releasever_minor"))
logger = logging.getLogger('dnf')


Expand All @@ -43,6 +45,35 @@ def _update_from_env(self):
elif key in numericvars:
self[key] = val

@staticmethod
def _split_releasever(releasever):
# type: (str) -> tuple[str, str]
pos = releasever.find(".")
if pos == -1:
releasever_major = releasever
releasever_minor = ""
else:
releasever_major = releasever[:pos]
releasever_minor = releasever[pos + 1:]
return releasever_major, releasever_minor

def __setitem__(self, key, value):
if Substitutions.is_read_only(key):
raise ReadOnlyVariableError(f"Variable \"{key}\" is read-only", variable_name=key)

setitem = super(Substitutions, self).__setitem__
setitem(key, value)

if key == "releasever" and value:
releasever_major, releasever_minor = Substitutions._split_releasever(value)
setitem("releasever_major", releasever_major)
setitem("releasever_minor", releasever_minor)

@staticmethod
def is_read_only(key):
# type: (str) -> bool
return key in READ_ONLY_VARIABLES

def update_from_etc(self, installroot, varsdir=("/etc/yum/vars/", "/etc/dnf/vars/")):
# :api

Expand Down
6 changes: 6 additions & 0 deletions dnf/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,12 @@ def __reduce__(self):
return (ProcessLockError, (self.value, self.pid))


class ReadOnlyVariableError(Error):
def __init__(self, value, variable_name):
super(ReadOnlyVariableError, self).__init__(value)
self.variable_name = variable_name


class RepoError(Error):
# :api
pass
Expand Down
32 changes: 32 additions & 0 deletions tests/conf/test_substitutions.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import os

import dnf.conf
from dnf.conf.substitutions import Substitutions
from dnf.exceptions import ReadOnlyVariableError

import tests.support

Expand Down Expand Up @@ -52,3 +54,33 @@ def test_named(self):
conf.substitutions.keys(),
['basearch', 'arch', 'GENRE', 'EMPTY'])
self.assertEqual('opera', conf.substitutions['GENRE'])


class SubstitutionsReadOnlyTest(tests.support.TestCase):
def test_set_readonly(self):
conf = dnf.conf.Conf()
variable_name = "releasever_major"
self.assertTrue(Substitutions.is_read_only(variable_name))
with self.assertRaises(ReadOnlyVariableError) as cm:
conf.substitutions["releasever_major"] = "1"
self.assertEqual(cm.exception.variable_name, "releasever_major")


class SubstitutionsReleaseverTest(tests.support.TestCase):
def test_releasever_simple(self):
conf = dnf.conf.Conf()
conf.substitutions["releasever"] = "1.23"
self.assertEqual(conf.substitutions["releasever_major"], "1")
self.assertEqual(conf.substitutions["releasever_minor"], "23")

def test_releasever_major_only(self):
conf = dnf.conf.Conf()
conf.substitutions["releasever"] = "123"
self.assertEqual(conf.substitutions["releasever_major"], "123")
self.assertEqual(conf.substitutions["releasever_minor"], "")

def test_releasever_multipart(self):
conf = dnf.conf.Conf()
conf.substitutions["releasever"] = "1.23.45"
self.assertEqual(conf.substitutions["releasever_major"], "1")
self.assertEqual(conf.substitutions["releasever_minor"], "23.45")

0 comments on commit 2c1a388

Please sign in to comment.