How to get the newly-installed version within a Debian postinst script?

流过昼夜 提交于 2019-12-04 01:17:18

This is the best method I have found to resolve this issue is to use a place-holder variable in your .postinst (or other control files):

case "$1" in
    configure)
        new_version="__NEW_VERSION__"
        # Do something interesting interesting with $new_version...
        ;;
    abort-upgrade|abort-remove|abort-deconfigure)
        # Do nothing
        ;;
    *)
        echo "Unrecognized postinst argument '$1'"
        ;;
esac

Then in debian/rules, replace the placeholder variable with the proper version number at build time:

# Must not depend on anything. This is to be called by
# binary-arch/binary-indep in another 'make' thread.
binary-common:
    dh_testdir
    dh_testroot
    dh_lintian
    < ... snip ... >

    # Replace __NEW_VERSION__ with the actual new version in any control files
    for pkg in $$(dh_listpackages -i); do \
        sed -i -e 's/__NEW_VERSION__/$(shell $(SHELL) debian/gen_deb_version)/' debian/$$pkg/DEBIAN/*; \
    done

    # Note dh_builddeb *must* come after the above code
    dh_builddeb

The resulting .postinst snippet, found in debian/<package-name>/DEBIAN/postinst, will look like:

case "$1" in
    configure)
        new_version="1.2.3"
        # Do something interesting interesting with $new_version...
        ;;
    abort-upgrade|abort-remove|abort-deconfigure)
        # Do nothing
        ;;
    *)
        echo "Unrecognized postinst argument '$1'"
        ;;
esac
VERSION=$(zless /usr/share/doc/$DPKG_MAINTSCRIPT_PACKAGE/changelog* \
     | dpkg-parsechangelog -l- -SVersion')

Advantages over other solutions here:

  • Works regardless of whether changelog is compressed or not
  • Uses dpkg's changelog parser instead of regular expressions, awk, etc.

I use the following somewhat dirty command in the postinst script:

NewVersion=$(zcat /usr/share/doc/$DPKG_MAINTSCRIPT_PACKAGE/changelog.gz | \
  head -1 | perl -ne '$_=~ /.*\((.*)\).*/; print $1;')

Add the following to the debian/rules:

override_dh_installdeb:
    dh_installdeb
    for pkg in $$(dh_listpackages -i); do \
        sed -i -e 's/__DEB_VERSION__/$(DEB_VERSION)/' debian/$$pkg/DEBIAN/*; \
    done

It will replace any occurrence of __DEB_VERSION__ in your debian scripts with the version number.

By the time postinst is run, all the package files have been installed and dpkg's data base has been updated, so you can get the just installed version with:

dpkg-query --show --showformat='${Version}' packagename

Why can't you hard-code the version into the postinst script at packaging time?

Try this:

VERSION=`dpkg -s $DPKG_MAINTSCRIPT_PACKAGE | sed -n 's/^Version: //p'`
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!