[debhelper-devel] [debhelper] 05/08: autoscript: Support hashref for substitution

Niels Thykier nthykier at moszumanska.debian.org
Sat Jul 15 19:19:21 UTC 2017


This is an automated email from the git hooks/post-receive script.

nthykier pushed a commit to branch shell-out-less
in repository debhelper.

commit 7e3ef5f67ccaad271f307f3d72d4f9acfb45fe5c
Author: Niels Thykier <niels at thykier.net>
Date:   Sat Jul 15 13:29:48 2017 +0000

    autoscript: Support hashref for substitution
    
    Signed-off-by: Niels Thykier <niels at thykier.net>
---
 Debian/Debhelper/Dh_Lib.pm | 11 ++++++++++-
 debian/changelog           |  4 ++++
 doc/PROGRAMMING            | 14 ++++++++------
 t/dh-lib.t                 |  6 ++++++
 4 files changed, 28 insertions(+), 7 deletions(-)

diff --git a/Debian/Debhelper/Dh_Lib.pm b/Debian/Debhelper/Dh_Lib.pm
index b648e4b..66fe9d0 100644
--- a/Debian/Debhelper/Dh_Lib.pm
+++ b/Debian/Debhelper/Dh_Lib.pm
@@ -902,7 +902,16 @@ sub autoscript_sed {
 		if (not defined($out_fd)) {
 			open($out, '>>', $outfile) or error("open($outfile): $!");
 		}
-		while (<$in>) { $sed->() if $sed; print {$out} $_; }
+		if (not defined($sed) or ref($sed) eq 'CODE') {
+			while (<$in>) { $sed->() if $sed; print {$out} $_; }
+		} else {
+			my $rstr = sprintf('#(%s)#', join('|', reverse(sort(keys(%$sed)))));
+			my $regex = qr/$rstr/;
+			while (my $line = <$in>) {
+				$line =~ s/$regex/$sed->{$1}/eg;
+				print {$out} $line;
+			}
+		}
 		if (not defined($out_fd)) {
 			close($out) or error("close($outfile): $!");
 		}
diff --git a/debian/changelog b/debian/changelog
index 55a07bd..2e93937 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -18,6 +18,10 @@ debhelper (10.7) UNRELEASED; urgency=medium
     is sufficient.
   * Dh_Lib: Avoid forking 2 to 7 subprocesses for adding an autosnippet
     when helper tools use a subroutine rather then a sed snippet.
+  * Dh_Lib: Extend autoscript to accept a hashref and use that for
+    substituting into the snippet.  This variant (like the subroutine
+    variant) avoids forking a lot of subprocesses and need not worry
+    about a shell possible interpreting metacharacters.
 
  -- Niels Thykier <niels at thykier.net>  Sat, 15 Jul 2017 09:42:32 +0000
 
diff --git a/doc/PROGRAMMING b/doc/PROGRAMMING
index f195e4a..7c765a9 100644
--- a/doc/PROGRAMMING
+++ b/doc/PROGRAMMING
@@ -239,16 +239,18 @@ isnative($package)
 	is a native debian package.
 	As a side effect, $dh{VERSION} is set to the version number of the
 	package.
-autoscript($package, $scriptname, $snippetname, $sedcommands || $sub)
+autoscript($package, $scriptname, $snippetname, $substparam)
 	Pass parameters:
 	 - binary package to be affected
 	 - script to add to
 	 - filename of snippet
-	 - (optional) EITHER sed commands to run on the snippet. Ie,
-	   s/#PACKAGE#/$PACKAGE/ Note: Passed to the shell inside double
-           quotes.
-	   OR a perl sub to invoke with $_ set to each line of the snippet in
-           turn.
+	 - (optional) A substitution parameter, which is one of 3 times:
+	   * sed commands to run on the snippet. E.g. s/#PACKAGE#/$PACKAGE/
+	     Note: Passed to the shell inside double quotes.
+	   * a perl sub to invoke with $_ set to each line of the snippet
+	     in turn.
+	   * a hashref, where each key will substitute "#${key}#" with the
+	     value that $key points to.  [debhelper (>= 10.7)]
 	This command automatically adds shell script snippets to a debian
 	maintainer script (like the postinst or prerm).
 	Note that in v6 mode and up, the snippets are added in reverse
diff --git a/t/dh-lib.t b/t/dh-lib.t
index d1eb355..66f38c3 100755
--- a/t/dh-lib.t
+++ b/t/dh-lib.t
@@ -35,5 +35,11 @@ each_compat_subtest {
 	ok_autoscript_result;
 
 	ok(rm_files('debian/testpackage.postinst.debhelper'));
+
+	ok(autoscript('testpackage', 'postinst', 'postinst-init',
+				  { 'SCRIPT' => 'test-script', 'INITPARMS' => 'test parms with"quote' } ));
+	ok_autoscript_result;
+
+	ok(rm_files('debian/testpackage.postinst.debhelper'));
 }
 

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debhelper/debhelper.git




More information about the debhelper-devel mailing list