]> git.pld-linux.org Git - packages/rpm-build-tools.git/blob - adapter_.sh
- v0.35
[packages/rpm-build-tools.git] / adapter_.sh
1 #!/bin/sh
2 #
3 # Authors:
4 #       Michał Kuratczyk <kura@pld.org.pl>
5 #       Sebastian Zagrodzki <s.zagrodzki@mimuw.edu.pl>
6 #       Tomasz Kłoczko <kloczek@rudy.mif.pg.gda.pl>
7 #       Artur Frysiak <wiget@pld-linux.org>
8 #       Michal Kochanowicz <mkochano@pld.org.pl>
9 #       Elan Ruusamäe <glen@pld-linux.org>
10 #
11 # See cvs log adapter{,.awk} for list of contributors
12 #
13 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
14
15 RCSID='$Id$'
16 r=${RCSID#* * }
17 rev=${r%% *}
18 VERSION="v0.35/$rev"
19 VERSIONSTRING="\
20 Adapter adapts .spec files for PLD Linux.
21 $VERSION (C) 1999-2009 Free Penguins".
22
23 PROGRAM=${0##*/}
24 dir=$(d=$0; [ -L "$d" ] && d=$(readlink "$d"); dirname "$d")
25 adapter=$dir/adapter.awk
26 usage="Usage: $PROGRAM [FLAGS] SPECFILE
27
28 -s|--no-sort|--skip-sort
29         skip BuildRequires, Requires sorting
30 -m|--no-macros|--skip-macros
31         skip use_macros() substitutions
32 -d|--skip-desc
33         skip desc wrapping
34 -a|--skip-defattr
35         skip %defattr corrections
36 -o
37         do not do any diffing, just dump the output
38 "
39
40 if [ ! -x /usr/bin/getopt ]; then
41         echo >&2 "You need to install util-linux to use adapter"
42         exit 1
43 fi
44
45 if [ ! -x /usr/bin/patch ]; then
46         echo >&2 "You need to install patch to use adapter"
47         exit 1
48 fi
49
50 t=$(getopt -o hsomdaV --long help,version,sort,sort-br,no-macros,skip-macros,skip-desc,skip-defattr -n "$PROGRAM" -- "$@") || exit $?
51 eval set -- "$t"
52
53 while true; do
54         case "$1" in
55         -h|--help)
56                 echo 2>&1 "$usage"
57                 exit 1
58         ;;
59         -s|--no-sort|--skip-sort)
60                 export SKIP_SORTBR=1
61         ;;
62         -m|--no-macros|--skip-macros)
63                 export SKIP_MACROS=1
64         ;;
65         -d|--skip-desc)
66                 export SKIP_DESC=1
67         ;;
68         -a|--skip-defattr)
69                 export SKIP_DEFATTR=1
70         ;;
71         -V|--version)
72                 echo "$VERSIONSTRING"
73                 exit 0
74                 ;;
75         -o)
76                 outputonly=1
77         ;;
78         --)
79                 shift
80                 break
81         ;;
82         *)
83                 echo >&2 "$PROGRAM: Internal error: \`$1' not recognized!"
84                 exit 1
85                 ;;
86         esac
87         shift
88 done
89
90 diffcol()
91 {
92          # vim like diff colourization
93          sed -e '
94          s,\e,\e[44m^[\e[49m,g;
95          s,\a,\e[44m^G\e[49m,g;
96          s,^\(Index:\|diff\|---\|+++\) .*$,\e[32m&,;
97          s,^@@ ,\e[33m&,g;
98          s,^-,\e[35m&,;
99          s,^+,\e[36m&,;
100          s,\r,\e[44m^M\e[49m,g;
101          s,     ,    ,g;
102          s,\([^[:space:]]\)\([[:space:]]\+\)$,\1\e[41m\2\e[49m,g;
103          s,$,\e[0m,
104          ' "$@"
105 }
106
107 diff2hunks()
108 {
109          # diff2hunks orignally by dig
110          perl -e '
111 #! /usr/bin/perl -w
112
113 use strict;
114
115 for my $filename (@ARGV) {
116         my $counter = 1;
117         my $fh;
118         open $fh, "<", $filename or die "$filename: open for reading: $!";
119         my @lines = <$fh>;
120         my @hunks;
121         my @curheader;
122         for my $i (0 ... $#lines) {
123                 next unless $lines[$i] =~ m/^\@\@ /;
124                 if ($i >= 2 and $lines[$i - 2] =~ m/^--- / and $lines[$i - 1] =~ m/^\+\+\+ /) {
125                         @curheader = @lines[$i - 2 ... $i - 1];
126                 }
127                 next unless @curheader;
128                 my $j = $i + 1;
129                 while ($j < @lines and $lines[$j] !~ m/^\@\@ /) {$j++}
130                 $j -= 2
131                         if $j >= 3 and $j < @lines
132                                 and $lines[$j - 2] =~ m/^--- /
133                                 and $lines[$j - 1] =~ m/^\+\+\+ /;
134                 $j--;
135                 $j-- until $lines[$j] =~ m/^[ @+-]/;
136                 my $hunkfilename = $filename;
137                 $hunkfilename =~ s/((\.(pat(ch)?|diff?))?)$/"-".sprintf("%03i",$counter++).$1/ei;
138                 my $ofh;
139                 open $ofh, ">", $hunkfilename or die "$hunkfilename: open for writing: $!";
140                 print $ofh @curheader, @lines[$i ... $j];
141                 close $ofh;
142         }
143 }
144 ' "$@"
145 }
146
147 # import selected macros for adapter.awk
148 # you should update the list also in adapter.awk when making changes here
149 import_rpm_macros() {
150         macros="
151         _topdir
152         _prefix
153         _bindir
154         _sbindir
155         _libdir
156         _sysconfdir
157         _datadir
158         _includedir
159         _mandir
160         _infodir
161         _examplesdir
162         _defaultdocdir
163         _kdedocdir
164         _gtkdocdir
165         _desktopdir
166         _pixmapsdir
167         _javadir
168
169         perl_sitearch
170         perl_archlib
171         perl_privlib
172         perl_vendorlib
173         perl_vendorarch
174         perl_sitelib
175
176         py_sitescriptdir
177         py_sitedir
178         py_scriptdir
179         py_ver
180
181         ruby_archdir
182         ruby_ridir
183         ruby_rubylibdir
184         ruby_sitearchdir
185         ruby_sitelibdir
186         ruby_rdocdir
187
188         php_pear_dir
189         php_data_dir
190         tmpdir
191 "
192         eval_expr=""
193         for macro in $macros; do
194                 eval_expr="$eval_expr\nexport $macro='%{$macro}'"
195         done
196
197
198         # get cvsaddress for changelog section
199         # using rpm macros as too lazy to add ~/.adapterrc parsing support.
200         eval_expr="$eval_expr
201         export _cvsmaildomain='%{?_cvsmaildomain}%{!?_cvsmaildomain:@pld-linux.org}'
202         export _cvsmailfeedback='%{?_cvsmailfeedback}%{!?_cvsmailfeedback:PLD Team <feedback@pld-linux.org>}'
203         "
204
205         eval $(rpm --eval "$(echo -e $eval_expr)")
206 }
207
208 adapterize() {
209         local workdir
210         workdir=$(mktemp -d ${TMPDIR:-/tmp}/adapter-XXXXXX) || exit
211         awk=gawk
212
213         local tmp=$workdir/$(basename $SPECFILE) || exit
214
215         import_rpm_macros
216
217         LC_ALL=en_US.UTF-8 $awk -f $adapter $SPECFILE > $tmp || exit
218
219         if [ "$outputonly" = 1 ]; then
220                 cat $tmp
221
222         elif [ "$(diff --brief $SPECFILE $tmp)" ]; then
223                 diff -u $SPECFILE $tmp > $tmp.diff
224                 if [ -t 1 ]; then
225                                 diffcol $tmp.diff | less -r
226                                 while : ; do
227                                         echo -n "Accept? (Yes, No, Confirm each chunk)? "
228                                         read ans
229                                         case "$ans" in
230                                         [yYoO]) # y0 mama
231                                                 mv -f $tmp $SPECFILE
232                                                 echo "Ok, adapterized."
233                                                 break
234                                         ;;
235                                         [cC]) # confirm each chunk
236                                                 diff2hunks $tmp.diff
237                                                 for t in $(ls $tmp-*.diff); do
238                                                                 diffcol $t | less -r
239                                                                 echo -n "Accept? (Yes, [N]o, Quit)? "
240                                                                 read ans
241                                                                 case "$ans" in
242                                                                 [yYoO]) # y0 mama
243                                                                         patch < $t
244                                                                         ;;
245                                                                 [Q]) # Abort
246                                                                         break
247                                                                         ;;
248                                                                 esac
249                                                 done
250                                                 break
251                                         ;;
252                                         [QqnNsS])
253                                                 echo "Ok, exiting."
254                                                 break
255                                         ;;
256                                         esac
257                                 done
258                 else
259                                 cat $tmp.diff
260                 fi
261         else
262                 echo "The SPEC is perfect ;)"
263         fi
264
265         rm -rf $workdir
266 }
267
268 SPECFILE="$1"
269 [ -f "$SPECFILE" ] || SPECFILE="$(basename $SPECFILE .spec).spec"
270
271 if [ $# -ne 1 -o ! -f "$SPECFILE" ]; then
272         echo "$usage"
273         exit 1
274 fi
275
276 adapterize
This page took 0.054709 seconds and 4 git commands to generate.