cleanup_packages.pl 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #! /usr/bin/perl
  2. # $Id: cleanup_packages.pl,v 1.1 2011/06/20 17:39:34 markus Exp $
  3. # vi: set tabstop=8 noexpandtab shiftwidth=8:
  4. use strict;
  5. use warnings;
  6. my $portsdir = $ENV{PORTSDIR} || '/usr/ports/';
  7. sub getInstalledPackages () {
  8. my %packages = ();
  9. open(my $fh, 'pkg_info |')
  10. or die $!;
  11. while(<$fh>) {
  12. s/[[:space:]]+.*//;
  13. chomp;
  14. $packages{$_} = 1;
  15. }
  16. close($fh);
  17. return \%packages;
  18. }
  19. sub getAvailablePackages ($) {
  20. my $subdir = shift;
  21. my $arch = `uname -m`;
  22. chomp $arch;
  23. my $directory = "$portsdir/packages/$arch/$subdir";
  24. my %packages = ();
  25. return \%packages if (! -d "$directory");
  26. open(my $fh, "ls $directory |")
  27. or die $!;
  28. while (<$fh>) {
  29. $_ =~ s/\.tgz$//;
  30. chomp;
  31. $packages{$_} = 1;
  32. }
  33. close($fh);
  34. return \%packages;
  35. }
  36. sub getNotInstalledPackages ($$$) {
  37. my $installed = shift;
  38. my $available = shift;
  39. my $dir = shift;
  40. my $arch = `uname -m`;
  41. chomp $arch;
  42. for my $pkg (sort keys %$available) {
  43. if (! exists $installed->{$pkg}) {
  44. print "rm $portsdir/packages/$arch/$dir/$pkg.tgz\n";
  45. }
  46. }
  47. }
  48. print '#! /bin/sh' . "\n";
  49. my $installed = getInstalledPackages();
  50. for my $dir ('all', 'cdrom', 'ftp') {
  51. my $available = getAvailablePackages($dir);
  52. getNotInstalledPackages($installed, $available, $dir);
  53. }