Skip to content

Instantly share code, notes, and snippets.

@JuniorPolegato
Created May 29, 2013 16:07
Show Gist options
  • Save JuniorPolegato/5671481 to your computer and use it in GitHub Desktop.
Save JuniorPolegato/5671481 to your computer and use it in GitHub Desktop.
This program gets a list of packages from repositories files in /var/lib/apt/lists and a list of installed packages. Then, list the packages/versions which not in files with stable in your name.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
Author: Junior Polegato
Date: 29 May 2013
Description: This program gets a list of packages from repositories
files in /var/lib/apt/lists and a list of installed
packages. Then, list the packages/versions which not in
files with stable in your name.
'''
import os
import subprocess
DIR = '/var/lib/apt/lists'
def read_packages_list(print_info = True):
repos_files = [p for p in os.listdir(DIR) if p[-9:] == "_Packages"]
packages = {}
for repo_file in repos_files:
if print_info: print repo_file
packs = open(DIR + "/" + repo_file).read().strip().split('\n\n')
repo_packages = 0
for pack_lines in [p.split('\n') for p in packs]:
package = {}
for line in pack_lines:
if not line: continue
if line[0] != ' ' and ': ' in line:
info, descr = line.split(': ', 1)
package[info] = descr
elif info in package:
package[info] += line
if 'Package' in package:
repo_packages += 1
package_name = package['Package']
if 'Version' in package:
package_version = package['Version']
else:
package_version = 'Unknown'
if package_name in packages:
if package_version in packages[package_name]:
packages[package_name][
package_version].append(repo_file)
else:
packages[package_name][
package_version] = [repo_file]
else:
packages[package_name] = {
package_version: [repo_file]}
if print_info: print ' \-->', repo_packages, 'package(s)'
return packages
def installed_list():
dpkg_query = subprocess.Popen(
"dpkg-query --show"
" --showformat='${Package} ${Version} ${Status}\n'",
shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
list_packages = dpkg_query.stdout.read().strip().split('\n')
retval = dpkg_query.wait()
return dict(package.split(' ', 2)[:2] for package in list_packages)
if __name__ == "__main__":
packages = read_packages_list()
installeds = installed_list()
for p, v in installeds.items():
if p in packages:
if v in packages[p]:
if 'stable' not in repr(packages[p][v]):
print p, v, packages[p][v]
else:
print p, v, 'version not in', packages[p].keys()
else:
print p, v, 'package not in repos'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment