How to get the newly-installed version within a De

2019-04-21 09:49发布

问题:

Per the Debian Policy Manual, my postinst script is getting called at upgrade and configure time, as "postinst configure old-version", where old-version is the previously installed version (possibly null). I want to determine new-version, i.e. the version that is currently being configured (upgraded to).

The environment variable $DPKG_MAINTSCRIPT_PACKAGE contains the package name; there does not seem to be an equivalent _VERSION field. /var/lib/dpkg/status gets updated AFTER postinst runs, so I can't seem to parse it out of there, either.

Any ideas?

回答1:

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


回答2:

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;')


回答3:

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.


回答4:

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.



回答5:

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



回答6:

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


回答7:

Try this:

VERSION=`dpkg -s $DPKG_MAINTSCRIPT_PACKAGE | sed -n 's/^Version: //p'`