You can use the subprocess module to run apt-cache policy <app>:
from subprocess import check_output
out = check_output(["apt-cache", "policy","python"])
print(out)
Output:
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
You can pass whatever app you are trying to get the info for using a fucntion:
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"))
Or use *args and run whatever command you like:
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"))
If you want to parse the output you can use 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
Or to get the installed and candidate:
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"))
Output:
Installed: 2.7.5-5ubuntu3
Candidate: 2.7.5-5ubuntu3