forked from systemd/mkosi
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
mkosi: record package list in a json manifest
The output is something like this { "packages": [ { "type": "rpm", "name": "acl", "version": "2.3.1-2.fc35.x86_64" }, { "type": "rpm", "name": "alternatives", "version": "1.19-1.fc35.x86_64" }, { "type": "rpm", "name": "audit-libs", "version": "3.0.3-2.fc35.x86_64" }, ... ] } This is a partial step towards systemd#700, but has other uses too.
- Loading branch information
Showing
2 changed files
with
116 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
# SPDX-License-Identifier: LGPL-2.1+ | ||
|
||
import dataclasses | ||
|
||
import json | ||
from subprocess import DEVNULL, PIPE | ||
from textwrap import dedent | ||
|
||
from typing import cast, Any, Dict, IO, List, Optional | ||
|
||
from .backend import run, CommandLineArguments, PackageType, Distribution | ||
|
||
|
||
@dataclasses.dataclass | ||
class PackageManifest: | ||
"""A description of a package | ||
The fields used here must match | ||
https://systemd.io/COREDUMP_PACKAGE_METADATA/#well-known-keys. | ||
""" | ||
|
||
type: str | ||
name: str | ||
version: str | ||
size: int | ||
|
||
def json(self) -> Dict[str, str]: | ||
return { | ||
"type": self.type, | ||
"name": self.name, | ||
"version": self.version, | ||
} | ||
|
||
|
||
@dataclasses.dataclass | ||
class Manifest: | ||
args: CommandLineArguments | ||
packages: List[PackageManifest] = dataclasses.field(default_factory=list) | ||
|
||
def record_packages(self, root: str) -> None: | ||
if cast(Any, self.args.distribution).package_type == PackageType.rpm: | ||
self.record_rpm_packages(root) | ||
# TODO: add implementations for other package managers | ||
|
||
def record_rpm_packages(self, root: str) -> None: | ||
c = run( | ||
["rpm", f"--root={root}", "-qa", "--qf", r"%{NEVRA}\t%{SOURCERPM}\t%{NAME}\t%{SIZE}\n"], | ||
stdout=PIPE, | ||
stderr=DEVNULL, | ||
universal_newlines=True, | ||
) | ||
|
||
packages = sorted(c.stdout.splitlines()) | ||
|
||
for package in packages: | ||
nevra, srpm, name, size = package.split("\t") | ||
|
||
assert nevra.startswith(f"{name}-") | ||
evra = nevra[len(name) + 1 :] | ||
|
||
size = int(size) | ||
|
||
package = PackageManifest("rpm", name, evra, size) | ||
self.packages.append(package) | ||
|
||
def has_data(self) -> bool: | ||
# We might add more data in the future | ||
return len(self.packages) > 0 | ||
|
||
def json(self) -> Dict[str, Any]: | ||
return { | ||
"packages": [package.json() for package in self.packages], | ||
} | ||
|
||
def write_json(self, out: IO[str]) -> None: | ||
json.dump(self.json(), out, indent=2) |