Python获取rpm包基本信息[import rpm]

在进行测试之前,必须先检查开发的rpm包数据

包括安装目录,权限,文件结构,是否包含某些文件等信息

写了个脚本自动化


python的rpm包,在网上资料实在不多,时间仓促,也就简单看了下


首先,import

import rpm

获取hdr

def get_hdr(rpmPath):
    ts = rpm.ts()
    try:
      fdno = os.open(rpmPath,os.O_RDONLY)
      hdr = ts.hdrFromFdno(fdno)
      os.close(fdno)
    except:
      print("ERROR: Init rpm error!")
      sys.exit(1)
    return hdr 


接着,可以从hdr获取到包中的各类信息

可以使用help(rpm)查看已经定义好的变量,变量基本读下就大概知道什么意思

主要是RPMTAG_*
以下简单获取自己想要的几项

def get_rpm_info(file_path):
    rpm_info = {}
    hdr = get_hdr(file_path)
    tag_files = [s for s in hdr[rpm.RPMTAG_FILENAMES] if ".svn" not in s]

    rpm_info.update({"tag_name":str(hdr[rpm.RPMTAG_NAME])})
    rpm_info.update({"tag_version":str(hdr[rpm.RPMTAG_VERSION])})
    rpm_info.update({"tag_release":str(hdr[rpm.RPMTAG_R])})
    rpm_info.update({"tag_vendor":str(hdr[rpm.RPMTAG_VENDOR])})
    rpm_info.update({"tag_desc":str(hdr[rpm.RPMTAG_SUMMARY])})
    rpm_info.update({"tag_packager":str(hdr[rpm.RPMTAG_PACKAGER])})
    rpm_info.update({"tag_files":sorted(tag_files)})
    rpm_info.update({"tag_xml_files":[s for s in hdr[rpm.RPMTAG_FILENAMES] if ".xml" in s]})
    rpm_info.update({"tag_install_path":os.path.commonprefix(tag_files)})

    return rpm_info

从上到下依次

包名,版本,release,开发者,描述,包所有者,包内文件列表,安装路径



Meet so Meet. C plusplus I-PLUS....
原文地址:https://www.cnblogs.com/iplus/p/4464674.html