[debhelper-devel] [debhelper] 04/08: Dh_Lib: Avoid forking 2-7 processes in autoscript with sub

Niels Thykier nthykier at moszumanska.debian.org
Sun Jul 16 11:13:55 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 6bb2c147828a457e837cae4198bacc70d2ad5e71
Author: Niels Thykier <niels at thykier.net>
Date:   Sat Jul 15 11:36:06 2017 +0000

    Dh_Lib: Avoid forking 2-7 processes in autoscript with sub
    
    Signed-off-by: Niels Thykier <niels at thykier.net>
---
 Debian/Debhelper/Dh_Lib.pm | 55 ++++++++++++++++++++++++++++++++--------------
 debian/changelog           |  2 ++
 2 files changed, 40 insertions(+), 17 deletions(-)

diff --git a/Debian/Debhelper/Dh_Lib.pm b/Debian/Debhelper/Dh_Lib.pm
index 14bfcfe..b648e4b 100644
--- a/Debian/Debhelper/Dh_Lib.pm
+++ b/Debian/Debhelper/Dh_Lib.pm
@@ -837,10 +837,7 @@ sub _tool_version {
 # 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;
-	my $filename=shift;
-	my $sed=shift || "";
+	my ($package, $script, $filename, $sed) = @_;
 
 	my $tool_version = _tool_version();
 	# This is the file we will modify.
@@ -864,13 +861,33 @@ sub autoscript {
 	if (-e $outfile && ($script eq 'postrm' || $script eq 'prerm')
 	   && !compat(5)) {
 		# Add fragments to top so they run in reverse order when removing.
-		complex_doit("echo \"# Automatically added by ".basename($0)."/${tool_version}\"> $outfile.new");
-		autoscript_sed($sed, $infile, "$outfile.new");
-		complex_doit("echo '# End automatically added section' >> $outfile.new");
-		complex_doit("cat $outfile >> $outfile.new");
+		if (not defined($sed) or ref($sed)) {
+			verbose_print("[META] Prepend autosnippet \"$filename\" to $script [${outfile}.new]");
+			open(my $out_fd, '>', "${outfile}.new") or error("open(${outfile}.new): $!");
+			print {$out_fd} '# Automatically added by ' . basename($0) . "/${tool_version}\n";
+			autoscript_sed($sed, $infile, undef, $out_fd);
+			print {$out_fd} "# End automatically added section\n";
+			open(my $in_fd, '<', $outfile) or error("open($outfile): $!");
+			while (my $line = <$in_fd>) {
+				print {$out_fd} $line;
+			}
+			close($in_fd);
+			close($out_fd) or error("close(${outfile}.new): $!");
+		} else {
+			complex_doit("echo \"# Automatically added by ".basename($0)."/${tool_version}\"> $outfile.new");
+			autoscript_sed($sed, $infile, "$outfile.new");
+			complex_doit("echo '# End automatically added section' >> $outfile.new");
+			complex_doit("cat $outfile >> $outfile.new");
+		}
 		rename_path("${outfile}.new", $outfile);
-	}
-	else {
+	} elsif (not defined($sed) or ref($sed)) {
+		verbose_print("[META] Append autosnippet \"$filename\" to $script [${outfile}]");
+		open(my $out_fd, '>>', $outfile) or error("open(${outfile}): $!");
+		print {$out_fd} '# Automatically added by ' . basename($0) . "/${tool_version}\n";
+		autoscript_sed($sed, $infile, undef, $out_fd);
+		print {$out_fd} "# End automatically added section\n";
+		close($out_fd) or error("close(${outfile}): $!");
+	} else {
 		complex_doit("echo \"# Automatically added by ".basename($0)."/${tool_version}\">> $outfile");
 		autoscript_sed($sed, $infile, $outfile);
 		complex_doit("echo '# End automatically added section' >> $outfile");
@@ -878,17 +895,21 @@ sub autoscript {
 }
 
 sub autoscript_sed {
-	my $sed = shift;
-	my $infile = shift;
-	my $outfile = shift;
-	if (ref($sed) eq 'CODE') {
+	my ($sed, $infile, $outfile, $out_fd) = @_;
+	if (not defined($sed) or ref($sed)) {
+		my $out = $out_fd;
 		open(my $in, '<', $infile) or die "$infile: $!";
-		open(my $out, '>>', $outfile) or die "$outfile: $!";
-		while (<$in>) { $sed->(); print {$out} $_; }
-		close($out) or die "$outfile: $!";
+		if (not defined($out_fd)) {
+			open($out, '>>', $outfile) or error("open($outfile): $!");
+		}
+		while (<$in>) { $sed->() if $sed; print {$out} $_; }
+		if (not defined($out_fd)) {
+			close($out) or error("close($outfile): $!");
+		}
 		close($in) or die "$infile: $!";
 	}
 	else {
+		error("Internal error - passed open handle for legacy method") if defined($out_fd);
 		complex_doit("sed \"$sed\" $infile >> $outfile");
 	}
 }
diff --git a/debian/changelog b/debian/changelog
index 162aa78..55a07bd 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -16,6 +16,8 @@ debhelper (10.7) UNRELEASED; urgency=medium
     trivial operations in the child process (e.g. redirect stdout).
   * dh_installman: Avoid forking a shell where a simple subprocess call
     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.
 
  -- Niels Thykier <niels at thykier.net>  Sat, 15 Jul 2017 09:42:32 +0000
 

-- 
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