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