Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Python查找debian包信息_Python_Linux_Ubuntu_Debian_Package - Fatal编程技术网

如何使用Python查找debian包信息

如何使用Python查找debian包信息,python,linux,ubuntu,debian,package,Python,Linux,Ubuntu,Debian,Package,我想使用python以编程方式查找debian包的最新可用版本。我环顾四周,但找不到合适的关键字来消除所有的噪音“python”“parse”“package”“index”恰巧翻过来 有人知道加载和解析这样一个包索引的方法吗? 下面是一个示例的URL,我无法用yaml或json对其进行解析: 我已经看过了,但我不确定如何从在线索引中找到我需要的内容 谢谢 您可以使用模块运行apt缓存策略: 输出: python: Installed: 2.7.5-5ubuntu3 Candidate

我想使用python以编程方式查找debian包的最新可用版本。我环顾四周,但找不到合适的关键字来消除所有的噪音“python”“parse”“package”“index”恰巧翻过来

有人知道加载和解析这样一个包索引的方法吗?
下面是一个示例的URL,我无法用yaml或json对其进行解析:

我已经看过了,但我不确定如何从在线索引中找到我需要的内容

谢谢

您可以使用模块运行apt缓存策略:

输出:

python:
  Installed: 2.7.5-5ubuntu3
  Candidate: 2.7.5-5ubuntu3
  Version table:
 *** 2.7.5-5ubuntu3 0
        500 http://ie.archive.ubuntu.com/ubuntu/ trusty/main amd64 Packages
        100 /var/lib/dpkg/status
您可以通过任何应用程序获取使用功能的信息:

from subprocess import check_output,CalledProcessError
def apt_cache(app):
    try:
        return check_output(["apt-cache", "policy",app])
    except CalledProcessError as e:
        return e.output

print(apt_cache("python"))
或者使用*args并运行您喜欢的任何命令:

from subprocess import check_output,CalledProcessError
def apt_cache(*args):
    try:
        return check_output(args)
    except CalledProcessError as e:
        return e.output

print(apt_cache("apt-cache","showpkg ","python"))
如果要分析输出,可以使用re:

import  re
from subprocess import check_output,CalledProcessError
def apt_cache(*args):
    try:
        out = check_output(args)
        m = re.search("Candidate:.*",out)
        return m.group() if m else "No match"
    except CalledProcessError as e:
        return e.output

print(apt_cache("apt-cache","policy","python"))
Candidate: 2.7.5-5ubuntu3
或获取已安装的和候选的:

def apt_cache(*args):
    try:
        out = check_output(args)
        m = re.findall("Candidate:.*|Installed:.*",out)
        return "{}\n{}".format(*m) if m else "No match"
    except CalledProcessError as e:
        return e.output
 print(apt_cache("apt-cache","policy","python"))
输出:

Installed: 2.7.5-5ubuntu3
Candidate: 2.7.5-5ubuntu3
Installed: 2.7.5-5ubuntu3
Candidate: 2.7.5-5ubuntu3