How-To: Identifying Package Sources

This guide will show you how to identify the sources of installed packages on your system using a small Python script. This script parses the output of apt-cache policy to determine where each package came from.

First, create a new Python file (e.g., check.py) and copy the following script into it:

#!/usr/bin/env python

# Should be written with python-apt
# but for now parse the output of apt-cache policy

import os
import re
command    = "apt-cache policy $(dpkg -l | awk '/ii/ {print $2}' )"
stream     = os.popen(command)
content    = stream.readlines()
getOrigin  = False
pkgList    = []

# Parse the output generated by apt-cache
for s in content:
  if(not s.startswith(' ')):
    pkg = type('', (), {})()   # Create an empty object 
    pkg.name = s[:-2]          # Remove trailing ':\n'
  elif(getOrigin):
    pkg.origin = re.split('\s+', s)[2]
    pkg.suite = re.split('\s+', s)[3]
    pkgList.append(pkg)
    getOrigin = False
  elif(s.startswith(' ***')):
    pkg.version = re.split('\s+', s)[2]
    getOrigin = True

# Display the list
for pkg in pkgList:
  print(pkg.name    + '\t'\
      + pkg.version + '\t'\
      + pkg.origin  + '\t'\
      + pkg.suite)

After saving the file, you need to make it executable. Open a terminal and run the following command:

chmod +x check.py

Execute the script to see the output, which will list the installed packages along with their versions, origins, and suites:

./check.py

You can filter the output using grep to search for specific information. For example, to search for packages from a particular origin, you can use:

./check.py | grep 'package-name'

This topic was automatically closed after 24 hours. New replies are no longer allowed.

Mastodon