Perl script: get_sections_from_packages

#!/usr/bin/perl -w

use strict;

my %sections = ();

my $wanted = get_wanted_sections();

# Clearly, you'll want to change this to the path to the Packages file
# that you download.
my $dist = $ARGV[0] || 'woody';
my $file = "/home/mkns/data/Packages-$dist";

open( FILE, $file ) or die "Can't open file $file: $!";
while (  ) {
  chomp;
  next unless $_ =~ /^Section/;
  my $section = substr( $_, 9 );
  $sections{$section}++;
}
close( FILE );

print_all_sections();

foreach my $key ( sort keys %sections ) {

  # Skip any Sections that we want
  next if exists $wanted->{$key};

  print "  --exclude-deb-section=$key \\ \n";
}

sub get_wanted_sections {

  # We're going to build a simple hash which contains keys that are
  # the names of Sections we don't want to download.  Let's define it
  # here.
  my %wanted = ();

  # Put in the following array any Sections that you *know* you don't
  # want to have on your mirror

  my @wanted = qw( base devel editors interpreters libs net shells
                   text utils web );
  foreach ( @wanted ) {
    $wanted{$_} = 1;
  }

  # Return a reference to the hash rather than the hash itself, to
  # save copying the data.
  return \%wanted;
}

sub print_all_sections {
  print "-" x 26, "\n";
  printf( "%-20s %5s\n", "Section", "Times" );
  print "-" x 26, "\n";
  foreach my $key ( sort keys %sections ) {
    printf( "%-20s %-4d\n", $key, $sections{$key} );
  }
}

One thought on “Perl script: get_sections_from_packages

Leave a Reply