歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Python獲取rpm包基本信息[import rpm]

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

日期:2017/3/1 10:49:00   编辑:Linux編程

在進行測試之前,必須先檢查開發的rpm包數據

包括安裝目錄,權限,文件結構,是否包含某些文件等信息

寫了個腳本自動化

python的rpm包,在網上資料實在不多,時間倉促,也就簡單看了下

首先,import

import rpm

獲取hdr

  1. def get_hdr(rpmPath):
  2. ts = rpm.ts()
  3. try:
  4. fdno = os.open(rpmPath,os.O_RDONLY)
  5. hdr = ts.hdrFromFdno(fdno)
  6. os.close(fdno)
  7. except:
  8. print("ERROR: Init rpm error!")
  9. sys.exit(1)
  10. return hdr

接著,可以從hdr獲取到包中的各類信息

可以使用help(rpm)查看已經定義好的變量,變量基本讀下就大概知道什麼意思

主要是RPMTAG_*

以下簡單獲取自己想要的幾項

  1. def get_rpm_info(file_path):
  2. rpm_info = {}
  3. hdr = get_hdr(file_path)
  4. tag_files = [s for s in hdr[rpm.RPMTAG_FILENAMES] if ".svn" not in s]
  5. rpm_info.update({"tag_name":str(hdr[rpm.RPMTAG_NAME])})
  6. rpm_info.update({"tag_version":str(hdr[rpm.RPMTAG_VERSION])})
  7. rpm_info.update({"tag_release":str(hdr[rpm.RPMTAG_R])})
  8. rpm_info.update({"tag_vendor":str(hdr[rpm.RPMTAG_VENDOR])})
  9. rpm_info.update({"tag_desc":str(hdr[rpm.RPMTAG_SUMMARY])})
  10. rpm_info.update({"tag_packager":str(hdr[rpm.RPMTAG_PACKAGER])})
  11. rpm_info.update({"tag_files":sorted(tag_files)})
  12. rpm_info.update({"tag_xml_files":[s for s in hdr[rpm.RPMTAG_FILENAMES] if ".xml" in s]})
  13. rpm_info.update({"tag_install_path":os.path.commonprefix(tag_files)})
  14. return rpm_info

從上到下依次

包名,版本,release,開發者,描述,包所有者,包內文件列表,安裝路徑

Copyright © Linux教程網 All Rights Reserved