1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
|
#!/usr/bin/python3
# Copyright (C) 2013 Kristoffer Gronlund <kgronlund@suse.com>
# See COPYING for license information.
import sys
import json
import subprocess
def run(cmd):
proc = subprocess.Popen(cmd,
shell=False,
stdin=None,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
out, err = proc.communicate(None)
proc.wait()
return proc.returncode, out.decode('utf-8'), err.decode('utf-8')
def package_data(pkg):
"""
Gathers version and release information about a package.
"""
rc, out, err = run(['/usr/bin/dpkg', '--status', pkg])
if rc == 0:
data = {'name': pkg}
for line in out.split('\n'):
info = line.split(':', 1)
if len(info) == 2:
data[info[0].strip().lower()] = info[1].strip()
return data
else:
return {'name': pkg, 'error': "package not installed"}
def main():
data = [package_data(pkg) for pkg in sys.argv[1:]]
print(json.dumps(data))
main()
|