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