cleanup_packages.pl 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 getProcessorArch {
  8. open(my $fh, '-|', 'uname', '-p')
  9. or die "Unable to get processor arch\n";
  10. my $arch = <$fh>;
  11. close($fh);
  12. chomp $arch;
  13. return $arch;
  14. }
  15. sub getInstalledPackages {
  16. my %packages = ();
  17. open(my $fh, '-|', '/usr/sbin/pkg_info')
  18. or die "Unable to read package list: $!\n";
  19. while(<$fh>) {
  20. s/\s+.*//x;
  21. chomp;
  22. $packages{$_} = 1;
  23. }
  24. close($fh);
  25. return \%packages;
  26. }
  27. sub getAvailablePackages {
  28. my $arch = shift;
  29. my $subdir = shift;
  30. my $directory = "$portsdir/packages/$arch/$subdir";
  31. return {} if (! -d "$directory");
  32. opendir(my $dh, $directory)
  33. or die "Unable to open package directory: $!\n";
  34. my %packages = map { s/\.tgz$//rx => 1 } grep { !/^\./x } readdir $dh;
  35. closedir $dh;
  36. return \%packages;
  37. }
  38. sub printNotInstalledPackages {
  39. my $arch = shift;
  40. my $installed = shift;
  41. my $available = shift;
  42. my $dir = shift;
  43. for my $pkg (sort keys %{$available}) {
  44. if (! exists $installed->{$pkg}) {
  45. print "rm $portsdir/packages/$arch/$dir/$pkg.tgz\n";
  46. }
  47. }
  48. return;
  49. }
  50. my $arch = getProcessorArch();
  51. print '#! /bin/sh' . "\n";
  52. my $installed = getInstalledPackages();
  53. for my $dir ('all', 'cdrom', 'ftp') {
  54. my $available = getAvailablePackages($arch, $dir);
  55. printNotInstalledPackages($arch, $installed, $available, $dir);
  56. }