blob: be02aa832049208c5dc0d6d41b652a92e9598058 [file] [log] [blame]
savex4448e132018-04-25 15:51:14 +02001import sys
2import subprocess
3import json
4
5from multiprocessing.dummy import Pool
6
7
8def shell(command):
9 _ps = subprocess.Popen(
10 command.split(),
11 stdout=subprocess.PIPE
12 ).communicate()[0].decode()
13
14 return _ps
15
16
17def get_versions(pkg):
18 # get the info for the package
19 _pkg_info = shell('apt-cache policy ' + pkg)
20
21 _installed = 'none'
22 _candidate = 'none'
23
24 # extract the installed and candidate
25 for line in _pkg_info.splitlines():
26 if line.find("Installed") > 0:
27 _installed = line.split(':', 1)[1].strip()
28 elif line.find("Candidate") > 0:
29 _candidate = line.split(':', 1)[1].strip()
30 return [pkg, _installed, _candidate, _pkg_info]
31
32
33# get list of packages
34_list = shell("apt list --installed")
35pkg_list = _list.splitlines()
36pkg_list = [_pkg.split('/')[0] for _pkg in pkg_list[1:]]
37
38# threading pool
39pool = Pool(10)
40
41result = pool.map(get_versions, pkg_list)
42
43# init pkg storage
44pkgs = {}
45for res in result:
46 _pkg = res[0]
47 if _pkg not in pkgs:
48 pkgs[_pkg] = {}
49 pkgs[_pkg]['installed'] = res[1]
50 pkgs[_pkg]['candidate'] = res[2]
51 pkgs[_pkg]['raw'] = res[3]
52
53buff = json.dumps(pkgs)
54sys.stdout.write(buff)