Skip to content

Commit

Permalink
Fix sanitizing local .deb packages
Browse files Browse the repository at this point in the history
Add multiple local .deb packages, e.g. `/home/user/foo.deb` and
`/home/user/bar.deb`, to the configuration and the resulting YAML file
will only contain the last one due to the assumption that entries with a
`/` are versioned.

Ignore APT patterns when sanitizing the packages list and correctly
parse paths. Assume that .deb packages are names
<name>_<version>_<arch>.deb.

Bug: #7
Signed-off-by: Benjamin Drung <bdrung@posteo.de>
  • Loading branch information
bdrung committed Sep 23, 2023
1 parent 77d1707 commit 1826acd
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 9 deletions.
20 changes: 11 additions & 9 deletions bdebstrap
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import collections
import io
import logging
import os
import pathlib
import re
import shutil
import subprocess
Expand Down Expand Up @@ -661,7 +662,7 @@ class Config(dict):

dict_merge(self, config)

def sanitize_packages(self):
def sanitize_packages(self) -> None:
"""Sanitize packages list by removing duplicates (keeping the latest one)."""
if "mmdebstrap" not in self or "packages" not in self["mmdebstrap"]:
return
Expand All @@ -671,16 +672,17 @@ class Config(dict):
if not package:
# Cover commented out and empty entries
continue
if "=" in package:
name, version = package.split("=", 1)
packages[name] = "=" + version
elif "/" in package:
name, version = package.split("/", 1)
packages[name] = "/" + version
if package[0] in ("?", "!", "~", "("):
# Do not fiddle with APT patterns
packages[package] = package
elif package.startswith("/") or package.startswith("./") or package.startswith("../"):
name = pathlib.Path(package).stem.split("_", 1)[0]
packages[name] = package
else:
packages[package] = ""
name = re.split("[=/]", package, maxsplit=1)[0]
packages[name] = package

self["mmdebstrap"]["packages"] = [k + v for k, v in packages.items()]
self["mmdebstrap"]["packages"] = list(packages.values())

def save(self, config_filename, simulate=False):
"""Save configuration to given config filename."""
Expand Down
27 changes: 27 additions & 0 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,13 +392,40 @@ def test_loading(self):
},
)

def test_sanitize_packages_debs(self) -> None:
"""Test sanitize_packages method: multiple local .debs"""
config = Config()
config["mmdebstrap"] = {"packages": ["/home/user/foo.deb", "/home/user/bar.deb"]}
config.sanitize_packages()
self.assertEqual(
config["mmdebstrap"]["packages"], ["/home/user/foo.deb", "/home/user/bar.deb"]
)

def test_sanitize_packages_duplicate_debs(self) -> None:
"""Test sanitize_packages method: remove duplicate local .debs."""
config = Config()
config["mmdebstrap"] = {
"packages": ["./bdebstrap_0.5_all.deb", "../bdebstrap_0.4_all.deb"]
}
config.sanitize_packages()
self.assertEqual(config["mmdebstrap"]["packages"], ["../bdebstrap_0.4_all.deb"])

def test_sanitize_packages_duplicates(self) -> None:
"""Test sanitize_packages method: remove duplicates."""
config = Config()
config["mmdebstrap"] = {"packages": ["less/jammy-updates", "more", "less=590-1build1"]}
config.sanitize_packages()
self.assertEqual(config["mmdebstrap"]["packages"], ["less=590-1build1", "more"])

def test_sanitize_packages_pattern(self) -> None:
"""Test sanitize_packages method: APT pattern"""
config = Config()
config["mmdebstrap"] = {"packages": ["?priority(required)", "?priority(important)"]}
config.sanitize_packages()
self.assertEqual(
config["mmdebstrap"]["packages"], ["?priority(required)", "?priority(important)"]
)

def test_yaml_rendering(self):
"""Test that config.yaml is formatted correctly."""
config = Config()
Expand Down

0 comments on commit 1826acd

Please sign in to comment.