Source code for reemote.operations.builtin.dpkg_selection
# Copyright (c) 2025 Kim Jarvis TPF Software Services S.A. kim.jarvis@tpfsystems.com # This software is licensed under the MIT License. See the LICENSE file for details.#fromreemote.commandimportCommand
[docs]classDpkgSelections:""" A class to manage dpkg package selection states using `dpkg --get-selections` and `dpkg --set-selections`. This class allows setting the selection state of a Debian package without installing/removing it. It supports standard dpkg selection states such as 'install', 'hold', 'deinstall', and 'purge'. Attributes: name (str): The name of the package. selection (str): The desired selection state for the package. Choices: 'install', 'hold', 'deinstall', 'purge'. guard (bool): If `False`, the command will not be executed. Examples: .. code:: python # Hold a package to prevent upgrades r = yield DpkgSelections(name="python", selection="hold") print(r.cp.stdout) # Allow a package to be upgraded again r = yield DpkgSelections(name="python", selection="install") print(r.cp.stdout) Notes: - This module only changes the selection state; it does not install or remove packages. Use the apt module for actual package management. - Only works on Debian-based systems where dpkg is available. """VALID_SELECTIONS={"install","hold","deinstall","purge"}def__init__(self,name:str,selection:str,guard:bool=True):ifselectionnotinself.VALID_SELECTIONS:raiseValueError(f"Invalid selection '{selection}'. Must be one of {self.VALID_SELECTIONS}.")self.name=nameself.selection=selectionself.guard=guarddef__repr__(self):return(f"DpkgSelections(name={self.name!r}, "f"selection={self.selection!r}, "f"guard={self.guard!r})")defexecute(self):# Construct the command to set the dpkg selectioncmd_str=f"echo '{self.name}{self.selection}' | dpkg --set-selections"r=yieldCommand(cmd_str,guard=self.guard)r.changed=True