]> git.pld-linux.org Git - packages/rpm-build-tools.git/blob - adapter.sh
- lftp debug 5 when debug enabled
[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         _npkgconfigdir
198
199         perl_sitearch
200         perl_archlib
201         perl_privlib
202         perl_vendorlib
203         perl_vendorarch
204         perl_sitelib
205
206         py_sitescriptdir
207         py_sitedir
208         py_scriptdir
209         py_ver
210
211         py3_sitescriptdir
212         py3_sitedir
213         py3_scriptdir
214         py3_ver
215
216         ruby_archdir
217         ruby_ridir
218         ruby_rubylibdir
219         ruby_sitearchdir
220         ruby_sitelibdir
221         ruby_rdocdir
222
223         php_pear_dir
224         php_data_dir
225         tmpdir
226 "
227         eval_expr=""
228         for macro in $macros; do
229                 eval_expr="$eval_expr\nexport $macro='%{$macro}'"
230         done
231
232
233         # get cvsaddress for changelog section
234         # using rpm macros as too lazy to add ~/.adapterrc parsing support.
235         eval_expr="$eval_expr
236         export _cvsmaildomain='%{?_cvsmaildomain}%{!?_cvsmaildomain:@pld-linux.org}'
237         export _cvsmailfeedback='%{?_cvsmailfeedback}%{!?_cvsmailfeedback:PLD Team <feedback@pld-linux.org>}'
238         "
239
240         export ADAPTER_REVISION=$REVISION
241
242         eval $(rpm --eval "$(echo -e $eval_expr)")
243 }
244
245 adapterize() {
246         local workdir
247         workdir=$(mktemp -d ${TMPDIR:-/tmp}/adapter-XXXXXX) || exit $?
248         awk=gawk
249
250         local tmp=$workdir/$(basename $SPECFILE) || exit $?
251
252         import_rpm_macros
253
254         LC_ALL=en_US.UTF-8 $awk -f $adapter $SPECFILE > $tmp || exit $?
255
256         if [ "$outputonly" = 1 ]; then
257                 cat $tmp
258
259         elif [ "$(diff --brief $SPECFILE $tmp)" ]; then
260                 diff -u $SPECFILE $tmp > $tmp.diff
261                 if [ -t 1 ]; then
262                                 diffcol $tmp.diff | $PAGER
263                                 while : ; do
264                                         echo -n "Accept? (Yes, No, Confirm each chunk)? "
265                                         read ans
266                                         case "$ans" in
267                                         [yYoO]) # y0 mama
268                                                 mv -f $tmp $SPECFILE
269                                                 echo "Ok, adapterized."
270                                                 break
271                                         ;;
272                                         [cC]) # confirm each chunk
273                                                 diff2hunks $tmp.diff
274                                                 for t in $(ls $tmp-*.diff); do
275                                                                 diffcol $t | $PAGER
276                                                                 echo -n "Accept? (Yes, [N]o, Quit)? "
277                                                                 read ans
278                                                                 case "$ans" in
279                                                                 [yYoO]) # y0 mama
280                                                                         patch -p0 < $t
281                                                                         ;;
282                                                                 [Q]) # Abort
283                                                                         break
284                                                                         ;;
285                                                                 esac
286                                                 done
287                                                 break
288                                         ;;
289                                         [QqnNsS])
290                                                 echo "Ok, exiting."
291                                                 break
292                                         ;;
293                                         esac
294                                 done
295                 else
296                                 cat $tmp.diff
297                 fi
298         else
299                 echo "The SPEC is perfect ;)"
300         fi
301
302         rm -rf $workdir
303 }
304
305 SPECFILE="$1"
306 [ -f "$SPECFILE" ] || SPECFILE="$(basename $SPECFILE .spec).spec"
307
308 if [ $# -ne 1 -o ! -f "$SPECFILE" ]; then
309         echo "$usage"
310         exit 1
311 fi
312
313 adapterize
314
315 # vim: ts=4:sw=4
This page took 0.091625 seconds and 3 git commands to generate.