【问题标题】:How to get the newly-installed version within a Debian postinst script?如何在 Debian postinst 脚本中获取新安装的版本?
【发布时间】:2009-04-03 19:23:14
【问题描述】:

根据Debian Policy Manual,我的 postinst 脚本在升级和配置时被调用,如“postinst configure old-version”,其中 old-version 是以前安装的版本(可能为空)。我想确定 new-version,即当前正在配置(升级到)的版本。

环境变量$DPKG_MAINTSCRIPT_PACKAGE包含包名;似乎没有等效的 _VERSION 字段。 /var/lib/dpkg/status 在 postinst 运行后更新,所以我似乎也无法从那里解析它。

有什么想法吗?

【问题讨论】:

    标签: shell debian packages


    【解决方案1】:

    我发现解决此问题的最佳方法是在您的 .postinst(或其他控制文件)中使用占位符变量:

    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
    

    然后在debian/rules 中,在构建时将占位符变量替换为正确的版本号:

    # 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
    

    debian/&lt;package-name&gt;/DEBIAN/postinst 中找到的.postinst sn-p 将如下所示:

    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
    

    【讨论】:

    • 对于 Python distutils 包,我需要将版本添加到 postinst。我在 setup.py 中添加了一个钩子,以便在构建阶段通过 postinst 文件进行搜索和替换。
    【解决方案2】:
    VERSION=$(zless /usr/share/doc/$DPKG_MAINTSCRIPT_PACKAGE/changelog* \
         | dpkg-parsechangelog -l- -SVersion')
    

    与其他解决方案相比的优势:

    • 无论变更日志是否压缩都可以使用
    • 使用 dpkg 的变更日志解析器代替正则表达式、awk 等。

    【讨论】:

    • 很好,但这需要在你的包中添加对 dpkg-dev 的依赖。 (我认为在你的例子中你的}' 太多了)
    【解决方案3】:

    将以下内容添加到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
    

    它将用版本号替换您的 debian 脚本中出现的任何 __DEB_VERSION__

    【讨论】:

    • $(DEB_VERSION) 似乎不再起作用了。可以将上面代码sn-p中的替换为$(shell dpkg-parsechangelog --count 1 -S version)
    【解决方案4】:

    我在 postinst 脚本中使用了以下有点脏的命令:

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

    【讨论】:

    • 丑陋,但它对我有用。他们肯定不会让本应直截了当的事情变得容易,对吧!
    • 对于未反映在更改日志中的 ~ 版本,这也会失败。
    【解决方案5】:

    到 postinst 运行的时候,所有的包文件都已经安装好了,dpkg 的数据库也已经更新了,所以你可以得到刚刚安装的版本:

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

    【讨论】:

      【解决方案6】:

      为什么不能在打包时将版本硬编码到 postinst 脚本中?

      【讨论】:

      • 在脚本中硬编码需要每次都更改它...在我看来这不是一个好主意。
      【解决方案7】:

      试试这个:

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

      【讨论】:

        猜你喜欢
        • 2021-12-18
        • 1970-01-01
        • 2011-06-24
        • 1970-01-01
        • 2017-04-04
        • 1970-01-01
        • 1970-01-01
        • 2016-03-22
        • 2015-12-01
        相关资源
        最近更新 更多