]> git.pld-linux.org Git - packages/rpm-build-tools.git/blame - adapter_.sh
- diffcol with tab marking, helps to understand adapter whitespace changes
[packages/rpm-build-tools.git] / adapter_.sh
CommitLineData
fd283fd9
ER
1#!/bin/sh
2#
fd283fd9
ER
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
77b7f1e2
ER
15RCSID='$Id$'
16r=${RCSID#* * }
17rev=${r%% *}
b0f031e7 18VERSION="v0.35/$rev"
77b7f1e2
ER
19VERSIONSTRING="\
20Adapter adapts .spec files for PLD Linux.
21$VERSION (C) 1999-2009 Free Penguins".
22
23PROGRAM=${0##*/}
24dir=$(d=$0; [ -L "$d" ] && d=$(readlink "$d"); dirname "$d")
25adapter=$dir/adapter.awk
26usage="Usage: $PROGRAM [FLAGS] SPECFILE
fd283fd9
ER
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
77b7f1e2
ER
36-o
37 do not do any diffing, just dump the output
fd283fd9
ER
38"
39
40if [ ! -x /usr/bin/getopt ]; then
77b7f1e2 41 echo >&2 "You need to install util-linux to use adapter"
fd283fd9
ER
42 exit 1
43fi
44
45if [ ! -x /usr/bin/patch ]; then
77b7f1e2 46 echo >&2 "You need to install patch to use adapter"
fd283fd9
ER
47 exit 1
48fi
49
77b7f1e2 50t=$(getopt -o hsomdaV --long help,version,sort,sort-br,no-macros,skip-macros,skip-desc,skip-defattr -n "$PROGRAM" -- "$@") || exit $?
fd283fd9
ER
51eval set -- "$t"
52
53while 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 ;;
77b7f1e2
ER
71 -V|--version)
72 echo "$VERSIONSTRING"
73 exit 0
74 ;;
75 -o)
76 outputonly=1
77 ;;
fd283fd9
ER
78 --)
79 shift
db90382f 80 break
fd283fd9
ER
81 ;;
82 *)
77b7f1e2 83 echo >&2 "$PROGRAM: Internal error: \`$1' not recognized!"
fd283fd9 84 exit 1
db90382f 85 ;;
fd283fd9
ER
86 esac
87 shift
88done
89
90diffcol()
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&,;
81deaf3c 100 s,\r,\e[44m^M\e[49m,g;
fd283fd9
ER
101 s, , ,g;
102 s,\([^[:space:]]\)\([[:space:]]\+\)$,\1\e[41m\2\e[49m,g;
103 s,$,\e[0m,
104 ' "$@"
105}
106
107diff2hunks()
108{
109 # diff2hunks orignally by dig
110 perl -e '
111#! /usr/bin/perl -w
112
113use strict;
114
115for my $filename (@ARGV) {
db90382f
ER
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 }
fd283fd9
ER
143}
144' "$@"
145}
146
77b7f1e2
ER
147# import selected macros for adapter.awk
148# you should update the list also in adapter.awk when making changes here
149import_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
208adapterize() {
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
db90382f 218
77b7f1e2
ER
219 if [ "$outputonly" = 1 ]; then
220 cat $tmp
db90382f 221
77b7f1e2 222 elif [ "$(diff --brief $SPECFILE $tmp)" ]; then
db90382f
ER
223 diff -u $SPECFILE $tmp > $tmp.diff
224 if [ -t 1 ]; then
225 diffcol $tmp.diff | less -r
fd283fd9 226 while : ; do
db90382f
ER
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
fd283fd9
ER
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 ;;
db90382f 245 [Q]) # Abort
fd283fd9
ER
246 break
247 ;;
248 esac
db90382f
ER
249 done
250 break
251 ;;
252 [QqnNsS])
253 echo "Ok, exiting."
254 break
255 ;;
256 esac
fd283fd9 257 done
db90382f
ER
258 else
259 cat $tmp.diff
260 fi
261 else
262 echo "The SPEC is perfect ;)"
263 fi
264
77b7f1e2 265 rm -rf $workdir
fd283fd9
ER
266}
267
81deaf3c
ER
268SPECFILE="$1"
269[ -f "$SPECFILE" ] || SPECFILE="$(basename $SPECFILE .spec).spec"
270
271if [ $# -ne 1 -o ! -f "$SPECFILE" ]; then
fd283fd9
ER
272 echo "$usage"
273 exit 1
274fi
275
fd283fd9 276adapterize
This page took 0.063293 seconds and 4 git commands to generate.