Skip to content

Instantly share code, notes, and snippets.

@daaang
Created April 13, 2018 12:38
Show Gist options
  • Save daaang/8064e97a0bf5d4185a1451de33fd55d2 to your computer and use it in GitHub Desktop.
Save daaang/8064e97a0bf5d4185a1451de33fd55d2 to your computer and use it in GitHub Desktop.
Get a list of all packages installed on a system, triple-hashed on apt source url, release, and repo
require 'json'
# Collect the url, release, and repo immediately following the first
# line beginning with ` *** `, which indicates the installed instance of
# the package.
get_repo = %r{\n +\*\*\* +.*?\n +[0-9]+ +([^ ]+) +([^ /]+)/([^ ]+) .* Packages}m
# Get a list of all packages apt knows about.
packages = `apt list --installed | sed -e 's,/.*$,,'`.strip.split("\n")
all = {}
packages.each do |package|
get_repo =~ `apt-cache policy #{package}`
all[$1] = {} unless all.key? $1
all[$1][$2] = {} unless all[$1].key? $2
all[$1][$2][$3] = [] unless all[$1][$2].key? $3
all[$1][$2][$3] << package
end
puts JSON.pretty_generate(all)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment