是否可以使用python apt模块添加ppa?

5 投票
2 回答
1137 浏览
提问于 2025-04-16 23:33

我需要通过一个Python脚本在远程服务器上添加一个ppa(个人包存档)。我想做的事情在bash(一个命令行工具)中的写法是:

$ add-apt-repository ppa:user/ppa-name

我想它的样子可能是这样的:

import apt
cache = apt.Cache()
# ?? add the ppa here ??
cache.update()
cache.open(None)
cache['package_from_ppa'].mark_install()
cache.upgrade()
cache.commit()

不过我在apt模块的源代码中没有找到很多关于添加软件源的内容。

2 个回答

0

我注意到提问者没有得到他想要的答案,所以这里有一个解决方案。

import aptsources.sourceslist as s
repo = ('deb', 'http://ppa.launchpad.net/danielrichter2007/grub-customizer/ubuntu', 'xenial', ['main'])
sources = s.SourcesList()
sources.add(repo)
sources.save()
5

以下内容摘自当前(在11.04 natty版本中)add-apt-repository的代码:

from softwareproperties.SoftwareProperties import SoftwareProperties
sp = SoftwareProperties()
sp.add_source_from_line(ppa_name)
sp.sourceslist.save()

当然,你应该添加一些错误检查之类的东西……可以这样查看当前安装的版本:

less `which add-apt-repository`

撰写回答