使用apt_pkg软件包以编程方式安装debian软件包(python-apt)

时间:2018-08-08 05:47:28

标签: debian python package-management apt

我已经构建了一些debian软件包并将其放在主机上。我想将这些软件包安装在python脚本中。因此,我使用apt_pkg编写了安装功能,如下所示:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import apt 
import apt_pkg
import sys

class my_pkg_manager:

    def __init__(self):

        apt_pkg.init()
        self.cache = apt_pkg.Cache()
        self.sources = apt_pkg.SourceList()
        self.pkg_records = apt_pkg.PackageRecords(self.cache)
        self.depcache = apt_pkg.DepCache(self.cache)
        self.pkg_manager = apt_pkg.PackageManager(self.depcache)
        self.fetcher = apt_pkg.Acquire()


    def find_my_package(self):

        for pkg in self.cache.packages:
            if len(pkg.version_list)>0:
                package_site = pkg.version_list[0].file_list[0][0].site
                if package_site == 'my_host_address.com':
            if pkg.name == 'my_pkg_name':
            return pkg
        return None


    def install_package(self, pkg):

        self.depcache.mark_install(pkg)
        self.sources.read_main_list()
        self.pkg_manager.get_archives(self.fetcher, self.sources,
                                      self.pkg_records)
        log_file = open('install_log','w')
        res = self.pkg_manager.do_install(log_file.fileno())

        if res == self.pkg_manager.RESULT_COMPLETED:
            print('result completed!')
        elif res == self.pkg_manager.RESULT_INCOMPLETE:
            print('result incomplete!')
        else:
            print('result failed!')


    def run(self):
        my_pkg = self.find_my_package()
        if my_pkg != None:
            self.install_package(my_pkg)
        else:
            print('Can't find package!') 


if __name__ == '__main__':

    my_package_manager =  my_pkg_manager()
    my_package_manager.run()

如果我使用apt-get install,将安装软件包没有任何问题,但是使用此脚本会发生以下错误:

  

res = self.pkg_manager.do_install(log_file.fileno())apt_pkg。错误:   E:内部错误,要安装的路径名不是绝对的   'myPackage_1.0.0_all.deb'

我已将.deb文件和Packages.gz放置在主机中名为“ debian”的目录中,并将以下行添加到了sources.list中:

deb http://my_host_address.com debian/

我不知道我的脚本有什么问题吗?

0 个答案:

没有答案