Skip to content

Commit

Permalink
dnf(4|5).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.

Two stages are provided, one for dnf-3 and one for dnf5.
  • Loading branch information
supakeen committed Aug 3, 2023
1 parent 14d3163 commit fa11b6d
Show file tree
Hide file tree
Showing 2 changed files with 155 additions and 0 deletions.
74 changes: 74 additions & 0 deletions stages/org.osbuild.dnf4.mark
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#!/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("dnf-3")

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

subprocess.run(
[dnf_bin, "mark", "-y", "install"]
+ [package["name"] for package in markings["user"]],
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)
81 changes: 81 additions & 0 deletions stages/org.osbuild.dnf5.mark
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#!/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")

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

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

for package in markings["group"]:
subprocess.run(
[dnf_bin, "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 fa11b6d

Please sign in to comment.