Skip to content

Instantly share code, notes, and snippets.

@matthiasroos
Forked from bmaupin/gist:1b59abbf30406ed1eb82
Last active April 25, 2020 19:21
Show Gist options
  • Select an option

  • Save matthiasroos/70744952887f1a2d14aeaed7f1e810a5 to your computer and use it in GitHub Desktop.

Select an option

Save matthiasroos/70744952887f1a2d14aeaed7f1e810a5 to your computer and use it in GitHub Desktop.
Parse installed packages from /var/log/apt/history.log
#!/usr/bin/python3
import re
import sys
fileAPTHISTORY = sys.argv[1]
INSTALL_STRING = ('Install: ', 'Upgrade: ')
REMOVE_STRING = ('Remove: ', 'Purge: ')
pattern = re.compile('([a-z0-9-.+]+?):[a-z0-9]+? \(.+?\),?')
installed_packages_set = set()
removed_packages_set = set()
with open(fileAPTHISTORY, 'r') as file_handle:
for line in file_handle:
line = line.strip()
if line.startswith(INSTALL_STRING):
installed_packages_set.update(pattern.findall(line))
elif line.startswith(REMOVE_STRING):
removed_packages_set.update(pattern.findall(line))
installed_packages = list(installed_packages_set)
removed_packages = list(removed_packages_set)
installed_packages.sort()
removed_packages.sort()
print(' '.join(installed_packages))
print(' '.join(removed_packages))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment