我可以使用 rpm 来扩展规范文件中的宏吗?

发布于 2024-09-17 17:31:42 字数 108 浏览 4 评论 0原文

具体的例子是我有很多带有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

微暖i 2024-09-24 17:31:42

从 rpm 4.9 开始,您可以使用:

rpmspec -P <spec_file>

这将在标准输出上打印扩展的规范文件

Since rpm 4.9 you can use:

rpmspec -P <spec_file>

This will print out expanded spec file on stdout

审判长 2024-09-24 17:31:42

如果您只需要解析源代码行,spectool 将为您完成此操作。它是 Fedora rpmdevtools 的一部分。

$ spectool ./mg.spec 
Source0: http://homepage.boetes.org/software/mg/mg-20110120.tar.gz
$ 

这是它的帮助屏幕

Usage: spectool [<options>] <specfile>
Options:
operating mode:
-l, --lf, --list-files        lists the expanded sources/patches (default)
-g, --gf, --get-files         gets the sources/patches that are listed with
                              a URL
-h, --help                    display this help screen

files on which to operate:
-A, --all                     all files, sources and patches (default)
-S, --sources                 all sources
-P, --patches                 all patches
-s, --source x[,y[,...]]      specified sources
-p, --patch a[,b[,...]]       specified patches

misc:
-d, --define 'macro value'    defines RPM macro 'macro' to be 'value'
-C, --directory dir           download into specified directory (default '.')
-R, --sourcedir               download into rpm's %{_sourcedir}
-n, --dryrun, --dry-run       don't download anything, just show what would be
                              done
-D, --debug                   output debug info, don't clean up when done

If its only the source lines you need parsed, spectool will do that for you. It's part of Fedora's rpmdevtools.

$ spectool ./mg.spec 
Source0: http://homepage.boetes.org/software/mg/mg-20110120.tar.gz
$ 

Here's its help screen

Usage: spectool [<options>] <specfile>
Options:
operating mode:
-l, --lf, --list-files        lists the expanded sources/patches (default)
-g, --gf, --get-files         gets the sources/patches that are listed with
                              a URL
-h, --help                    display this help screen

files on which to operate:
-A, --all                     all files, sources and patches (default)
-S, --sources                 all sources
-P, --patches                 all patches
-s, --source x[,y[,...]]      specified sources
-p, --patch a[,b[,...]]       specified patches

misc:
-d, --define 'macro value'    defines RPM macro 'macro' to be 'value'
-C, --directory dir           download into specified directory (default '.')
-R, --sourcedir               download into rpm's %{_sourcedir}
-n, --dryrun, --dry-run       don't download anything, just show what would be
                              done
-D, --debug                   output debug info, don't clean up when done
蝶…霜飞 2024-09-24 17:31:42

如果您查看 @mmckinst 谈论的 rpmdevtools 中的 /usr/bin/spectool 脚本,您会发现这只是一个精心设计的 hack。它创建一个 tmp 规范文件,该文件本质上执行下面脚本的操作。这就是我们用来扩展规范文件,然后 grep 查找我们需要的文件部分的方法。就我们而言,我们想要的不仅仅是源代码和补丁。

下面是模拟此行为的示例 bash 脚本。它将通过 %prep 部分向上扩展所有宏。

#!/bin/bash
spec_file="$1" # pass in the path to the spec file as the first argument
tmp_spec="/tmp/eval-$.spec"
cat "$spec_file" | sed '/^%prep/,$d' > "$tmp_spec"
echo '%prep' >> "$tmp_spec"
echo 'cat<<__EOF__' >> $tmp_spec
cat "$spec_file" | sed '/^%prep/,$d' >> "$tmp_spec"
echo '__EOF__' >> "$tmp_spec"
rpmbuild -bp "$tmp_spec" 2>/dev/null
rm -f "$tmp_spec"  

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.

#!/bin/bash
spec_file="$1" # pass in the path to the spec file as the first argument
tmp_spec="/tmp/eval-$.spec"
cat "$spec_file" | sed '/^%prep/,$d' > "$tmp_spec"
echo '%prep' >> "$tmp_spec"
echo 'cat<<__EOF__' >> $tmp_spec
cat "$spec_file" | sed '/^%prep/,$d' >> "$tmp_spec"
echo '__EOF__' >> "$tmp_spec"
rpmbuild -bp "$tmp_spec" 2>/dev/null
rm -f "$tmp_spec"  
柠北森屋 2024-09-24 17:31:42

扩展脚本中的宏

如果您对宏扩展后 RPM 中的脚本是什么样子感兴趣,您可以构建 RPM,然后让 RPM 提取脚本:

rpmbuild -bi my-package.spec
rpm -qp --scripts my-package.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:

rpmbuild -bi my-package.spec
rpm -qp --scripts my-package.rpm

This works because RPM expands the macros at build-time.

强辩 2024-09-24 17:31:42

我们用 Python 编写了该解析器:)

https://github.com/packit/specfile/

$ python3 -c 'import specfile; print(specfile.Specfile("units.spec").expand("%version"))'
2.21

We wrote that parser in Python :)

https://github.com/packit/specfile/

$ python3 -c 'import specfile; print(specfile.Specfile("units.spec").expand("%version"))'
2.21
柳絮泡泡 2024-09-24 17:31:42

您可以使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文