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

guillem pushed a commit to branch master
in repository dpkg.

View the commit online:
https://git.dpkg.org/cgit/dpkg/dpkg.git/commit/?id=5a683899c5216dd565d21bb9b6592c1c6fde174b

commit 5a683899c5216dd565d21bb9b6592c1c6fde174b
Author: Guillem Jover <[email protected]>
AuthorDate: Wed May 20 18:31:07 2020 +0200

    scripts: Refactor shell error handling into a shell library
---
 debian/changelog                   |  1 +
 scripts/Makefile.am                |  4 ++
 scripts/dpkg-maintscript-helper.sh | 79 ++------------------------------
 scripts/sh/dpkg-error.sh           | 92 ++++++++++++++++++++++++++++++++++++++
 t/shellcheck.t                     |  1 +
 5 files changed, 102 insertions(+), 75 deletions(-)

diff --git a/debian/changelog b/debian/changelog
index 821ed85f5..bcdac083e 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -102,6 +102,7 @@ dpkg (1.20.1) UNRELEASED; urgency=medium
   * Code internals:
     - Use $() in shell or qx() in perl instead of ``.
     - dpkg-split: Switch part number variables from unsigned int to int.
+    - scripts: Refactor shell error handling into a shell library
   * Build system:
     - Handle .git being a plain file when getting the dpkg tree version.
     - Add debian/changelog as a Changes file to the CPAN distribution.
diff --git a/scripts/Makefile.am b/scripts/Makefile.am
index c71d902dc..b43a2e4ee 100644
--- a/scripts/Makefile.am
+++ b/scripts/Makefile.am
@@ -44,6 +44,9 @@ EXTRA_DIST = \
        $(test_scripts) \
        $(test_data)
 
+nobase_dist_pkgdata_DATA = \
+       sh/dpkg-error.sh \
+       # EOL
 
 CLEANFILES = \
        $(test_data_objects) \
@@ -147,6 +150,7 @@ do_perl_subst = $(AM_V_GEN) sed \
        -e "s:our \$$PROGVERSION = .*;:our \$$PROGVERSION = 
'$(PACKAGE_VERSION)';:"
 
 do_shell_subst = $(AM_V_GEN) sed \
+       -e "s:^PKGDATADIR=.*$$:PKGDATADIR='$(pkgdatadir)':" \
        -e "s:^version=['\"][^'\"]*[\"']:version=\"$(PACKAGE_VERSION)\":"
 
 SUFFIXES = .pl .sh
diff --git a/scripts/dpkg-maintscript-helper.sh 
b/scripts/dpkg-maintscript-helper.sh
index bba20d20a..e74ac66a6 100755
--- a/scripts/dpkg-maintscript-helper.sh
+++ b/scripts/dpkg-maintscript-helper.sh
@@ -545,74 +545,6 @@ symlink_match()
        [ "$(readlink -f "$SYMLINK")" = "$SYMLINK_TARGET" ]
 }
 
-# Standard ANSI colors and attributes.
-COLOR_NORMAL=''
-COLOR_RESET=''
-COLOR_BOLD=''
-COLOR_BLACK=''
-COLOR_RED=''
-COLOR_GREEN=''
-COLOR_YELLOW=''
-COLOR_BLUE=''
-COLOR_MAGENTA=''
-COLOR_CYAN=''
-COLOR_WHITE=''
-COLOR_BOLD_BLACK=''
-COLOR_BOLD_RED=''
-COLOR_BOLD_GREEN=''
-COLOR_BOLD_YELLOW=''
-COLOR_BOLD_BLUE=''
-COLOR_BOLD_MAGENTA=''
-COLOR_BOLD_CYAN=''
-COLOR_BOLD_WHITE=''
-
-setup_colors()
-{
-       : "${DPKG_COLORS=auto}"
-
-       case "$DPKG_COLORS" in
-       auto)
-               if [ -t 1 ]; then
-                       USE_COLORS=yes
-               else
-                       USE_COLORS=no
-               fi
-               ;;
-       always)
-               USE_COLORS=yes
-               ;;
-       *)
-               USE_COLORS=no
-               ;;
-       esac
-
-       if [ $USE_COLORS = yes ]; then
-               COLOR_PROG="$COLOR_BOLD"
-               COLOR_INFO="$COLOR_GREEN"
-               COLOR_NOTICE="$COLOR_YELLOW"
-               COLOR_WARN="$COLOR_BOLD_YELLOW"
-               COLOR_ERROR="$COLOR_BOLD_RED"
-       else
-               COLOR_RESET=""
-       fi
-       FMT_PROG="$COLOR_PROG$PROGNAME$COLOR_RESET"
-}
-
-debug() {
-       if [ -n "$DPKG_DEBUG" ]; then
-               echo "DEBUG: $FMT_PROG: $*" >&2
-       fi
-}
-
-error() {
-       echo "$FMT_PROG: ${COLOR_ERROR}error${COLOR_RESET}: $*" >&2
-       exit 1
-}
-
-warning() {
-       echo "$FMT_PROG: ${COLOR_WARN}warning${COLOR_RESET}: $*" >&2
-}
-
 usage() {
        cat <<END
 Usage: $PROGNAME <command> <parameter>... -- <maintainer-script-parameter>...
@@ -639,19 +571,16 @@ Commands:
 END
 }
 
-badusage() {
-       echo "$FMT_PROG: ${COLOR_ERROR}error${COLOR_RESET}: $1" >&2
-       echo >&2
-       echo "Use '$PROGNAME --help' for program usage information." >&2
-       exit 1
-}
-
 # Main code
 set -e
 
 PROGNAME=$(basename "$0")
 version="unknown"
 
+PKGDATADIR=scripts/sh
+
+. "$PKGDATADIR/dpkg-error.sh"
+
 setup_colors
 
 command="$1"
diff --git a/scripts/sh/dpkg-error.sh b/scripts/sh/dpkg-error.sh
new file mode 100644
index 000000000..9913151d8
--- /dev/null
+++ b/scripts/sh/dpkg-error.sh
@@ -0,0 +1,92 @@
+#!/bin/sh
+#
+# Copyright © 2010 Raphaël Hertzog <[email protected]>
+# Copyright © 2011-2015 Guillem Jover <[email protected]>
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <https://www.gnu.org/licenses/>.
+
+# Standard ANSI colors and attributes.
+COLOR_NORMAL=''
+COLOR_RESET=''
+COLOR_BOLD=''
+COLOR_BLACK=''
+COLOR_RED=''
+COLOR_GREEN=''
+COLOR_YELLOW=''
+COLOR_BLUE=''
+COLOR_MAGENTA=''
+COLOR_CYAN=''
+COLOR_WHITE=''
+COLOR_BOLD_BLACK=''
+COLOR_BOLD_RED=''
+COLOR_BOLD_GREEN=''
+COLOR_BOLD_YELLOW=''
+COLOR_BOLD_BLUE=''
+COLOR_BOLD_MAGENTA=''
+COLOR_BOLD_CYAN=''
+COLOR_BOLD_WHITE=''
+
+setup_colors()
+{
+  : "${DPKG_COLORS=auto}"
+
+  case "$DPKG_COLORS" in
+  auto)
+    if [ -t 1 ]; then
+      USE_COLORS=yes
+    else
+      USE_COLORS=no
+    fi
+    ;;
+  always)
+    USE_COLORS=yes
+    ;;
+  *)
+    USE_COLORS=no
+    ;;
+  esac
+
+  if [ $USE_COLORS = yes ]; then
+    COLOR_PROG="$COLOR_BOLD"
+    COLOR_INFO="$COLOR_GREEN"
+    COLOR_NOTICE="$COLOR_YELLOW"
+    COLOR_WARN="$COLOR_BOLD_YELLOW"
+    COLOR_ERROR="$COLOR_BOLD_RED"
+  else
+    COLOR_RESET=""
+  fi
+  FMT_PROG="$COLOR_PROG$PROGNAME$COLOR_RESET"
+}
+
+debug() {
+  if [ -n "$DPKG_DEBUG" ]; then
+    echo "DEBUG: $FMT_PROG: $*" >&2
+  fi
+}
+
+error() {
+  echo "$FMT_PROG: ${COLOR_ERROR}error${COLOR_RESET}: $*" >&2
+  exit 1
+}
+
+warning() {
+  echo "$FMT_PROG: ${COLOR_WARN}warning${COLOR_RESET}: $*" >&2
+}
+
+badusage() {
+  echo "$FMT_PROG: ${COLOR_ERROR}error${COLOR_RESET}: $1" >&2
+  echo >&2
+  echo "Use '$PROGNAME --help' for program usage information." >&2
+  exit 1
+}
diff --git a/t/shellcheck.t b/t/shellcheck.t
index a27b678fe..9428eac69 100644
--- a/t/shellcheck.t
+++ b/t/shellcheck.t
@@ -40,6 +40,7 @@ my @files = qw(
     scripts/dpkg-maintscript-helper.sh
 );
 my @shellcheck_opts = (
+    '--exclude=SC1090', # Allow non-constant source.
     '--exclude=SC2039', # Allow local keyword.
     '--exclude=SC2166', # Allow -a and -o.
     '--exclude=SC2034', # Allow unused variables for colors.

-- 
Dpkg.Org's dpkg

Reply via email to