参考链接:python-rpm-spec · PyPI

安装模块

先使用 pip 安装 python-rpm-spec 模块

pip3 install python-rpm-spec

使用示例

test.spec

Name:           pandas
Version:        0.25.3
Release:        2
Summary:        Data structures and data analysis tools for Python
License:        BSD
URL:            https://pandas.pydata.org/
Source0:        https://github.com/pandas-dev/pandas/releases/download/v%{version}/%{pypi_name}-%{version}.tar.gzBuildRequires:  gcc gcc-c++ gdb-headless
BuildRequires:  python3-devel python3-setuptools python3-numpy
%if %{with test}
BuildRequires:  python3-pytest >= 4.0.2
%endifRequires: python3-numpy python3-dateutil python3-pytz
Conflicts: conflicts-testProvides: provides-test
Obsoletes: obsoletes-test

test.py

# pip3 install python-rpm-spec
from pyrpm.spec import Specspec_path = 'test.spec'
spec = Spec.from_file(spec_path)print('Name:'.ljust(20), spec.name)
print('Version:'.ljust(20), spec.version)
print('Release:'.ljust(20), spec.release)
print('Summary:'.ljust(20), spec.summary)
print('License:'.ljust(20), spec.license)
print('URL:'.ljust(20), spec.url)
print('Source0:'.ljust(20), spec.sources[0])
print('Requires:'.ljust(20), spec.requires)
print('BuildRequires:'.ljust(20), spec.build_requires)
print('Provides:'.ljust(20), spec.provides)
print('Conflicts:'.ljust(20), spec.conflicts)
print('Obsoletes:'.ljust(20), spec.obsoletes)
print('SourceList:'.ljust(20), spec.sources)
print('SourceDict:'.ljust(20), spec.sources_dict)# build_requires = []
# for br in spec.build_requires:
#     build_requires.append(f'{br.name} {br.operator} {br.version}' if br.version else f'{br.name}')
# print('BuildRequires:'.ljust(20), build_requires)

result

Name:                pandas
Version:             0.25.3
Release:             2
Summary:             Data structures and data analysis tools for Python
License:             BSD
URL:                 https://pandas.pydata.org/
Source0:             https://github.com/pandas-dev/pandas/releases/download/v%{version}/%{pypi_name}-%{version}.tar.gz
Requires:            [python3-numpy, python3-dateutil, python3-pytz]
BuildRequires:       [gcc, gcc-c++, gdb-headless, python3-devel, python3-setuptools, python3-numpy, python3-pytest >= 4.0.2]
Provides:            [provides-test]
Conflicts:           [conflicts-test]
Obsoletes:           [obsoletes-test]
SourceList:          ['https://github.com/pandas-dev/pandas/releases/download/v%{version}/%{pypi_name}-%{version}.tar.gz']
SourceDict:          {'Source0': 'https://github.com/pandas-dev/pandas/releases/download/v%{version}/%{pypi_name}-%{version}.tar.gz'}

其他字段

当然,如果你好奇心更严重一点,或许可以从 spec.py 的源码文件看出一些端倪,因为它其实也是使用的正则匹配去获取 spec 文件对应的这些内容的,只不过通过封装添加了一些额外的处理,下面展示了一些其他可能会进行正则匹配的字段。

_tags = [_NameValue("name", re_tag_compile(r"^Name\s*:\s*(\S+)")),_NameValue("version", re_tag_compile(r"^Version\s*:\s*(\S+)")),_NameValue("epoch", re_tag_compile(r"^Epoch\s*:\s*(\S+)")),_NameValue("release", re_tag_compile(r"^Release\s*:\s*(\S+)")),_NameValue("summary", re_tag_compile(r"^Summary\s*:\s*(.+)")),_NameValue("description", re_tag_compile(r"^%description\s*(\S*)")),_NameValue("changelog", re_tag_compile(r"^%changelog\s*(\S*)")),_NameValue("license", re_tag_compile(r"^License\s*:\s*(.+)")),_NameValue("group", re_tag_compile(r"^Group\s*:\s*(.+)")),_NameValue("url", re_tag_compile(r"^URL\s*:\s*(\S+)")),_NameValue("buildroot", re_tag_compile(r"^BuildRoot\s*:\s*(\S+)")),_SplitValue("buildarch", re_tag_compile(r"^BuildArch\s*:\s*(\S+)")),_SplitValue("excludearch", re_tag_compile(r"^ExcludeArch\s*:\s*(.+)")),_SplitValue("exclusivearch", re_tag_compile(r"^ExclusiveArch\s*:\s*(.+)")),_ListAndDict("sources", re_tag_compile(r"^(Source\d*\s*):\s*(.+)")),_ListAndDict("patches", re_tag_compile(r"^(Patch\d*\s*):\s*(\S+)")),_List("build_requires", re_tag_compile(r"^BuildRequires\s*:\s*(.+)")),_List("requires", re_tag_compile(r"^Requires\s*:\s*(.+)")),_List("conflicts", re_tag_compile(r"^Conflicts\s*:\s*(.+)")),_List("obsoletes", re_tag_compile(r"^Obsoletes\s*:\s*(.+)")),_List("provides", re_tag_compile(r"^Provides\s*:\s*(.+)")),_List("packages", re.compile(r"^%package\s+(\S+)")),_MacroDef("define", re.compile(r"^%define\s+(\S+)\s+(\S+)")),_MacroDef("global", re.compile(r"^%global\s+(\S+)\s+(\S+)")),_DummyMacroDef("dummy", re.compile(r"^%[a-z_]+\b.*$")),
]

相似命令

当然,rpmspec 命令也可以从 spec 提取一部分信息出来。

[root@master ~]# rpmspec --help
Usage: rpmspec [OPTION...]Spec options:-P, --parse                   parse spec file(s) to stdout-q, --query                   query spec file(s)--rpms                        operate on binary rpms generated by spec (default)--srpm                        operate on source rpm generated by spec--target=STRING               override target platform--queryformat=QUERYFORMAT     use the following query formatCommon options for all rpm modes and executables:-D, --define='MACRO EXPR'     define MACRO with value EXPR--undefine=MACRO              undefine MACRO-E, --eval='EXPR'             print macro expansion of EXPR--macros=<FILE:...>           read <FILE:...> instead of default file(s)--noplugins                   don't enable any plugins--nodigest                    don't verify package digest(s)--nosignature                 don't verify package signature(s)--rcfile=<FILE:...>           read <FILE:...> instead of default file(s)-r, --root=ROOT               use ROOT as top level directory (default: "/")--dbpath=DIRECTORY            use database in DIRECTORY--querytags                   display known query tags--showrc                      display final rpmrc and macro configuration--quiet                       provide less detailed output-v, --verbose                 provide more detailed output--version                     print the version of rpm being usedOptions implemented via popt alias/exec:--conflicts                   list capabilities this package conflicts with--obsoletes                   list other packages removed by installing this package--provides                    list capabilities that this package provides--requires                    list capabilities required by package(s)--buildconflicts              list capabilities conflicting with build of this package--buildrequires               list capabilities required to build this packageHelp options:-?, --help                    Show this help message--usage                       Display brief usage message

Python 解析 spec 文件相关推荐

  1. 【Android 逆向】使用 Python 解析 ELF 文件 ( Capstone 反汇编 ELF 文件中的机器码数据 | 完整代码示例 ) ★★★

    文章目录 一.完整代码示例 二.执行结果 三.博客资源 一.完整代码示例 使用 Python 解析 ELF 文件完整代码示例 : # coding=utf-8 # 解析 elf 文件需要导入的依赖库 ...

  2. [系统安全] 四十一.APT系列(6)Python解析PE文件并获取时间戳判断来源区域

    您可能之前看到过我写的类似文章,为什么还要重复撰写呢?只是想更好地帮助初学者了解病毒逆向分析和系统安全,更加成体系且不破坏之前的系列.因此,我重新开设了这个专栏,准备系统整理和深入学习系统安全.逆向分 ...

  3. Python解析json文件

    Python解析json文件 实现代码 import json import sysstdout = sys.stdoutwith open("company.json", &qu ...

  4. python解析xml文件最好选用的模块_用Python解析XML文件

    本文翻译自:https://developer.yahoo.com/python/python-xml.html 使用Python解析XML文件 许多YDN APIs提供了JSON格式的数据输出,JS ...

  5. python解析dat文件生成xlsx文件

    系列文章目录 提示:这里可以添加系列文章的所有文章的目录,目录需要自己手动添加 python解析dat文件生成xlsx文件 提示:写完文章后,目录可以自动生成,如何生成可参考右边的帮助文档 文章目录 ...

  6. 【Android 逆向】使用 Python 解析 ELF 文件 ( Capstone 反汇编框架 | PyCharm 中导入 Capstone 反汇编框架 )

    文章目录 一.Capstone 反汇编框架 二.PyCharm 中导入 Capstone 反汇编框架 一.Capstone 反汇编框架 Android 的 APK 安装文件中 , 可能存在若干 so ...

  7. python读取xml标注坐标_遍历文件 创建XML对象 方法 python解析XML文件 提取坐标计存入文件...

    XML文件??? xml即可扩展标记语言,它可以用来标记数据.定义数据类型,是一种允许用户对自己的标记语言进行定义的源语言. 里面的标签都是可以随心所欲的按照他的命名规则来定义的,文件名为roi.xm ...

  8. python解析log文件_python解析基于xml格式的日志文件

    大家中午好,由于过年一直还没回到状态,好久没分享一波小知识了,今天,继续给大家分享一波Python解析日志的小脚本. 首先,同样的先看看日志是个啥样. 都是xml格式的,是不是看着就头晕了??没事,我 ...

  9. python 解析xml 文件: SAX方式

    环境 python:3.4.4 准备xml文件 首先新建一个xml文件,countries.xml.内容是在python官网上看到的. <?xml version="1.0" ...

  10. python解析xml文件选用模块_Python标准库系列之xml模块

    Python's interfaces for processing XML are grouped in the xml package. 带分隔符的文件仅有两维的数据:行和列.如果你想在程序之间交 ...

最新文章

  1. eclipse 中断言使用的设定
  2. 【迈克尔・乔丹:人工智能,革命远未发生】
  3. linux卸载rpm包
  4. DOS命令行中用MAVEN构建Java和Java Web项目
  5. java线程学习之notify方法和notifyAll方法
  6. 【分享预告】细数GAN和图像分类的前世今生
  7. js ajax java传参_ajax参数传递与后台接收
  8. UE3客户端服务器GamePlay框架
  9. binarysearch java,java数据结构之二分查找法 binarySearch的实例
  10. boolean类型_10、typescript的高级类型
  11. python和java的区别-三分钟看懂Python和Java的区别
  12. 后缀数组模板 hdu1403
  13. MySQL基础一些概念一些用法
  14. url在html中的作用,所谓的URL到底是什么意思,URL有什么作用
  15. C语言中简单冒泡排序不花里胡哨
  16. Linux无线网连接解决方案
  17. ad域时间源配置_Windows Server 2008 R2 域控制器的时间同步配置
  18. python pdf 加水印_Python中通过PyPDF2实现PDF添加水印
  19. java生成二维码扫描跳转到指定的路径URL
  20. Visual Studio Community 2019 安装

热门文章

  1. HDU5442 Favorite Donut(KMP+最大表示法)
  2. 【论文推荐】了解《目标跟踪》必看的6篇论文(附打包下载地址)
  3. NES APU Replayer
  4. Mysql 快速生成日期时间维度表
  5. finecms aip.php漏洞,FineCMS漏洞挖掘
  6. 怎么样关掉红米note开发者选项
  7. 云服务器怎么连,云主机连接操作步骤是怎样的?
  8. C++初学必练基础题【第三期】
  9. 最新FFmpeg RTSP流抓取
  10. 129页4万字某智慧能源集团数字化管控平台项目 建设方案