]> git.pld-linux.org Git - packages/rpm-build-tools.git/blob - pearize.sh
- ensure rpm macros present
[packages/rpm-build-tools.git] / pearize.sh
1 #!/bin/sh
2 # updates php-pear .spec with Requires/Conflicts lines.
3 # the items are added randomly to the preamble, but once added their order is left intact.
4 # it is still better than nothing. if somebody wishes to add sorting in this
5 # script. i'd be just glad :)
6 #
7 # needs 'pear' for 'pear make-rpm-spec' command, ./builder for fetching sources.
8 # You should have all PEAR packages installed to get best results (needed for epoch autodetection)
9 # So far there are 3 packages with epoch > 0:
10 # $ grep ^Epoch:.* php-pear-*.spec | grep -v 'Epoch:.*0'
11 # php-pear-MDB2.spec:Epoch:               1
12 # php-pear-MDB.spec:Epoch:                1
13 # php-pear-PEAR.spec:Epoch:               1
14 #
15 # To create completely new PEAR package spec, follow something like this:
16 # $ pear download RDF-alpha
17 # File RDF-0.1.0alpha1.tgz downloaded
18 # $ pear make-rpm-spec RDF-0.1.0alpha1.tgz
19 # Wrote RPM spec file php-pear-RDF.spec
20 # $
21 #
22 # TODO: adjust similiarily noautoreqdeps
23 # BUGS: the beta portions in version deps could be wrong (php-4.3.0b1 and alike)
24 # see php-pear-DBA_Relational.spec
25 # Something strange: Requires:  php-common < 4:3:5.1
26 #
27 # NOTE: old version of this script which was used to convert to new package format is in CVS branch MIGRATE.
28 #
29 # Send blames and beer\b\b\b\bideas to glen@pld-linux.org
30
31 set -e
32 spec="$1"
33 if [ -z "$spec" ]; then
34         echo >&2 "Usage: $0 SPECFILE"
35         exit 0
36 fi
37 if [ ! -f "$spec" ]; then
38         echo >&2 "$spec doesn't exist?"
39         exit 1
40 fi
41
42 if [[ "$(rpm -q php-pear-PEAR_Command_Packaging)" == *is?not* ]]; then
43         echo >&2 "Please install php-pear-PEAR_Command_Packaging"
44         exit 1
45 fi
46 echo "Processing $spec"
47
48 getsource() {
49         local spec="$1"
50         local NR="$2"
51         rpmbuild --nodigest --nosignature -bp --define 'prep %dump' $spec 2>&1 | awk  "/SOURCE$NR\t/ {print \$3}"
52 }
53
54 tarball=$(getsource $spec 0)
55 if [ -z "$tarball" ]; then
56         echo >&2 "Spec is missing Source0!"
57         exit 1
58 fi
59
60 if [ ! -f $tarball ]; then
61         ./builder -g "$spec"
62 fi
63
64 stmp=$(mktemp "${TMPDIR:-/tmp}/fragXXXXXX")
65 template=pearize.spec
66 cat > $stmp <<'EOF'
67 @extra_headers@
68 License: @release_license@
69 State: @release_state@
70 EOF
71 pear make-rpm-spec --spec-template=$stmp --output=$template $tarball
72 rm -f $stmp
73
74 mv $template .$template~
75 template=.$template~
76
77 # take as argument dependency in form NAME EQUALITY VERSION
78 # adds rpm epoch to VERSION if the package is installed and has epoch bigger than zero.
79 add_epoch() {
80         local dep="$@"
81         local pkg="$1"
82         local ver="$3"
83
84         # already have epoch
85         if [[ "$ver" = *:* ]]; then
86                 echo "$dep"
87                 return
88         fi
89
90         query=$(rpm -q --qf '%{epoch}\n' $pkg || :)
91         epoch=$(echo "$query" | grep -v 'installed' || :)
92         if [ "$epoch" ] && [ "$epoch" -gt 0 ]; then
93                 echo "$dep" | sed -e "s, [<>=]\+ ,&$epoch:,"
94         else
95                 echo "$dep"
96         fi
97 }
98
99 preamble=$(mktemp "${TMPDIR:-/tmp}/fragXXXXXX")
100 # take just main package preamble, preamble of tests (and other) subpackage(s) just confuses things.
101 sed -ne '/^Name:/,/^BuildRoot/p' $spec > $preamble
102
103 # create backup
104 bak=$(cp -fbv $spec $spec | awk '{print $NF}' | tr -d "['\`]" )
105
106 # ensure rpm-build-macros is present
107 if ! grep -q "^BuildRequires:.*rpmbuild(macros)" $preamble; then
108         sed -i -e "/^BuildRequires:.*rpm-php-pearprov/aBuildRequires:\trpmbuild(macros) >= 1.300" $spec
109 fi
110 # parse requires
111 requires=$(grep '^BuildRequires:' $template || :)
112 if [ -n "$requires" ]; then
113         echo "$requires" | while read tag dep; do
114                 dep=$(add_epoch $dep)
115                 if ! grep -q "^BuildRequires:.*$dep" $preamble; then
116                         sed -i -e "/^BuildRoot/iBuildRequires:\t$dep" $spec
117                 fi
118         done
119 fi
120
121 requires=$(grep '^Requires:' $template || :)
122 if [ -n "$requires" ]; then
123         echo "$requires" | while read tag dep; do
124                 dep=$(add_epoch $dep)
125                 if ! grep -q "^Requires:.*$dep" $preamble; then
126                         dep=$(echo "$dep" | sed -e 's,php-pear-PEAR\b,php-pear-PEAR-core,')
127                         sed -i -e "/^BuildRoot/iRequires:\t$dep" $spec
128                 fi
129         done
130 fi
131
132 # parse conflicts
133 conflicts=$(grep '^Conflicts:' $template || :)
134 if [ -n "$conflicts" ]; then
135         echo "$conflicts" | while read tag dep; do
136                 dep=$(add_epoch $dep)
137                 if ! grep -q "^Conflicts:.*$req" $preamble; then
138                         sed -i -e "/^BuildRoot/iConflicts:\t$dep" $spec
139                 fi
140         done
141 fi
142
143 # parse optional deps
144 optional=$(grep '^Suggests:' $template || :)
145 if [ -n "$optional" ]; then
146         echo "$optional" | while read tag dep; do
147                 dep=$(add_epoch $dep)
148                 if ! grep -q "^Suggests:.*$dep" $preamble; then
149                         sed -i -e "/^BuildRoot/iSuggests:\t$dep" $spec
150                 fi
151
152                 for req in $dep; do
153                         case "$req" in
154                         php-pear-*)
155                                 # convert pear package name to file pattern
156                                 req=$(echo "$req" | sed -e 's,^php-pear-,pear(,;y,_,/,;s,$,.*),')
157                                 ;;
158                         *)
159                                 # process only php-pear-* packages
160                                 continue
161                                 ;;
162                         esac
163
164                         m=$(grep "^%define.*_noautoreq" $spec || :)
165                         if [ -z "$m" ]; then
166                                 sed -i -e "/^BuildRoot:/{
167                                         a
168                                         a# exclude optional dependencies
169                                         a%define\       \       _noautoreq\     $req
170                                 }
171                                 " $spec
172                         else
173                                 m=$(echo "$m" | grep -o "$req" || :)
174                                 if [ -z "$m" ]; then
175                                         sed -i -e "/^%define.*_noautoreq/s,$, $req," $spec
176                                 fi
177                         fi
178                 done
179         done
180 fi
181
182 optional=$(grep '^Optional-ext:' $template || :)
183 if [ -n "$optional" ]; then
184         tmp=$(mktemp "${TMPDIR:-/tmp}/fragXXXXXX")
185         echo "$optional" | while read tag ext; do
186                 grep -q "PHP extension .$ext" && continue
187                 cat > $tmp <<-EOF
188                 echo '%{name} can optionally use PHP extension "$ext"' >> optional-packages.txt
189                 EOF
190                 sed -i -e "
191                 /%pear_package_setup/ {
192                         r $tmp
193                 }
194                 " $spec
195         done
196         rm -f .ext.tmp
197 fi
198
199 has_opt=$(grep -Ec '^Optional-(pkg|ext):' $template || :)
200 if [ "$has_opt" -gt 0 ]; then
201         if ! grep -q 'rpmbuild(macros)' $spec; then
202                 sed -i -e '
203                 /rpm-php-pearprov/{
204                         aBuildRequires: rpmbuild(macros) >= 1.300
205                 }
206                 ' $spec
207         fi
208         if ! grep -Eq '%{_docdir}/.*/optional-packages.txt|%pear_package_print_optionalpackages' $spec; then
209                 sed -i -e '
210                 /^%files$/{
211                         i%post -p <lua>
212                         i%pear_package_print_optionalpackages
213                         i
214                 }
215                 /rpmbuild(macros)/{
216                         s/>=.*/>= 1.571/
217                 }
218                 ' $spec
219         fi
220         if ! grep -q '%doc.*optional-packages.txt' $spec; then
221                 sed -i -e '
222                 /^%doc install.log/{
223                 s/$/ optional-packages.txt/
224                 }
225                 ' $spec
226         fi
227 fi
228
229 # parse state
230 state=$(awk '/^State:/{print $2}' $template)
231 sed -i -e "/^%define.*_status/{
232         /%define.*_status.*$state/!s/.*/%define\t\t_status\t\t$state/
233 }" $spec
234
235 # parse license
236 #license=$(awk '/^License:/{print $2}' $template)
237 #sed -i -e "s/^License:.*/License:\t$license/" $spec
238
239 rm -f $preamble
240
241 diff=$(mktemp "${TMPDIR:-/tmp}/fragXXXXXX")
242 if ! diff -u $bak $spec > $diff; then
243         vim -o $spec $diff
244         rm -f $diff
245 else
246         echo "$spec: No diffs"
247 fi
This page took 0.056529 seconds and 4 git commands to generate.