I've written a small python script to check the total file size of a package's files under different main directories (/opt, /etc, /usr,...). This could be used to check the optification of a package.
If this could be useful to others, I can post it on the wiki or elsewhere, just point me to the right place.
Code:
#!/usr/bin/python
# deb package optification check script by Jeroen Wouters
import os, sys, subprocess
if len(sys.argv) != 2:
print "Usage: " + sys.argv[0] + " PKG_NAME"
print "Prints a summary of memory used by package PKG_NAME"
sys.exit(2)
def sizeof_fmt(num):
# Format filesizes in a human readable way
# From http://blogmag.net/blog/read/38/Print_human_readable_file_size
for x in ['bytes','KB','MB','GB','TB']:
if num < 1024.0:
return "%3.1f%s" % (num, x)
num /= 1024.0
# Get the command line output from dpkg and split it into filenames
dpkg_output = subprocess.Popen(["dpkg", "-L", sys.argv[1]],
stdout=subprocess.PIPE).communicate()[0]
dpkg_output = dpkg_output.splitlines()
# We're only looking at files
pkg_files = filter(os.path.isfile, dpkg_output)
# Look up under wich main paths the pkg has files (paths under /)
main_paths = set([pkg_file.split('/')[1] for pkg_file in pkg_files])
usage = dict([(path,0) for path in main_paths])
# Add the file size to the dictionary element corresponding to the files main path
for pkg_file in pkg_files:
filesize = os.path.getsize(pkg_file)
main_path = pkg_file.split('/')[1]
usage[main_path] += filesize
# Print the data
for x in usage:
print "/" + x + ": " + sizeof_fmt(usage[x])
Note: there is a difference between the size this script reports and what du reports. This is because file size is not equal to disk usage.
If this could be useful to others, I can post it on the wiki or elsewhere, just point me to the right place.