Expanding RPM's Build-in Macro Values.

While looking into Drupal directory structures in Fedora, I stumbled upon this file, /usr/lib/rpm/macros.d/macros.drupal7, which seems to be the configuration file for RPM command.
$ cat /usr/lib/rpm/macros.d/macros.drupal7 
%drupal7            %{_datadir}/drupal7
%drupal7_modules    %{drupal7}/modules
%drupal7_themes     %{drupal7}/themes
%drupal7_libraries  %{_sysconfdir}/drupal7/all/libraries

# No-op macro to allow spec compatibility with RPM < 4.9 (no fileattrs)
%drupal7_find_provides_and_requires %{nil}

Let's try to expand the values of the above built-in RPM's macros.
$ cat /usr/lib/rpm/macros.d/macros.drupal7  | awk '{print $1}' | grep ^% | xargs -I % sh -c 'echo -en "%\t"; rpm --eval %' | column -t
%drupal7                             /usr/share/drupal7
%drupal7_modules                     /usr/share/drupal7/modules
%drupal7_themes                      /usr/share/drupal7/themes
%drupal7_libraries                   /etc/drupal7/all/libraries
%drupal7_find_provides_and_requires

Why not create a Bash function for the above command instead? Put this in your $HOME/.bashrc file.
# expanding the value of the rpm's built-in macros.
function rpm_macro() {
    if [[ -z "$1" ]]; then
        echo "No filename supplied"
    else
        cat $1 | awk '{print $1}' | grep ^% |\
        xargs -I % sh -c 'echo -en "%\t"; rpm --eval %' | column -t  
    fi  
}

Try out our newly created bash function.
$ rpm_macro macros.drupal7 
%drupal7                             /usr/share/drupal7
%drupal7_modules                     /usr/share/drupal7/modules
%drupal7_themes                      /usr/share/drupal7/themes
%drupal7_libraries                   /etc/drupal7/all/libraries
%drupal7_find_provides_and_requires

No comments:

Post a Comment