我可以使用 rpm 来扩展 specfile 中的宏吗?

问题描述 投票:0回答:6

具体的例子是我有很多带有

Source0
: 或其他包含宏的
Source
行的规范文件。我如何在不实际开始构建规范文件或编写自己的解析器的情况下扩展这些宏?

rpm rpm-spec
6个回答
38
投票

自 rpm 4.9 起您可以使用:

rpmspec -P <spec_file>

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


12
投票

如果它只是您需要解析的源代码行,

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

2
投票

如果您查看 @mmckinst 谈论的 rpmdevtools 中的

/usr/bin/spectool
脚本,您会发现它只是一个精心设计的 hack。它创建一个 tmp spec 文件,基本上执行下面脚本的操作。这就是我们用来扩展规范文件然后 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"  

1
投票

在脚本中扩展宏

如果你对 RPM 中的脚本在宏扩展后的样子感兴趣,你可以只构建 RPM 然后获取 RPM 来提取脚本:

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

之所以可行,是因为 RPM 在构建时扩展了宏。


0
投票

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

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

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

-4
投票

您可以 grep 获取源代码行,sed 提取包含宏的字符串,然后 rpm --eval 'string' 对其求值。请注意,这只会扩展全局宏,而不是本规范中定义的宏。

要扩展它们,您可能需要为它们进行 grep 并将它们作为自定义宏文件提供给 rpm。

© www.soinside.com 2019 - 2024. All rights reserved.