Hello World

File build-pkg-deb of Package build

#
# Debian dpkg specific functions.
#
################################################################
#
# Copyright (c) 1995-2014 SUSE Linux Products GmbH
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 or 3 as
# published by the Free Software Foundation.
#
# 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 (see the file COPYING); if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
#
################################################################

DEB_UNSAFE_IO=
DEB_NO_TRIGGERS=

#
# A wrapper around chroot to set the environment correctly for dpkg and
# pre/postinst scripts.
#
deb_chroot ()
{
    #
    # to workaround some version of fileutils that doesn't do a 'chdir /'
    # when doing a 'chroot /' call lets subshell and change dir manually
    #
    (
	cd $1 &&
	    DEBIAN_FRONTEND=noninteractive DEBIAN_PRIORITY=critical \
			   DEBCONF_NONINTERACTIVE_SEEN=true \
			   LC_ALL=C LANGUAGE=C LANG=C \
			   chroot $*
    )
}

deb_setup() {
    mkdir -p $BUILD_ROOT/var/lib/dpkg
    mkdir -p $BUILD_ROOT/var/log
    mkdir -p $BUILD_ROOT/etc/default
    :>> $BUILD_ROOT/var/lib/dpkg/status
    :>> $BUILD_ROOT/var/lib/dpkg/available
    :>> $BUILD_ROOT/var/log/dpkg.log
    :>> $BUILD_ROOT/etc/ld.so.conf

    # Install a policy so that services are not started. See
    # https://manpages.debian.org/unstable/init-system-helpers/invoke-rc.d.8.en.html
    # for details.
    mkdir -p $BUILD_ROOT/usr/sbin
    cat > $BUILD_ROOT/usr/sbin/policy-rc.d <<EOF
#!/bin/sh
exit 101
EOF
    chmod 755 $BUILD_ROOT/usr/sbin/policy-rc.d

    # Manpages database trigger takes a lot of time and is not useful in a build chroot
    mkdir -p $BUILD_ROOT/var/cache/debconf/
    cat >> $BUILD_ROOT/var/cache/debconf/config.dat <<EOF
Name: man-db/auto-update
Template: man-db/auto-update
Value: false
Owners: man-db
Flags: seen

EOF
}

pkg_initdb_deb() {
    deb_setup
    # force dpkg into database to make epoch test work
    if ! test "$BUILD_ROOT/.init_b_cache/rpms/dpkg.deb" -ef "$BUILD_ROOT/.init_b_cache/dpkg.deb" ; then
	cp --remove-destination $BUILD_ROOT/.init_b_cache/rpms/dpkg.deb $BUILD_ROOT/.init_b_cache/dpkg.deb || cleanup_and_exit 1
    fi
    DEB_UNSAFE_IO=
    chroot $BUILD_ROOT dpkg --force-unsafe-io --version >/dev/null 2>&1 && DEB_UNSAFE_IO="--force-unsafe-io"
    deb_chroot $BUILD_ROOT dpkg --install $DEB_UNSAFE_IO --force-depends .init_b_cache/dpkg.deb >/dev/null 2>&1
}

pkg_prepare_deb() {
    # test if dpkg knows --force-unsafe-io and --no-triggers
    DEB_UNSAFE_IO=
    DEB_NO_TRIGGERS=
    chroot $BUILD_ROOT dpkg --force-unsafe-io --version >/dev/null 2>&1 && DEB_UNSAFE_IO="--force-unsafe-io"
    chroot $BUILD_ROOT dpkg --no-triggers --version >/dev/null 2>&1 && DEB_NO_TRIGGERS="--no-triggers"
}

pkg_install_deb() {
    ( deb_chroot $BUILD_ROOT dpkg --install $DEB_UNSAFE_IO $DEB_NO_TRIGGERS --force-depends .init_b_cache/$PKG.deb 2>&1 || touch $BUILD_ROOT/exit ) | \
	perl -ne '$|=1;/^(Configuration file|Installing new config file|Selecting previously deselected|Selecting previously unselected|\(Reading database|Unpacking |Setting up|Creating config file|Preparing to replace dpkg|Preparing to unpack )/||/^$/||print'
    # ugly workaround for upstart system. some packages (procps) try
    # to start a service in their configure phase. As we don't have
    # a running upstart, we just link the start binary to /bin/true
    if test -e "$BUILD_ROOT/sbin/start"; then
	if test "$BUILD_ROOT/sbin/start" -ef "$BUILD_ROOT/sbin/initctl" ; then
	    echo "linking /sbin/start to /bin/true"
	    mv "$BUILD_ROOT/sbin/start" "$BUILD_ROOT/sbin/start.disabled"
	    ln -s "/bin/true" "$BUILD_ROOT/sbin/start"
	fi
    fi
    # another workaround, see bug bnc#733699
    rm -f "$BUILD_ROOT/var/run/init.upgraded"
}

pkg_erase_deb() {
    deb_chroot $BUILD_ROOT dpkg --purge --force-depends $PKG 2>&1 | {
      local retry
      while read line; do
          case "$line" in
              subprocess\ installed\ *script\ returned\ error\ exit\ status*)
                 chroot $BUILD_ROOT rm -f /var/lib/dpkg/info/$PKG.{pre,post}rm
                 retry=1
              ;;
              *) echo "$line" ;;
          esac
      done
      if test -n "$retry"; then
          echo "re-try deleting $PKG without post/pre remove scripts"
          deb_chroot $BUILD_ROOT dpkg --purge --force-depends $PKG 2>&1 || touch $BUILD_ROOT/exit
      fi
    } | perl -ne '$|=1;/^(\(Reading database|Removing |Purging configuration files for )/||/^$/||print'
}

pkg_cumulate_deb() {
    return 1
}

pkg_verify_installed_deb() {
    return 1
}

pkg_finalize_deb() {
    echo "configuring all installed packages..."
    # configure all packages after complete installation, not for each package like rpm does
    # We need to run this twice, because of cyclic dependencies as it does not succeed on most
    # debian based distros in the first attempt.
    if ! deb_chroot $BUILD_ROOT dpkg --configure --pending  2>&1; then
         echo "first configure attempt failed, trying again..."
         deb_chroot $BUILD_ROOT dpkg --configure --pending  2>&1 || cleanup_and_exit 1
    fi
}

pkg_preinstall_deb() {
    rm -rf control.tar.gz control.tar.xz control.tar.zst data.tar.gz data.tar.xz data.tar.zst
    ar x "$BUILD_INIT_CACHE/rpms/$PKG.deb" control.tar.gz control.tar.xz control.tar.zst data.tar.gz data.tar.xz data.tar.zst 2>/dev/null
    rm -rf "$BUILD_INIT_CACHE/scripts/control"
    mkdir "$BUILD_INIT_CACHE/scripts/control" || cleanup_and_exit 1
    :> "$BUILD_INIT_CACHE/scripts/control/.dummy"	# pin control dir
    if test -f "control.tar.gz" -a ! -L "control.tar.gz" ; then
	$TAR -C "$BUILD_INIT_CACHE/scripts/control" -z < control.tar.gz
    elif test -f "control.tar.xz" -a ! -L "control.tar.xz" ; then
	$TAR -C "$BUILD_INIT_CACHE/scripts/control" -J < control.tar.xz
    elif test -f "control.tar.zst" -a ! -L "control.tar.zst" ; then
	$TAR -C "$BUILD_INIT_CACHE/scripts/control" -J < control.tar.zst
    fi
    if test -f "data.tar.gz" -a ! -L "data.tar.gz" ; then
	$TAR -z < data.tar.gz
    elif test -f "data.tar.xz" -a ! -L "data.tar.xz" ; then
	$TAR -J < data.tar.xz
    elif test -f "data.tar.zst" -a ! -L "data.tar.zst" ; then
	$TAR -J < data.tar.zst
    fi
    rm -rf "$BUILD_INIT_CACHE/scripts/$PKG.pre" "$BUILD_INIT_CACHE/scripts/$PKG.post"
    if test -e "$BUILD_INIT_CACHE/scripts/$PKG.run" ; then
	test -f "$BUILD_INIT_CACHE/scripts/control/preinst" && mv "$BUILD_INIT_CACHE/scripts/control/preinst" "$BUILD_INIT_CACHE/scripts/$PKG.pre"
	test -f "$BUILD_INIT_CACHE/scripts/control/postinst" && mv "$BUILD_INIT_CACHE/scripts/control/postinst" "$BUILD_INIT_CACHE/scripts/$PKG.post"
    fi
    rm -rf "$BUILD_INIT_CACHE/scripts/control" control.tar.gz control.tar.xz control.tar.zst data.tar.gz data.tar.xz data.tar.zst
}

pkg_runscripts_deb() {
    if ! test -e $BUILD_ROOT/var/lib/dpkg/status ; then
	deb_setup
    fi
    if test -e "$BUILD_ROOT/.init_b_cache/scripts/$PKG.pre" ; then
	echo "running $PKG preinstall script"
	deb_chroot $BUILD_ROOT ".init_b_cache/scripts/$PKG.pre" install \
		   < /dev/null
	rm -f "$BUILD_ROOT/.init_b_cache/scripts/$PKG.pre"
    fi
    if test -e "$BUILD_ROOT/.init_b_cache/scripts/$PKG.post" ; then
	echo "running $PKG postinstall script"
	deb_chroot $BUILD_ROOT ".init_b_cache/scripts/$PKG.post" configure '' \
		   < /dev/null
	rm -f "$BUILD_ROOT/.init_b_cache/scripts/$PKG.post"
    fi
}

pkg_sysrootinstall_deb() {
    if test "x$1" = "x--prepare" ; then
	assert_dir_path "$BUILD_SYSROOT"
	chroot $BUILD_ROOT mkdir -p "$BUILD_SYSROOT"
	local target=$(queryconfig target --dist "$BUILD_DIST" --configdir "$CONFIG_DIR" --archpath "$BUILD_ARCH")
	if test -n "$target" ; then
	    local debarch=$(deb_chroot $BUILD_ROOT dpkg-architecture -q DEB_HOST_ARCH --host-type "$target")
	    if test -n "$debarch" ; then
		echo "Adding architecture $debarch"
		deb_chroot $BUILD_ROOT dpkg --add-architecture "$debarch"
	    fi
	fi
	return
    fi
    if test "x$1" = "x--finalize" ; then
	return
    fi

    ( deb_chroot $BUILD_ROOT dpkg --install $DEB_UNSAFE_IO $DEB_NO_TRIGGERS --force-depends .init_b_cache/$PKG.deb 2>&1 || touch $BUILD_ROOT/exit ) | \
	perl -ne '$|=1;/^(Configuration file|Installing new config file|Selecting previously deselected|Selecting previously unselected|\(Reading database|Unpacking |Setting up|Creating config file|Preparing to replace dpkg|Preparing to unpack )/||/^$/||print'
}


# Local Variables:
# mode: Shell-script
# End: