user376403
user376403

Reputation: 1155

Can I use rpm to expand the macros in a specfile?

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?

Upvotes: 26

Views: 15268

Answers (6)

Tomas Tomecek
Tomas Tomecek

Reputation: 6516

We wrote that parser in Python :)

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

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

Upvotes: 1

Erik Osterman
Erik Osterman

Reputation: 579

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"  

Upvotes: 3

Stan
Stan

Reputation: 2599

Since rpm 4.9 you can use:

rpmspec -P <spec_file>

This will print out expanded spec file on stdout

Upvotes: 46

Alan Donnelly
Alan Donnelly

Reputation: 1525

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.

Upvotes: 1

Mark McKinstry
Mark McKinstry

Reputation: 2626

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

Upvotes: 13

m1tk4
m1tk4

Reputation: 3477

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.

Upvotes: -4

Related Questions