[debhelper-devel] [debhelper] 01/02: Dh_Lib: Support ordering for service handling autosnippets

Niels Thykier nthykier at moszumanska.debian.org
Wed Jan 3 10:46:09 UTC 2018


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

nthykier pushed a commit to branch master
in repository debhelper.

commit e6870ceafb9d51800de86a7106cdfb4ce9c9dad8
Author: Niels Thykier <niels at thykier.net>
Date:   Wed Jan 3 10:41:20 2018 +0000

    Dh_Lib: Support ordering for service handling autosnippets
    
    This is part 1 of 2 for solving Debian#814285 and Debian#885998
    
    Signed-off-by: Niels Thykier <niels at thykier.net>
---
 lib/Debian/Debhelper/Dh_Lib.pm | 78 ++++++++++++++++++++++++++++++++----------
 t/Test/DH.pm                   | 22 +++++++++++-
 2 files changed, 80 insertions(+), 20 deletions(-)

diff --git a/lib/Debian/Debhelper/Dh_Lib.pm b/lib/Debian/Debhelper/Dh_Lib.pm
index 82f0761..6c8194a 100644
--- a/lib/Debian/Debhelper/Dh_Lib.pm
+++ b/lib/Debian/Debhelper/Dh_Lib.pm
@@ -921,12 +921,18 @@ sub _tool_version {
 # 3: filename of snippet
 # 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/ }
+#    or a hashref with keys being variables and values being their replacement.  Ie. { PACKAGE => $PACKAGE }
+# 5: Internal usage only
 sub autoscript {
-	my ($package, $script, $filename, $sed) = @_;
+	my ($package, $script, $filename, $sed, $extra_options) = @_;
 
 	my $tool_version = _tool_version();
 	# This is the file we will modify.
 	my $outfile="debian/".pkgext($package)."$script.debhelper";
+	if ($extra_options && exists($extra_options->{'snippet-order'})) {
+		my $order = $extra_options->{'snippet-order'};
+		$outfile = generated_file($package, "${script}.${order}");
+	}
 
 	# Figure out what shell script snippet to use.
 	my $infile;
@@ -1618,6 +1624,18 @@ sub is_udeb {
 	}
 }
 
+sub _concat_slurp_script_files {
+	my (@files) = @_;
+	my $res = '';
+	for my $file (@files) {
+		open(my $fd, '<', $file) or error("open($file) failed: $!");
+		my $f = <$fd>;
+		close($fd);
+		$res .= $f;
+	}
+	return $res;
+}
+
 # Handles #DEBHELPER# substitution in a script; also can generate a new
 # script from scratch if none exists but there is a .debhelper file for it.
 sub debhelper_script_subst {
@@ -1627,34 +1645,56 @@ sub debhelper_script_subst {
 	my $tmp=tmpdir($package);
 	my $ext=pkgext($package);
 	my $file=pkgfile($package,$script);
+	my $service_script = generated_file($package, "${script}.service", 0);
+	my @generated_scripts = ("debian/$ext$script.debhelper", $service_script);
+	if ($script eq 'prerm' or $script eq 'postrm') {
+		@generated_scripts = reverse(@generated_scripts);
+	}
+	@generated_scripts = grep { -f } @generated_scripts;
 
 	if ($file ne '') {
-		if (-f "debian/$ext$script.debhelper") {
+		if (@generated_scripts) {
+			if ($dh{VERBOSE}) {
+				verbose_print('cp -f ' . escape_shell($file) . " $tmp/DEBIAN/$script");
+				verbose_print("perl -p -i -e s~#DEBHELPER#~qx{cat @generated_scripts}~eg\" $tmp/DEBIAN/$script");
+			}
 			# Add this into the script, where it has #DEBHELPER#
-			doit({ stdout => "$tmp/DEBIAN/$script" }, 'perl', '-pe',
-				 "s~#DEBHELPER#~qx{cat debian/$ext$script.debhelper}~eg", $file);
-		}
-		else {
+			my $text = _concat_slurp_script_files(@generated_scripts);
+			if (not $dh{NO_ACT}) {
+				open(my $out_fd, '>', "$tmp/DEBIAN/$script") or error("open($tmp/DEBIAN/$script) failed: $!");
+				open(my $in_fd, '<', $file) or error("open($file) failed: $!");
+				while (my $line = <$in_fd>) {
+					$line =~ s/#DEBHELPER#/$text/g;
+					print {$out_fd} $line;
+				}
+				close($in_fd);
+				close($out_fd) or error("close($tmp/DEBIAN/$script) failed: $!");
+			}
+		} else {
 			# Just get rid of any #DEBHELPER# in the script.
 			doit({ stdout => "$tmp/DEBIAN/$script" }, 'sed', 's/#DEBHELPER#//', $file);
 		}
 		reset_perm_and_owner('0755', "$tmp/DEBIAN/$script");
 	}
-	elsif ( -f "debian/$ext$script.debhelper" ) {
+	elsif (@generated_scripts) {
 		if ($dh{VERBOSE}) {
 			verbose_print(q{printf '#!/bin/sh\nset -e\n' > } . "$tmp/DEBIAN/$script");
-			verbose_print("cat debian/$ext$script.debhelper >> $tmp/DEBIAN/$script");
-		}
-		open(my $out_fd, '>', "$tmp/DEBIAN/$script") or error("open($tmp/DEBIAN/$script): $!");
-		print {$out_fd} "#!/bin/sh\n";
-		print {$out_fd} "set -e\n";
-		open(my $in_fd, '<', "debian/$ext$script.debhelper")
-			or error("open(debian/$ext$script.debhelper): $!");
-		while (my $line = <$in_fd>) {
-			print {$out_fd} $line;
-		}
-		close($in_fd);
-		close($out_fd) or error("close($tmp/DEBIAN/$script): $!");
+			verbose_print("cat @generated_scripts >> $tmp/DEBIAN/$script");
+		}
+		if (not $dh{NO_ACT}) {
+			open(my $out_fd, '>', "$tmp/DEBIAN/$script") or error("open($tmp/DEBIAN/$script): $!");
+			print {$out_fd} "#!/bin/sh\n";
+			print {$out_fd} "set -e\n";
+			for my $generated_script (@generated_scripts) {
+				open(my $in_fd, '<', $generated_script)
+					or error("open($generated_script) failed: $!");
+				while (my $line = <$in_fd>) {
+					print {$out_fd} $line;
+				}
+				close($in_fd);
+			}
+			close($out_fd) or error("close($tmp/DEBIAN/$script) failed: $!");
+		}
 		reset_perm_and_owner('0755', "$tmp/DEBIAN/$script");
 	}
 }
diff --git a/t/Test/DH.pm b/t/Test/DH.pm
index a5465c5..01fb3db 100644
--- a/t/Test/DH.pm
+++ b/t/Test/DH.pm
@@ -37,7 +37,7 @@ our @EXPORT = qw(
     each_compat_up_to_and_incl_subtest each_compat_subtest
     each_compat_from_and_above_subtest run_dh_tool
     uid_0_test_is_ok create_empty_file readlines
-    error
+    error find_script
 );
 
 our ($TEST_DH_COMPAT, $ROOT_OK, $ROOT_CMD);
@@ -198,4 +198,24 @@ sub readlines {
     return \@lines;
 }
 
+# In *inst order (find_script will shuffle them around for *rm order)
+my @SNIPPET_FILE_TEMPLATES = (
+	'debian/#PACKAGE#.#SCRIPT#.debhelper',
+	'debian/.debhelper/generated/#PACKAGE#/#SCRIPT#.service',
+);
+
+sub find_script {
+	my ($package, $script) = @_;
+	my @files;
+	for my $template (@SNIPPET_FILE_TEMPLATES) {
+		my $file = ($template =~ s/#PACKAGE#/$package/r);
+		$file =~ s/#SCRIPT#/$script/;
+		push(@files, $file) if -f $file;
+	}
+	if ($script eq 'postrm' or $script eq 'prerm') {
+		@files = reverse(@files);
+	}
+	return @files;
+}
+
 1;

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