savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 1 | import json |
Alex | 3ebc563 | 2019-04-18 16:47:18 -0500 | [diff] [blame] | 2 | import subprocess |
| 3 | import sys |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 4 | from multiprocessing.dummy import Pool |
| 5 | |
| 6 | |
| 7 | def shell(command): |
| 8 | _ps = subprocess.Popen( |
| 9 | command.split(), |
| 10 | stdout=subprocess.PIPE |
| 11 | ).communicate()[0].decode() |
| 12 | |
| 13 | return _ps |
| 14 | |
| 15 | |
| 16 | def 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") |
| 34 | pkg_list = _list.splitlines() |
| 35 | pkg_list = [_pkg.split('/')[0] for _pkg in pkg_list[1:]] |
| 36 | |
| 37 | # threading pool |
| 38 | pool = Pool(10) |
| 39 | |
| 40 | result = pool.map(get_versions, pkg_list) |
| 41 | |
| 42 | # init pkg storage |
| 43 | pkgs = {} |
| 44 | for 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 | |
| 52 | buff = json.dumps(pkgs) |
| 53 | sys.stdout.write(buff) |