Skip to content

Commit d8dd472

Browse files
fmoessbauerUrist-McGit
authored andcommitted
chore(apt): issue warning on missing decompression tool
The apt cache data can be compressed. To work with the decompressed data, decompression tooling is needed. Instead of silently ignoring the case where a utility is missing, we now issue a warning. This informs the user about the missing tool and potential implications. Closes: #150 Signed-off-by: Felix Moessbauer <felix.moessbauer@siemens.com>
1 parent d90a2e9 commit d8dd472

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

src/debsbom/apt/cache.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@
1010
import logging
1111
from pathlib import Path
1212

13-
from ..util.compression import find_compressed_file_variants, stream_compressed_file
13+
from ..util.compression import (
14+
CompressionToolMissing,
15+
find_compressed_file_variants,
16+
stream_compressed_file,
17+
)
1418
from ..dpkg.package import BinaryPackage, SourcePackage
1519
from .. import HAS_PYTHON_APT
1620

@@ -169,6 +173,8 @@ def _parse_sources(
169173
# pass the filename of a compressed file when using apt_pkg
170174
sources_raw = Packages.iter_paragraphs(content, use_apt_pkg=False)
171175
yield from Repository._make_srcpkgs(sources_raw, srcpkg_filter)
176+
except CompressionToolMissing as e:
177+
logger.warning(f'{e}: skipping path "{compressed_variant}"')
172178
except (FileNotFoundError, IndexError, RuntimeError):
173179
logger.debug(f"Missing apt cache sources: {sources_file}")
174180

@@ -191,6 +197,8 @@ def _parse_packages(
191197
packages_raw = Packages.iter_paragraphs(content, use_apt_pkg=False)
192198
logger.debug(f"Parsing apt cache binary packages: {packages_file}")
193199
yield from Repository._make_binpkgs(packages_raw, binpkg_filter)
200+
except CompressionToolMissing as e:
201+
logger.warning(f'{e}: skipping path "{compressed_variant}"')
194202
except (FileNotFoundError, IndexError, RuntimeError):
195203
logger.debug(f"Missing apt cache packages: {packages_file}")
196204

src/debsbom/util/compression.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,19 @@
55
from collections import namedtuple
66
from collections.abc import Iterable
77
from pathlib import Path
8+
import shutil
89
import subprocess
910

1011

12+
class CompressionToolMissing(RuntimeError):
13+
"""
14+
Error indicating that a needed compression / decompression tool is missing.
15+
"""
16+
17+
def __init__(self, tool):
18+
super().__init__(f'Compression tool "{tool}" missing')
19+
20+
1121
class Compression:
1222

1323
Format = namedtuple("compression", "tool compress extract fileext")
@@ -53,11 +63,14 @@ def stream_compressed_file(path: Path) -> Iterable[str]:
5363
"""Streams the decompressed content of a compressed file."""
5464
try:
5565
comp = Compression.from_ext(path.suffix)
66+
tool = shutil.which(comp.tool)
67+
if not tool:
68+
raise CompressionToolMissing(comp.tool)
5669
except ValueError:
5770
comp = Compression.NONE
5871

5972
compressor = subprocess.Popen(
60-
[comp.tool] + comp.extract + [path],
73+
[tool] + comp.extract + [path],
6174
stdout=subprocess.PIPE,
6275
text=True,
6376
)

0 commit comments

Comments
 (0)