--- apt-installed-status/trunk/apt-installed-status 2014/07/04 13:52:46 336 +++ apt-installed-status/trunk/apt-installed-status 2014/07/04 15:50:28 337 @@ -24,7 +24,7 @@ use Getopt::Std; my $execname = 'apt-installed-status'; -my $version = '1.0'; +my $version = '1.1'; ########################## @@ -54,6 +54,11 @@ # 'multiarch': string: package multi-arch option (not always available) my %package_lists; +# When only the names of the package lists is asked for, we fill this variable. +# archives: array +# counter: string: name of the archive +my @archive_names; + # Read a package list file and save it into global variable %package_lists. # Parameter 1: $file: File name (within /var/lib/apt/lists) of the package list @@ -157,11 +162,61 @@ find_all_packages(); } +# Determine which package archives are available. +# Parameters: None +# Returns: Nothing +# Side effects: Fills in global variabale @archive_names +sub init_names +{ + opendir(DIR, '/var/lib/apt/lists') or die "Can't opendir: $!"; + while (defined(my $file = readdir(DIR))) { + push (@archive_names,$file) if $file =~ /_Packages$/ + } + closedir(DIR); +} + ############### # OUTPUT DATA # ############### +sub print_archive_names +{ + print join("\n", sort @archive_names)."\n"; +} + +sub print_archive_names_only +{ + my ($archives) = @_; + my %archives; + foreach my $archive (split (/,/,$archives)) { + $archives{$archive} = 1; + } + print join("\n", sort (grep { exists $archives{$_} } @archive_names))."\n"; +} + +sub print_archive_names_only_regexp +{ + my ($archives_regexp) = @_; + print join("\n", sort (grep /$archives_regexp/,@archive_names))."\n"; +} + +sub print_archive_names_except +{ + my ($archives) = @_; + my %archives; + foreach my $archive (split (/,/,$archives)) { + $archives{$archive} = 1; + } + print join("\n", sort (grep { ! exists $archives{$_} } @archive_names))."\n"; +} + +sub print_archive_names_except_regexp +{ + my ($archives_regexp) = @_; + print join("\n", sort (grep !/$archives_regexp/,@archive_names))."\n"; +} + # Go through the list of all packages and call a subroutine on each installed # package. # Parameter 1: $sub: Reference to the subroutine to call @@ -341,6 +396,7 @@ print STDERR " -X ARCHREGEXP: except: list all packages that are not in archives\n"; print STDERR " that match the regular expression\n"; print STDERR " -l: long: print archives too\n"; + print STDERR " -L: list: print archives that are selected instead of packages\n"; print STDERR "Default: print all packages and sources\n"; } @@ -360,12 +416,14 @@ our $VERSION=$version; my %options; -if (! getopts('hVola:A:x:X:',\%options)) +if (! getopts('hVoLla:A:x:X:',\%options)) { print_help(); exit 1; } my $long=0; +my $names_only=0; + if ($options{'h'}) { print_help(); exit 0; @@ -374,7 +432,14 @@ print_version(); exit 0; } -init(); +if ($options{'L'}) { + $names_only=1; +} +if ($names_only) { + init_names(); +} else { + init(); +} if ($options{'o'}) { print_orphans(); exit 0; @@ -383,22 +448,42 @@ $long = 1; } if ($options{a}) { - print_archives_only($options{a},$long); + if ($names_only) { + print_archive_names_only($options{a}); + } else { + print_archives_only($options{a},$long); + } exit 0; } if ($options{A}) { - print_archives_only_regexp($options{A},$long); + if ($names_only) { + print_archive_names_only_regexp($options{A}); + } else { + print_archives_only_regexp($options{A},$long); + } exit 0; } if ($options{x}) { - print_archives_except($options{x},$long); + if ($names_only) { + print_archive_names_except($options{x}); + } else { + print_archives_except($options{x},$long); + } exit 0; } if ($options{X}) { - print_archives_except_regexp($options{X},$long); + if ($names_only) { + print_archive_names_except($options{X}); + } else { + print_archives_except_regexp($options{X},$long); + } exit 0; } -print_all; +if ($names_only) { + print_archive_names() +} else { + print_all(); +} # vim:sw=2:ft=perl:expandtab: