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