Wednesday, October 24, 2007

MacPorts cleanup

Well, GrandPerspective, a nice open source tool that shows your disk space use with rectangles for each file depending on the file size, was showing me a great deal of used space in tgz files in my /opt/local/var/macports/distfiles/ folder. After looking around, for some reason, my MacPorts keeps plenty of useless stuff after installing packages. There must be a way to get it to clean it up automatically, but I had to clean up my previous installs (after all, I wasn't looking at GrandPerspective solely for its aesthetics).
port clean --all installed
was failing at a port and aborted (I may have been playing with the ports folder a bit much) without reaching the other, less fortunately alphabetised, ports.

Using the arguably misguided patch approach, I wrote a perl script to solve my immediate problem rather than the underlying one. It individually cleans each port, continuing if some fail. Apparently, MacPorts has a safeguard for ports that need their folder, as it refused to clean them without adding an argument. The script below can be used safely, and saved me a few hundreds of "Mebibytes" of space.

Just save the script to a file, make it executable, and sudo it.

#!/usr/bin/perl -w
my @items = `port echo installed`;
my %clean;
foreach $line (@items)
{
if ($line =~ /([a-zA-Z0-9_-]+).*?/)
{
system("port", "clean", "--all", $1) unless ($clean{$1});
$clean{$1} = 1;
}
}