summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNiels Thykier <niels@thykier.net>2017-07-15 13:29:48 +0000
committerNiels Thykier <niels@thykier.net>2017-07-22 17:29:38 +0000
commit99a07068640f57cbab2a88511c221802d505b19e (patch)
treed54f4d6e3823d99fab07d450d6a533de371cba48
parent9b2bb8378582ee39f40e01a98b907ddf1b887e5d (diff)
downloaddebhelper-99a07068640f57cbab2a88511c221802d505b19e.tar.gz
autoscript: Support hashref for substitution
Signed-off-by: Niels Thykier <niels@thykier.net>
-rw-r--r--Debian/Debhelper/Dh_Lib.pm11
-rw-r--r--debian/changelog4
-rw-r--r--doc/PROGRAMMING14
-rwxr-xr-xt/dh-lib.t6
4 files changed, 28 insertions, 7 deletions
diff --git a/Debian/Debhelper/Dh_Lib.pm b/Debian/Debhelper/Dh_Lib.pm
index 7e45c710..f5e91f51 100644
--- a/Debian/Debhelper/Dh_Lib.pm
+++ b/Debian/Debhelper/Dh_Lib.pm
@@ -917,7 +917,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 23b47649..6b3cdfd9 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -21,6 +21,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@thykier.net> Sat, 15 Jul 2017 09:42:32 +0000
diff --git a/doc/PROGRAMMING b/doc/PROGRAMMING
index 1115d998..5befe565 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 d1eb355a..66f38c30 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'));
}