【问题标题】:How can I automatically answer yes in python?如何在 python 中自动回答是?
【发布时间】:2017-04-10 07:11:21
【问题描述】:

假设我有一个稍后运行apt upgrade -y 的脚本,我需要为不同的包手动回答这些问题。我怎样才能自动做到这一点?

设置 virtualbox-guest-x11 (5.0.32-dfsg-0ubuntu1.16.04.2) ...

Configuration file '/etc/X11/Xsession.d/98vboxadd-xclient'
 ==> File on system created by you or by a script.
 ==> File also in package provided by package maintainer.
   What would you like to do about it ?  Your options are:
    Y or I  : install the package maintainer's version
    N or O  : keep your currently-installed version
      D     : show the differences between the versions
      Z     : start a shell to examine the situation
 The default action is to keep your current version.
*** 98vboxadd-xclient (Y/I/N/O/D/Z) [default=N] ?  conffile prompt detected: /etc/X11/Xsession.d/98vboxadd-xclient /etc/X11/Xsession.d/98vboxadd-xclient.dpkg-new

我的脚本能够检测到配置提示,但我应该如何自动回答是?

通过使用 python-apt api,我可以通过这个功能检测到提示

def conffile(self, current, new):
    print " conffile prompt detected: %s %s" % (current, new)

    """(Abstract) Called when a conffile question from dpkg is detected."""

【问题讨论】:

标签: python debian apt


【解决方案1】:

使用yes 命令:

yes Y | apt ...

【讨论】:

    【解决方案2】:

    由于(根据问题下的评论)您使用的是Python-APT,因此首先想到的方法是使用Configuration 类来设置conf['APT::Get::Assume-Yes'] = True

    【讨论】:

    • 我试过这样:conf = apt_pkg.config() conf['APT::Get::Assume-Yes'] = True我最终得到了apt_pkg.Configuration, object is not callable
    • 这可能是因为 apt_pkg.config 已经是一个实例。提到它的段落还说您不需要另一个实例。另外,我还没有测试过,所以 True 可能应该是一个字符串或其他东西。
    【解决方案3】:

    对于所有像我一样迷失的人,答案是:

    import apt_pkg
    
    apt_pkg.init_config()
    apt_pkg.config.set("DPkg::Options::", "--force-confnew")
    

    这将使 apt 自动接受新的配置文件。

    【讨论】:

      猜你喜欢
      • 2021-08-24
      • 2022-09-26
      • 1970-01-01
      • 2011-09-29
      • 2012-07-23
      • 2012-12-13
      • 2012-05-15
      • 2023-03-31
      • 1970-01-01
      相关资源
      最近更新 更多