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