Procházet zdrojové kódy

dpkg-maintscript-helper: add "supports" command

With this command a maintainer script can verify whether a given command
is supported by dpkg-maintscript-helper before calling it. Thanks to this
he can avoid a pre-dependency on dpkg.

Also improve the error message output when the command is unknown to
suggest upgrading dpkg as a possible way to fix the problem encountered.
Raphaël Hertzog před 16 roky
rodič
revize
60a060e6f5

+ 2 - 0
debian/changelog

@@ -26,6 +26,8 @@ dpkg (1.15.7.2) UNRELEASED; urgency=low
   * Rename /usr/lib/dpkg/maintscript-helper into
     /usr/bin/dpkg-maintscript-helper, it is a public interface even if working
     around known limitations.
+  * Add "supports" command to dpkg-maintscript-helper to ensure the wanted
+    command is supported before calling it.
 
   [ Guillem Jover ]
   * Add powerpcspe support to ostable and triplettable.

+ 19 - 0
man/dpkg-maintscript-helper.1

@@ -100,6 +100,25 @@ to \fInewconffile\fP if \fIoldconffile\fP is still available. On
 abort\-upgrade/abort\-install, the \fBpostrm\fP renames
 \fIoldconffile\fP\fB.dpkg\-remove\fP back to \fIoldconffile\fP if required.
 .
+.SH INTEGRATION IN PACKAGES
+.P
+Given that \fBdpkg\-maintscript\-helper\fP is used in the \fBpreinst\fP,
+using it unconditionnaly requires a pre-dependency to ensure that the
+required version of dpkg has been configured before. The required version
+depends on the command used, for \fBrm_conffile\fP and \fBmv_conffile\fP
+it is 1.15.7.2:
+.P
+    Pre-Depends: dpkg (>= 1.15.7.2)
+.P
+But in many cases the operation done by the program is not critical for
+the package, and instead of using a pre-dependency we can call the
+program only if we know that the required command is supported by
+the currently installed dpkg:
+.P
+    if dpkg-maintscript-helper supports <command>; then
+        dpkg-maintscript-helper <command> ...
+    fi
+.
 .SH AUTHORS
 Copyright \(co 2010 Rapha\[:e]l Hertzog
 .br

+ 33 - 1
scripts/dpkg-maintscript-helper.sh

@@ -208,16 +208,24 @@ debug() {
 }
 
 error() {
-	echo "ERROR: $PROGNAME: $1" >&2
+	echo "$PROGNAME: error: $1" >&2
 	exit 1
 }
 
+warning() {
+	echo "$PROGNAME: warning: $1" >&2
+}
+
 usage() {
 	cat <<END
 Syntax: $0 <command> [<parameters>] -- <maintainer script parameters>
 
 Commands and parameters:
 
+  supports <command>
+	Returns 0 (success) if the given command is supported, 1
+	otherwise.
+
   rm_conffile <conffile> <last-version> [<package>]
 	Remove obsolete conffile.
 	Must be called in preinst, postinst and postrm.
@@ -240,6 +248,25 @@ command="$1"
 shift
 
 case "$command" in
+supports)
+	case "$1" in
+	rm_conffile|mv_conffile)
+		code=0
+		;;
+	*)
+		code=1
+		;;
+	esac
+	if [ -z "$DPKG_MAINTSCRIPT_NAME" ]; then
+		warning "environment variable DPKG_MAINTSCRIPT_NAME missing"
+		code=1
+	fi
+	if [ -z "$DPKG_MAINTSCRIPT_PACKAGE" ]; then
+		warning "environment variable DPKG_MAINTSCRIPT_PACKAGE missing"
+		code=1
+	fi
+	exit $code
+	;;
 rm_conffile)
 	rm_conffile "$@"
 	;;
@@ -263,6 +290,11 @@ mv_conffile)
 	END
 	;;
 *)
+	cat >&2 <<-END
+	$PROGNAME: error: command $command is unknown
+	Hint: upgrading dpkg to a newer version might help.
+
+	END
 	usage
 	exit 1
 esac