# Copyright 2023 Canonical Ltd.## This program is free software; you can redistribute it and/or# modify it under the terms of the GNU Lesser General Public# License version 3 as published by the Free Software Foundation.## This program is distributed in the hope that it will be useful,# but WITHOUT ANY WARRANTY; without even the implied warranty of# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU# Lesser General Public License for more details.## You should have received a copy of the GNU Lesser General Public License# along with this program. If not, see <http://www.gnu.org/licenses/>."""Support for files installed/updated through DNF."""# pylint:disable=duplicate-codeimportloggingimportsubprocessfromcraft_parts.utilsimportos_utilsfrom.importerrorsfrom.yumimportYUMRepositorylogger=logging.getLogger(__name__)
[docs]classDNFRepository(YUMRepository):"""Repository management using DNF."""
[docs]@classmethoddefget_packages_for_source_type(cls,source_type:str)->set[str]:"""Return a list of packages required to work with source_type."""ifsource_type=="bzr":raiseNotImplementedError("bzr version control system is not yet supported on this base.")ifsource_type=="deb":raiseNotImplementedError("Deb files not yet supported on this base.")ifsource_type=="git":packages={"git"}elifsource_type=="tar":packages={"tar"}elifsource_typein["hg","mercurial"]:packages={"mercurial"}elifsource_typein["svn","subversion"]:packages={"subversion"}elifsource_typein["rpm2cpio","rpm"]:packages=set()elifsource_type=="7zip":packages={"p7zip"}else:packages=set()returnpackages
@classmethoddef_install_packages(cls,package_names:list[str])->None:"""Really install the packages."""logger.debug("Installing packages: %s"," ".join(package_names))dnf_command=["dnf","install","-y"]try:process_run(dnf_command+package_names)exceptsubprocess.CalledProcessErroraserr:raiseerrors.BuildPackagesNotInstalled(packages=package_names)fromerr
[docs]defprocess_run(command:list[str])->None:"""Run a command and log its output."""# Pass logger so messages can be logged as originating from this package.os_utils.process_run(command,logger.debug)