Skip to content

Commit

Permalink
dnf.mark: mark packages in DNF state database
Browse files Browse the repository at this point in the history
This adjustment allows the definition of the mark with the RPMs and runs
DNF after installing the RPMs to put the proper markings in the DNF
state database. See osbuild#455.

This ensures that packages don't get removed during `autoremove` leading
to broken systems.

This stage conditionally selects `dnf5` or `dnf` semantics.
  • Loading branch information
supakeen committed Jul 24, 2023
1 parent a7b75be commit 294244e
Showing 1 changed file with 127 additions and 0 deletions.
127 changes: 127 additions & 0 deletions stages/org.osbuild.dnf.mark
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
#!/usr/bin/python3
"""
Mark packages in the DNF state database.
"""

import itertools
import operator
import shutil
import subprocess
import sys

from osbuild import api

SCHEMA_2 = """
"options": {
"additionalProperties": false,
"properties": {
"packages": {
"type": "array",
"minItems": 1,
"description": "Packages and their marks.",
"items": {
"type": "object",
"additionalProperties": false,
"required": ["name", "mark"],
"properties": {
"name": {
"type": "string",
"description": "Package name."
},
"mark": {
"type": "string",
"enum": ["user", "dependency", "weak", "group"],
"description": "Package mark."
},
"group": {
"type": "string",
"description": "Group to mark package for when `mark` is `group`."
}
}
}
}
}
}
"""


def mark(packages):
dnf_bin = shutil.which("dnf5")

# let's start by finding out which version of dnf we have
version = subprocess.run(
[dnf_bin, "--version"], capture_output=True, encoding="utf8", check=True
).stdout

# now it's important to note that dnf and dnf5 have different version outputs,
# dnf outputs it's version directly and dnf5 has a prefix. we can strip that
# prefix and it'll be a no-op if it isn't there, both have this on the first
# line
version = version.splitlines()[0]
version = version.removeprefix("dnf5 version ")
version, _ = version.split(".", 1)

# if that's a 5, then we have dnf5
is_dnf5 = version == "5"

# now there is an alternative and that is dnf5 installed separately, in which
# case we adjust our path and turn on dnf5
dnf5_bin = shutil.which("dnf5")

if dnf5_bin:
dnf_bin = dnf5_bin
is_dnf5 = True

# group by markings
markings = dict(
itertools.groupby(
sorted(packages, key=operator.itemgetter("name")),
operator.itemgetter("name"),
)
)

if is_dnf5:
mark5(dnf_bin, markings)
else:
mark4(dnf_bin, markings)


def mark4(executable, markings):
subprocess.run(
[executable, "mark", "-y", "install"]
+ [package["name"] for package in markings["user"]],
check=True,
)

# XXX how do i pass the group marking to dnf4?
subprocess.run(
[executable, "mark", "-y", "group"]
+ [package["name"] for package in markings["group"]],
check=True,
)


def mark5(executable, markings):
for mark_as in ["user", "dependency", "weak"]:
subprocess.run(
[executable, "mark", "-y", mark_as]
+ [package["name"] for package in markings[mark]],
check=True,
)

for package in markings["group"]:
subprocess.run(
[executable, "mark", "-y", "group", package["group"], package["name"]],
check=True,
)


def main(_, options):
mark(options["packages"])
return 0


if __name__ == "__main__":
args = api.arguments()
r = main(args["tree"], args["options"])
sys.exit(r)

0 comments on commit 294244e

Please sign in to comment.