]> git.pld-linux.org Git - packages/rpm-build-tools.git/blob - pearize.sh
- note packages with epoch
[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 # parse requires
107 requires=$(grep '^BuildRequires:' $template || :)
108 if [ -n "$requires" ]; then
109         echo "$requires" | while read tag dep; do
110                 dep=$(add_epoch $dep)
111                 if ! grep -q "^BuildRequires:.*$dep" $preamble; then
112                         sed -i -e "/^BuildRoot/iBuildRequires:\t$dep" $spec
113                 fi
114         done
115 fi
116
117 requires=$(grep '^Requires:' $template || :)
118 if [ -n "$requires" ]; then
119         echo "$requires" | while read tag dep; do
120                 dep=$(add_epoch $dep)
121                 if ! grep -q "^Requires:.*$dep" $preamble; then
122                         dep=$(echo "$dep" | sed -e 's,php-pear-PEAR\b,php-pear-PEAR-core,')
123                         sed -i -e "/^BuildRoot/iRequires:\t$dep" $spec
124                 fi
125         done
126 fi
127
128 # parse conflicts
129 conflicts=$(grep '^Conflicts:' $template || :)
130 if [ -n "$conflicts" ]; then
131         echo "$conflicts" | while read tag dep; do
132                 dep=$(add_epoch $dep)
133                 if ! grep -q "^Conflicts:.*$req" $preamble; then
134                         sed -i -e "/^BuildRoot/iConflicts:\t$dep" $spec
135                 fi
136         done
137 fi
138
139 # parse optional deps
140 optional=$(grep '^Suggests:' $template || :)
141 if [ -n "$optional" ]; then
142         echo "$optional" | while read tag dep; do
143                 dep=$(add_epoch $dep)
144                 if ! grep -q "^Suggests:.*$dep" $preamble; then
145                         sed -i -e "/^BuildRoot/iSuggests:\t$dep" $spec
146                 fi
147
148                 for req in $dep; do
149                         case "$req" in
150                         php-pear-*)
151                                 # convert pear package name to file pattern
152                                 req=$(echo "$req" | sed -e 's,^php-pear-,pear(,;y,_,/,;s,$,.*),')
153                                 ;;
154                         *)
155                                 # process only php-pear-* packages
156                                 continue
157                                 ;;
158                         esac
159
160                         m=$(grep "^%define.*_noautoreq" $spec || :)
161                         if [ -z "$m" ]; then
162                                 sed -i -e "/^BuildRoot:/{
163                                         a
164                                         a# exclude optional dependencies
165                                         a%define\       \       _noautoreq\     $req
166                                 }
167                                 " $spec
168                         else
169                                 m=$(echo "$m" | grep -o "$req" || :)
170                                 if [ -z "$m" ]; then
171                                         sed -i -e "/^%define.*_noautoreq/s,$, $req," $spec
172                                 fi
173                         fi
174                 done
175         done
176 fi
177
178 optional=$(grep '^Optional-ext:' $template || :)
179 if [ -n "$optional" ]; then
180         tmp=$(mktemp "${TMPDIR:-/tmp}/fragXXXXXX")
181         echo "$optional" | while read tag ext; do
182                 grep -q "PHP extension .$ext" && continue
183                 cat > $tmp <<-EOF
184                 echo '%{name} can optionally use PHP extension "$ext"' >> optional-packages.txt
185                 EOF
186                 sed -i -e "
187                 /%pear_package_setup/ {
188                         r $tmp
189                 }
190                 " $spec
191         done
192         rm -f .ext.tmp
193 fi
194
195 has_opt=$(grep -Ec '^Optional-(pkg|ext):' $template || :)
196 if [ "$has_opt" -gt 0 ]; then
197         if ! grep -q 'rpmbuild(macros)' $spec; then
198                 sed -i -e '
199                 /rpm-php-pearprov/{
200                         aBuildRequires: rpmbuild(macros) >= 1.300
201                 }
202                 ' $spec
203         fi
204         if ! grep -Eq '%{_docdir}/.*/optional-packages.txt|%pear_package_print_optionalpackages' $spec; then
205                 sed -i -e '
206                 /^%files$/{
207                         i%post -p <lua>
208                         i%pear_package_print_optionalpackages
209                         i
210                 }
211                 /rpmbuild(macros)/{
212                         s/>=.*/>= 1.571/
213                 }
214                 ' $spec
215         fi
216         if ! grep -q '%doc.*optional-packages.txt' $spec; then
217                 sed -i -e '
218                 /^%doc install.log/{
219                 s/$/ optional-packages.txt/
220                 }
221                 ' $spec
222         fi
223 fi
224
225 # parse state
226 state=$(awk '/^State:/{print $2}' $template)
227 sed -i -e "/^%define.*_status/{
228         /%define.*_status.*$state/!s/.*/%define\t\t_status\t\t$state/
229 }" $spec
230
231 # parse license
232 #license=$(awk '/^License:/{print $2}' $template)
233 #sed -i -e "s/^License:.*/License:\t$license/" $spec
234
235 rm -f $preamble
236
237 diff=$(mktemp "${TMPDIR:-/tmp}/fragXXXXXX")
238 if ! diff -u $bak $spec > $diff; then
239         vim -o $spec $diff
240         rm -f $diff
241 else
242         echo "$spec: No diffs"
243 fi
This page took 0.102624 seconds and 4 git commands to generate.