1

我已经构建了一些 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.Error: E:Internal Error, Pathname to install is not absolute 'myPackage_1.0.0_all.deb'

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

deb http://my_host_address.com debian/

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

4

0 回答 0