From a3494762925e5a42a42ce82c688f62f163ffad1b Mon Sep 17 00:00:00 2001 From: Marcin Owsiany Date: Sun, 9 Sep 2012 12:13:20 +0100 Subject: Make it possible to pass perl code to autoscript. The shell-quoted sed code passed as parameter 4 is fragile (see Bug#665296). Make it possible to pass a sub that operates on each line via $_ instead. Also add a basic unit test for Dh_Lib, for now just with tests for autoscript. --- Debian/Debhelper/Dh_Lib.pm | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) (limited to 'Debian/Debhelper') diff --git a/Debian/Debhelper/Dh_Lib.pm b/Debian/Debhelper/Dh_Lib.pm index 10ae69f7..7b550b0e 100644 --- a/Debian/Debhelper/Dh_Lib.pm +++ b/Debian/Debhelper/Dh_Lib.pm @@ -504,7 +504,8 @@ sub pkgfilename { # 1: package # 2: script to add to # 3: filename of snippet -# 4: sed to run on the snippet. Ie, s/#PACKAGE#/$PACKAGE/ +# 4: either text: shell-quoted sed to run on the snippet. Ie, 's/#PACKAGE#/$PACKAGE/' +# or a sub to run on each line of the snippet. Ie sub { s/#PACKAGE#/$PACKAGE/ } sub autoscript { my $package=shift; my $script=shift; @@ -533,18 +534,34 @@ sub autoscript { && !compat(5)) { # Add fragments to top so they run in reverse order when removing. complex_doit("echo \"# Automatically added by ".basename($0)."\"> $outfile.new"); - complex_doit("sed \"$sed\" $infile >> $outfile.new"); + autoscript_sed($sed, $infile, "$outfile.new"); complex_doit("echo '# End automatically added section' >> $outfile.new"); complex_doit("cat $outfile >> $outfile.new"); complex_doit("mv $outfile.new $outfile"); } else { complex_doit("echo \"# Automatically added by ".basename($0)."\">> $outfile"); - complex_doit("sed \"$sed\" $infile >> $outfile"); + autoscript_sed($sed, $infile, $outfile); complex_doit("echo '# End automatically added section' >> $outfile"); } } +sub autoscript_sed { + my $sed = shift; + my $infile = shift; + my $outfile = shift; + if (ref($sed) eq 'CODE') { + open(IN, $infile) or die "$infile: $!"; + open(OUT, ">>$outfile") or die "$outfile: $!"; + while () { $sed->(); print OUT } + close(OUT) or die "$outfile: $!"; + close(IN) or die "$infile: $!"; + } + else { + complex_doit("sed \"$sed\" $infile >> $outfile"); + } +} + # Removes a whole substvar line. sub delsubstvar { my $package=shift; -- cgit v1.2.3