blob: ae7683308758cd24928447c22023cb54aa7798dc [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(),
Alex9a4ad212020-10-01 18:04:25 -050010 stdout=subprocess.PIPE,
11 stderr=subprocess.PIPE
savex4448e132018-04-25 15:51:14 +020012 ).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)
Alex9a4ad212020-10-01 18:04:25 -050054if len(sys.argv) > 1 and sys.argv[1] == 'stdout':
55 sys.stdout.write(buff)
56else:
57 print(buff)