我可以使用 rpm 来扩展规范文件中的宏吗?
具体的例子是我有很多带有 Source0
: 或其他包含宏的 Source
行的规范文件。如何在不实际启动规范文件或编写自己的解析器的情况下扩展这些宏?
The concrete example being I have lots of specfiles with Source0
: or other Source
lines containing macros. How can I have these macros expanded without actually starting a build on the specfile or writing my own parser?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
从 rpm 4.9 开始,您可以使用:
这将在标准输出上打印扩展的规范文件
Since rpm 4.9 you can use:
This will print out expanded spec file on stdout
如果您只需要解析源代码行,
spectool
将为您完成此操作。它是 Fedora rpmdevtools 的一部分。这是它的帮助屏幕
If its only the source lines you need parsed,
spectool
will do that for you. It's part of Fedora's rpmdevtools.Here's its help screen
如果您查看 @mmckinst 谈论的 rpmdevtools 中的
/usr/bin/spectool
脚本,您会发现这只是一个精心设计的 hack。它创建一个 tmp 规范文件,该文件本质上执行下面脚本的操作。这就是我们用来扩展规范文件,然后 grep 查找我们需要的文件部分的方法。就我们而言,我们想要的不仅仅是源代码和补丁。下面是模拟此行为的示例 bash 脚本。它将通过
%prep
部分向上扩展所有宏。If you look at the
/usr/bin/spectool
script in rpmdevtools that @mmckinst talks about, you'll see it's just an elaborate hack. It creates a tmp spec file that essentially does what script below does. This is what we're using to expand the spec file and then grep for the parts of the file we need. In our case, we wanted more than the sources and patches.Here's a sample bash script that simulates this behavior. It will expand all macros up through the
%prep
section.扩展脚本中的宏
如果您对宏扩展后 RPM 中的脚本是什么样子感兴趣,您可以构建 RPM,然后让 RPM 提取脚本:
这很有效,因为 RPM 在构建时扩展了宏。
Expand macros in scripts
If you're interested in what the scripts in your RPM look like after macro expansion, you can just build the RPM and then get RPM to extract the scripts:
This works because RPM expands the macros at build-time.
我们用 Python 编写了该解析器:)
https://github.com/packit/specfile/
We wrote that parser in Python :)
https://github.com/packit/specfile/
您可以使用 grep 获取源行,使用 sed 提取包含宏的字符串,然后使用 rpm --eval 'string' 对其进行评估。请注意,这只会扩展全局宏,而不是本规范中定义的宏。
要扩展它们,您可能需要 grep 查找它们并将它们作为自定义宏文件提供给 rpm。
You could grep to get the Source lines, sed to extract the string containing the macro and then rpm --eval 'string' to evaluate it. Note that this will only expand the global macros, not the ones defined in this spec.
To expand those as well you'd probably need to grep for them and feed them to rpm as your custom macros file.