Packaging guidelines/ParsingSpecFiles

Public service announcement for all tooling authors: If you are still parsing .spec files by hand via grep, sed, cut and friends, you are doing it wrong. Use  instead. It's the same tool used during building, so you can be sure it covers every special case of RPM .spec syntax (and macro expansion!) that you will never get right in your shell script, anyway.

Usage examples
Wrong:

grep -i BuildRequires "$spec"

Right:

rpmspec --query --buildrequires "$spec"

Wrong(!):

grep Name: "$SPEC" | cut -b6- | tr -d " "

Right:

rpmspec --query --srpm --queryformat="%{name}" $spec

Wrong(!!):

grep -i "^${1}:" $spec | sed -e 's/^.*:\s*//g' -e 's/\s*$//g'

Right:

rpmspec --query --srpm --queryformat="%{$1}" $spec

Wrong, wrong, wrong(!):

grep --color=never BuildRequires "$SPEC" | sed -e '/^#.*$/d' | gawk -F: '{ print $2 }' | tr ',' ' ' | xargs

Right:

rpmspec --query --buildrequires "$spec"

Use the right tools for the job
There's no excuse not to use "rpmspec" - it's installed by default (with rpm) on all systems where you want to build/install RPM packages, and in Debian/Ubuntu, it's just an "apt-get install rpm" away.

nemo@mersdk:~$ rpmspec RPM version 4.11.0.1 Copyright (C) 1998-2002 - Red Hat, Inc. This program may be freely redistributed under the terms of the GNU GPL

Usage: rpmspec [-v?] [-P|--parse] [-q|--query] [--rpms] [--srpm] [--target=STRING] [--queryformat=QUERYFORMAT] [-D|--define='MACRO EXPR'] [--undefine=MACRO] [-E|--eval='EXPR'] [--nodigest] [--nosignature] [-r|--root=ROOT] [--dbpath=DIRECTORY] [--querytags] [--showrc] [--quiet] [-v|--verbose] [--version] [-?|--help] [--usage] [--conflicts] [--obsoletes] [--provides] [--requires] [--buildconflicts] [--buildrequires]