在尊重其依赖性的同时安装多个deb文件


13

为了支持多个文件的脱机安装.deb,我正在使用:

sudo dpkg -i dependencies/*.deb

我注意到有些软件包由于安装顺序而无法使用此方法

例如, 这是我安装mariadb-server-5.5及其依赖项时发生的情况:

这些是文件

这是错误

如何*.deb在尊重文件依赖关系的同时安装文件?

  • 我试图避免建立本地存储库,因为它很难维护。
  • 解决方法是,我dpkg -i *.deb两次运行命令。

1
您尝试过GDebi吗?dpkg在依赖管理方面,这比普通的要聪明得多。您可以使用该--apt-line标志模拟结果操作。
David Foerster,2015年

这是一个古老的问题,但希望您可以如何做回社区?我想知道您是否尝试过1)仅运行安装两次(第二遍应该可以吗?)或2)任何其他选择,例如apt-get install -f
pzkpfw

1
@pzkpfw当前仅运行两次安装。计划编写python脚本以按拓扑顺序对依赖项进行排序。我将在部署后立即进行更新
Jossef Harush

如果它没有
坏,

Answers:


3

您可以尝试使用-R并安装选项:

sudo dpkg -R --install dependencies/

-R递归处理在特定目录及其所有子目录中找到的所有匹配模式* .deb的常规文件


-1,这不起作用。与所讨论的情况相同
约瑟夫·哈鲁什

2

拓扑排序(通过脚本)

该命令有问题 -不正确地遵守应安装的订购软件包(即使您提供了所有必需的软件包)。dpkg -i packages/*.deb

自定义脚本来统治他们

假设您的debian发行版已安装python(我的是ubuntu 14.04 LTS并随附python27

除了脱机.deb packages目录,还提供一个脚本;

  • 提取元数据并对所有候选程序包进行拓扑排序
  • 用于dpkg -i以正确的顺序安装已排序的软件包

例如,执行此命令以安装所有预先收集的脱机软件包

sudo python install.py
  • 您的目录结构应如下所示

    在此处输入图片说明


install.py

#!/usr/bin/env python

import os
import re
import subprocess
import logging

import sys

rootLogger = logging.getLogger()
rootLogger.setLevel(logging.INFO)
consoleHandler = logging.StreamHandler(sys.stdout)
consoleHandler.setFormatter(logging.Formatter("%(asctime)s - %(levelname)s - %(message)s"))
rootLogger.addHandler(consoleHandler)

SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))


class TopologicalSort(object):
    def __init__(self, dependency_map):
        self._dependency_map = dependency_map
        self._already_processed = set()

    def _get_dependencies(self, item, root=None):

        if not root:
            root = item

        elif root == item:
            logging.warn("circular dependency detected in '{}'".format(item))
            raise StopIteration()

        dependencies = self._dependency_map.get(item, [])
        for dependency in dependencies:

            if dependency in self._already_processed:
                continue

            self._already_processed.add(dependency)

            for sub_dependency in self._get_dependencies(dependency, root=root):
                yield sub_dependency

            yield dependency

    def sort(self):
        # Reduction, connect all nodes to a dummy node and re-calculate
        special_package_id = 'topological-sort-special-node'
        self._dependency_map[special_package_id] = self._dependency_map.keys()
        sorted_dependencies = self._get_dependencies(special_package_id)
        sorted_dependencies = list(sorted_dependencies)
        del self._dependency_map[special_package_id]

        # Remove "noise" dependencies (only referenced, not declared)
        sorted_dependencies = filter(lambda x: x in self._dependency_map, sorted_dependencies)
        return sorted_dependencies


class DebianPackage(object):
    def __init__(self, file_path):
        metadata = subprocess.check_output('dpkg -I {}'.format(file_path), shell=True)
        metadata = metadata.replace('\n ', '\n')
        self._metadata = metadata
        self.id = self._get('Package')
        self.dependencies = list(self._get_dependencies())
        self.file_path = file_path

    def _get_dependencies(self):
        dependencies = self._get('Depends') + ',' + self._get('Pre-Depends')
        dependencies = re.split(r',|\|', dependencies)
        dependencies = map(lambda x: re.sub(r'\(.*\)|:any', '', x).strip(), dependencies)
        dependencies = filter(lambda x: x, dependencies)
        dependencies = set(dependencies)
        for dependency in dependencies:
            yield dependency

    def _get(self, key):
        search = re.search(r'\n{key}:(.*)\n[A-Z]'.format(key=key), self._metadata)
        return search.group(1).strip() if search else ''


def sort_debian_packages(directory_path):
    file_names = os.listdir(directory_path)
    debian_packages = {}
    dependency_map = {}
    for file_name in file_names:

        file_path = os.path.join(directory_path, file_name)

        if not os.path.isfile(file_path):
            continue

        debian_package = DebianPackage(file_path)
        debian_packages[debian_package.id] = debian_package
        dependency_map[debian_package.id] = debian_package.dependencies

    sorted_dependencies = TopologicalSort(dependency_map).sort()
    sorted_dependencies = map(lambda package_id: debian_packages[package_id].file_path, sorted_dependencies)
    return sorted_dependencies


def main():
    # ------------------
    # Sort the packages using topological sort

    packages_dir_path = os.path.join(SCRIPT_DIR, 'packages')
    logging.info('sorting packages in "{}" using topological sort ...'.format(packages_dir_path))
    sorted_packages = sort_debian_packages(packages_dir_path)

    # ------------------
    # Install the packages in the sorted order

    for index, package_file_path in enumerate(sorted_packages):
        command = 'dpkg -i {}'.format(package_file_path)
        logging.info('executing "{}" ...'.format(command))
        subprocess.check_call(command, shell=True)


if __name__ == '__main__':

    if os.geteuid() != 0:
        logging.error('must be run as root')
        sys.exit(1)

    try:
        main()
    except:
        logging.error('failed to install packages', exc_info=True)
        sys.exit(1)
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.