Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions apport/packaging.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ def get_source(self, package: str) -> str:
"this method must be implemented by a concrete subclass"
)

def get_installed_binaries(self, source_package: str) -> set[str]:
"""Return all installed binary packages for a given source."""
raise NotImplementedError(
"this method must be implemented by a concrete subclass"
)

def get_package_origin(self, package: str) -> str | None:
"""Return package origin.

Expand Down
10 changes: 9 additions & 1 deletion apport/packaging_impl/apt_dpkg.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ def _clear_apt_cache(self) -> None:
self._apt_cache = None
self._sandbox_apt_cache = None

def _cache(self):
def _cache(self) -> apt.Cache:
"""Return apt.Cache() (initialized lazily)."""
if not self._apt_cache:
self._clear_apt_cache()
Expand Down Expand Up @@ -444,6 +444,14 @@ def get_source(self, package: str) -> str:
return self._apt_pkg(package).candidate.source_name
raise ValueError(f"package {package} does not exist")

def get_installed_binaries(self, source_package: str) -> set[str]:
"""Return all installed binary packages for a given source."""
return {
pkg.name
for pkg in self._cache()
if pkg.installed and (source_package == pkg.installed.source_name)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if pkg.installed and (source_package == pkg.installed.source_name)
if pkg.installed and source_package == pkg.installed.source_name

}

def get_package_origin(self, package: str) -> str | None:
"""Return package origin.

Expand Down
29 changes: 20 additions & 9 deletions apport/ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -777,27 +777,38 @@ def run_update_report(self) -> bool:

# list of affected source packages
self.report = apport.Report("Bug")
# FIXME: for now we assume this is a binary package because it's just easier
# and it didn't work with a source package anyway.
if self.args.package:
pkgs = [self.args.package.strip()]
binary = self.args.package.strip()
pkgs = {packaging.get_source(binary): set((binary,))}
else:
pkgs = self.crashdb.get_affected_packages(self.args.update_report)
pkgs = {
src: packaging.get_installed_binaries(src)
for src in self.crashdb.get_affected_packages(self.args.update_report)
}

info_collected = False
for p in pkgs:
assert self.report is not None
for src, binaries in pkgs.items():
# print(f"Collecting apport information for source package {p}...")
self.cur_package = p
self.report["SourcePackage"] = p
self.report["Package"] = p # no way to find this out
assert binaries
self.cur_package = src
self.report["SourcePackage"] = src
self.report["Package"] = src # Slight abuse of the field but it's mandatory
self.report["InstalledBinaries"] = " ".join(sorted(binaries))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We probably also want to collect the version of those packages (similar to what Dependencies does).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was going to answer that we don't need it since it's already known, but actually that's a really good point, that would show mixed-version weirdness.


# we either must have the package installed or a source package
# hook available to collect sensible information
try:
packaging.get_version(p)
packaging.get_version(next(iter(binaries)))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That will only return one of the binary packages and is not stable.

except ValueError:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: Can we still reach that exception?

if not os.path.exists(
os.path.join(apport.report.PACKAGE_HOOK_DIR, f"source_{p}.py")
os.path.join(apport.report.PACKAGE_HOOK_DIR, f"source_{src}.py")
):
print(f"Package {p} not installed and no hook available, ignoring")
print(
f"Package {src} not installed and no hook available, ignoring"
)
continue
self.collect_info(ignore_uninstalled=True)
info_collected = True
Expand Down
11 changes: 11 additions & 0 deletions tests/integration/test_packaging_apt_dpkg.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,17 @@ def test_get_source(self) -> None:
self.assertEqual(impl.get_source("bash"), "bash")
self.assertIn("glibc", impl.get_source("libc6"))

def test_get_installed_binaries_library(self) -> None:
binaries = impl.get_installed_binaries("glibc")
self.assertIn("libc6", binaries)
self.assertNotIn("glibc", binaries)

def test_get_installed_binaries_binary(self) -> None:
self.assertIn("bash", impl.get_installed_binaries("bash"))

def test_get_installed_binaries_none(self) -> None:
self.assertEqual(set(impl.get_installed_binaries("nonexisting")), set())

def test_get_package_origin(self) -> None:
"""get_package_origin()."""
# determine distro name
Expand Down
Loading