How to get the newly-installed version within a De

2019-04-21 09:25发布

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?

7条回答
地球回转人心会变
2楼-- · 2019-04-21 10:04

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
查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-04-21 10:07

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;')
查看更多
Melony?
4楼-- · 2019-04-21 10:15

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
查看更多
淡お忘
5楼-- · 2019-04-21 10:16

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.

查看更多
Root(大扎)
6楼-- · 2019-04-21 10:17
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.
查看更多
爱情/是我丢掉的垃圾
7楼-- · 2019-04-21 10:22

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

查看更多
登录 后发表回答