Sun Jan 4 00:44:38 2009 UTC ()
This is a simple script that will synchronise the content of your ${DISTDIR}
directory with currently installed packages.

This script will delete any ${DISTFILE} in ${DISTDIR} that does not currently
have an installed package as an owner.

If you play with packages a fair bit and download some just to have a play with
and delete the package later your ${DISTDIR} can end up with a lot of orphaned
${DISTFILES}.  While lintpkgsrc will help you remove outdated ${DISTFILES} it
does not do any corealtion with installed packakges which is the gap this
script aims to fill.

With "lintpkgsrc -or && pkg_distinst --delete" you can and up with a fairly
lean and current ${DISTDIR}.

Status:

Vendor Tag:	TNF
Release Tags:	pkgsrc-base


(adrianp)
diff -r0 -r1.1.1.1 pkgsrc/pkgtools/pkg_distinst/DESCR
diff -r0 -r1.1.1.1 pkgsrc/pkgtools/pkg_distinst/Makefile
diff -r0 -r1.1.1.1 pkgsrc/pkgtools/pkg_distinst/MESSAGE
diff -r0 -r1.1.1.1 pkgsrc/pkgtools/pkg_distinst/PLIST
diff -r0 -r1.1.1.1 pkgsrc/pkgtools/pkg_distinst/files/pkg_distinst.pl

File Added: pkgsrc/pkgtools/pkg_distinst/DESCR
This is a simple script that will synchronise the content of your ${DISTDIR}
directory with currently installed packages.

This script will delete any ${DISTFILE} in ${DISTDIR} that does not currently
have an installed package as an owner.

If you play with packages a fair bit and download some just to have a play with
and delete the package later your ${DISTDIR} can end up with a lot of orphaned
${DISTFILES}.  While lintpkgsrc will help you remove outdated ${DISTFILES} it
does not do any corealtion with installed packakges which is the gap this
script aims to fill.

With "lintpkgsrc -or && pkg_distinst --delete" you can and up with a fairly
lean and current ${DISTDIR}.

File Added: pkgsrc/pkgtools/pkg_distinst/Makefile
# $NetBSD: Makefile,v 1.1.1.1 2009/01/04 00:44:38 adrianp Exp $

DISTNAME=	pkg_distinst-0.01
CATEGORIES=	pkgtools
MASTER_SITES=	# empty
DISTFILES=	# empty

MAINTAINER=	adrianp@NetBSD.org
COMMENT=	Remove any distfiles not belonging to an installed package

PKG_DESTDIR_SUPPORT=	user-destdir

WRKSRC=		${WRKDIR}
USE_TOOLS+=	perl:run
NO_CHECKSUM=	yes
NO_BUILD=	yes
USE_LANGUAGES=	# none
AUTO_MKDIRS=	yes

SUBST_CLASSES+=		di
SUBST_STAGE.di=		do-configure
SUBST_FILES.di=		pkg_distinst.pl
SUBST_SED.di=		-e 's,@PERL5@,${PERL5},g'
SUBST_SED.di+=		-e 's,@PKGSRCDIR@,${PKGSRCDIR},g'
SUBST_SED.di+=		-e 's,@MAKE@,${MAKE},g'
SUBST_MESSAGE.di=	Fixing script for installation.

do-extract:
	cd ${FILESDIR} && cp pkg_distinst.pl ${WRKSRC}/

do-install:
	${INSTALL_SCRIPT} ${WRKSRC}/pkg_distinst.pl \
		${DESTDIR}${PREFIX}/bin/pkg_distinst

.include "../../mk/bsd.pkg.mk"

File Added: pkgsrc/pkgtools/pkg_distinst/MESSAGE
===========================================================================
$NetBSD: MESSAGE,v 1.1.1.1 2009/01/04 00:44:38 adrianp Exp $

WARNING:
This script will quite hapily delete *any* file in ${DISTDIR}
(or --distdir) that does not belong to an installed package.  The only
exception to this is that it tries to avoid deleting some CVS files.
Please be sure that you are not storing anything else in ${DISTDIR}
(or --distdir) that you may want to keep.
===========================================================================

File Added: pkgsrc/pkgtools/pkg_distinst/PLIST
@comment $NetBSD: PLIST,v 1.1.1.1 2009/01/04 00:44:38 adrianp Exp $
bin/pkg_distinst

File Added: pkgsrc/pkgtools/pkg_distinst/files/pkg_distinst.pl
#!@PERL5@ -w

# $NetBSD: pkg_distinst.pl,v 1.1.1.1 2009/01/04 00:44:38 adrianp Exp $

use Data::Dumper;
use File::Basename;
use File::Find;
use Getopt::Long;
use strict;

my ($pkg_info, $pkgsrc, @pkg, @installed, $ipkg, $loc, @distfiles, $line);
my (@pdistfiles, %dupe, $distdir, @downloaded, $dl, @dltmp, $dir, $file);
my (@orphan, $found, $delete, $help);

# set some defaults
#
$pkgsrc = "";
$distdir = "";
$delete = 0;
$found = 0;
$help = 0;

GetOptions (	'pkgsrc=s' => \$pkgsrc, 
		'distdir=s' => \$distdir,
		'delete' => \$delete,
		'help' => \$help,
	);

if ($help == 1) {
	help();
	exit 0;
}

# check everything we need exists
#
if ($pkgsrc eq "") {
	$pkgsrc = "@PKGSRCDIR@";
}

if (! -d "$pkgsrc/pkgtools/pkglint") {
	print "ERROR: Unable to find $pkgsrc: $!\n";
	exit 1;
}

chdir("$pkgsrc/pkgtools/pkglint");
$pkg_info = `@MAKE@ show-var VARNAME=PKG_INFO_CMD`;
chomp($pkg_info);

if ($distdir eq "") {
	$distdir = `@MAKE@ show-var VARNAME=DISTDIR`;
	chomp($distdir);
}

if (! -d "$distdir") {
	print "ERROR: Unable to find $distdir: $!\n";
	exit 1;
}

# get a list of installed packages
#
open(PKG_INFO, "$pkg_info|");

while(<PKG_INFO>) {
	$line = $_;
	@pkg = split / /, $line;
	push(@installed, $pkg[0]);
}

close(PKG_INFO);

# get a list of distfiles for the installed packages
#
print "Gathering DISTFILES for installed packages";

foreach $ipkg (@installed) {

	$loc = `$pkg_info -Q PKGPATH $ipkg`;
	chomp($loc);

	if (! -d "$pkgsrc/$loc") {
		print "WARNING: Unable to find package $loc: $!\n";
	} else {
		chdir("$pkgsrc/$loc");

		$line = `@MAKE@ show-var VARNAME=DISTFILES`;
		chomp($line);

		if ($line ne "") {
			@pdistfiles = split / /, $line;
			push(@distfiles, @pdistfiles);
		}

		print ".";
	}
}

print "done\n";

# remove duplicates
#
%dupe = map { $_, 1 } @distfiles;
@distfiles = keys %dupe;

# get a list of downloaded DISTFILES in DISTDIR
#
find(\&dfile, $distdir);

# trim .cvsignore and CVS/*
#
foreach $dl (@downloaded) {
	$dir = dirname($dl);
	$file = basename($dir);
	if (-f $dl && $dl ne "$distdir/.cvsignore" && $file ne "CVS") {
		push(@dltmp, $dl);
	}
}

@downloaded = @dltmp;

# figure out what belongs and what doesn't
#
foreach $dl (@downloaded) {
	$file = basename($dl);

	foreach my $ipkg (@distfiles) {
		if ($file eq $ipkg) {
			$found = 1;
			last;
		}
	}

	if ($found == 0) {
		push(@orphan, $dl);
	} else {
		$found = 0;
	}
}

# Print the list of orphaned files
#
foreach $file (@orphan) {
	print "$file\n";
	if ($delete == 1) {
		unlink($file);
	}
}

exit 0;

#
# subs follow
#

sub dfile {
	push(@downloaded, $File::Find::name);
}

sub help {
	print "pkg_distinst [--pkgsrc <pkgsrc dir> | --distdir <distfiles dir> | --delete | --help]\n";
	print "\t --pkgsrc <pkgsrc dir>: Specify a pkgsrc directory to use.\n";
	print "\t\t This will default to \${PKGSRCDIR}\n";
	print "\t --distdir <distfiles dir>: Specify a distfiles directory to use.\n"; 
	print "\t\t This will default to \${DISTDIR}\n";
	print "\t --delete: Delete any orphaned files found.\n";
	print "\t\t Default is to display the list of files.\n";
	print "\t --help: You\'re looking at it\n";
}