Python Script: Clean up out of date packages in pkgsrc.

Having updated some packages on my NetBSD system, I had some duplicate copies of the built binaries in my pkgsrc tree. This of course is pretty much just a waste of disk space so I went about writing a script to clean up the extra package tarballs.

I made a script that would examine the list of files and find ones with the same package name (with the version info chopped off). It would then compare the files by date and the delete the older files. The main problem with this script is packages like apache and samba. Those packages have multiple packages within pkgsrc that are simply different versions of the same software. Unfortunately these different versions share the same package name as far as the package management system is concerned, which means this script could delete a binary package file you wanted to keep. For example apache-1.3x versus apache-2.x.x.

Fortunately I wouldn’t imagine many people installing more than one copy of packages like samba and apache, but it is something to be aware of as it does affect some other packages as well. So far it hasn’t been a problem for me. I’ve noted some packages like python are fine as the version has become a part of the package name (eg. python27 for the 2.7.x series of python). So it would be a good idea to have a quick look in your package directory.

After removing the extra tarball files, the script goes and checks all the symlinks in the packages tree to make sure they are still good, and removes the link if it is broken.

Note you will need the common.py file in addition to this one (clean-packages.py) for the common utility functions. This was in my last post containing the build script.

#!/usr/pkg/bin/python2.6 -u

# this script is meant to find and remove the binary packages that are old and superseeded by new ones.
# We need to also go through the category diretories and remove the symlinks for the corresponding
# packages.

from common import *
import os

#global variables

pkg_path = "/usr/pkgsrc/packages/"

# functions and procedures

def processFiles(path):
    """Works out which files need to be removed and then preforms the removal"""
    print "processing files in",path
    #first we should check if this is a directory!
    if not os.path.isdir(path):
        print "  Not a directory!"
    #now get the list of files and check for duplicates.
    rml = [] #the list of files to remove
    l = os.listdir(path)
    for x in l:
        sx = stripPkgName(x)
        for y in l:
            sy = stripPkgName(y)
            if sx==sy and not x==y:
                print "  Found duplicate package",x,"and",y
                # compare both the files and mark one for removal
                tx = os.stat(path+"/"+x).st_mtime
                ty = os.stat(path+"/"+y).st_mtime
                if tx>ty:
                    print "    ",y,"is older, marking for removal"
                    if not y in rml:
    print "Files to be removed:",rml
    for x in rml:
        print "  Removing",path+'/'+x

def checkSymlinks(path):
    """ checks symlinks and see if they are valid."""
    print "Checking symlinks in",path
    #first we should check if this is a directory!
    if not os.path.isdir(path):
        print "  Not a directory!"
    # Begin checking the symlinks
    l = os.listdir(path)
    for x in l:
        if os.path.islink(path+'/'+x):
            if not os.path.exists(os.readlink(path+'/'+x)):
                print "  Broken symlink ",x
# main code!

# get a list of the contents of the package store
list = os.listdir(pkg_path)
    print "You don't have the CVS ignore and CVS directory present!"


for i in list:

0 Responses to “Python Script: Clean up out of date packages in pkgsrc.”

  1. Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

Blogs I Follow

Enter your email address to follow this blog and receive notifications of new posts by email.


Mister G Kids

A daily comic about real stuff little kids say in school. By Matt Gajdoš

Random Battles: my life long level grind

completing every RPG, ever.

Gough's Tech Zone

Reversing the mindless enslavement of humans by technology.

Retrocosm's Vintage Computing, Tech & Scale RC Blog

Random mutterings on retro computing, old technology, some new, plus radio controlled scale modelling.


retro computing and gaming plus a little more

Retrocomputing with 90's SPARC

21st-Century computing, the hard way

%d bloggers like this: