[Pkg-asv-commits] r19 - trunk

ahoenen-guest at alioth.debian.org ahoenen-guest at alioth.debian.org
Mon Jan 14 21:06:39 UTC 2008


Author: ahoenen-guest
Date: 2008-01-14 21:06:39 +0000 (Mon, 14 Jan 2008)
New Revision: 19

Modified:
   trunk/apt-show-versions
Log:
- Fix handling of missing official suites: previous implementation ignored
  missing official suites after last existing release.
- Output optimizations:
  * Sort packages (an external sort pipe does not work exactly, for option -a
    it does not work at all).
  * When printing all releases (-a), enforce the tabular layout.
- Complete string constant usage for keys of nested $ipackages/$apackages hash
  structures.
- Minor issues:
  * Append missing line endings to warn and die messages (to avoid the technical
    automatic message completion).
  * Remove a superfluous "make emacs perl-mode happy" comment:
    ' characters are balanced without it.


Modified: trunk/apt-show-versions
===================================================================
--- trunk/apt-show-versions	2008-01-13 14:51:52 UTC (rev 18)
+++ trunk/apt-show-versions	2008-01-14 21:06:39 UTC (rev 19)
@@ -48,15 +48,18 @@
 my $cache = AptPkg::Cache->new;
 my $policy = $cache->policy;
 
-my $VERSION;
-$VERSION ='0.12';
+my $ASV_VERSION;
+$ASV_VERSION ='0.12';
 
 # Provide some constants (to avoid redundant literals).
 my $CODENAME = 'Codename';
 my $NAME     = 'Name';
+my $PACKAGE  = 'Package';
 my $RELEASE  = 'Release';
+my $STATUS   = 'Status';
 my $SUITE    = 'Suite';
 my $UNKNOWN  = 'unknown';
+my $VERSION  = 'Version';
 
 # process commandline parameters
 my %opts;
@@ -91,7 +94,7 @@
 
 if (exists $opts{'help'}) {
     print <<EOF;
-Apt-Show-Versions v.$VERSION (c) Christoph Martin
+Apt-Show-Versions v.$ASV_VERSION (c) Christoph Martin
 
 Usage:
  apt-show-versions         shows available versions of installed packages.
@@ -111,7 +114,6 @@
  -v|--verbose               Verbose messages.
  -h|--help                  Print this help.
 EOF
-    # ' (make emacs perl-mode happy)
     exit;
 }
 
@@ -147,10 +149,10 @@
 }
 # Test also to be sure $filescachefile is not corrupt and returns a ref to it
 if (!-e $filescachefile or -M $list_dir < -M $filescachefile or !ref($filesref)) {
-    opendir(DIR, $list_dir) or die "Can't opendir $list_dir: $!";
+    opendir(DIR, $list_dir) or die "Can't opendir $list_dir: $!\n";
     @files = map { $list_dir . $_} grep /Packages$/, readdir(DIR);
     ($< == 0) and (store(\@files, $filescachefile) or
-        warn "Can't write $filescachefile");
+        warn "Can't write $filescachefile\n");
 }
 unless (scalar @files > 0) {die "Error: No information about packages! (Maybe no deb entries?)\n"};
 closedir DIR ;
@@ -165,7 +167,7 @@
 if (!-e $ipackagescachefile or -M $status_file < -M $ipackagescachefile or !ref($ipackages)) {
     ($ipackages, undef) = parse_file ($status_file, 1);
     ($< == 0) and (store($ipackages, $ipackagescachefile) or
-        warn "Can't write $ipackagescachefile");
+        warn "Can't write $ipackagescachefile\n");
 }
 
 # Get available packages list from cache if possible
@@ -184,9 +186,7 @@
 # - Keys:   Known official suite names
 # - Values: Order index
 my %official_suites;
-foreach (0 .. $#official_suites) {
-    $official_suites{$official_suites[$_]} = $_ + 1;
-}
+$official_suites{$official_suites[$_]} = $_ foreach (0 .. $#official_suites);
 
 # Get available package information out of all Packages files
 foreach (@files) {
@@ -211,19 +211,18 @@
     my $key = $opts{'package'};
 
     print_package ($key);
-    exit;
 }
 elsif (%pkg_names) {
     print_package($_) foreach (sort keys %pkg_names);
-    exit;
 }
-
-# print info for all packages or packages matching regex
-foreach my $key (keys %$ipackages) {
-    next if (exists $opts{'package'} &&
-        exists $opts{'regex'} &&
-        !($key =~ m/$opts{'package'}/));
-    print_package ($key);
+else {
+    # print info for all packages or packages matching regex
+    foreach my $key (sort keys %$ipackages) {
+        next if (exists $opts{'package'} &&
+                 exists $opts{'regex'} &&
+                 !($key =~ m/$opts{'package'}/));
+        print_package ($key);
+    }
 }
 
 # print uptodate or up/downgradeable status of package depending on
@@ -267,61 +266,80 @@
 
     # print more information if required
     if ($opts{'allversions'}) {
-        if ($ipackages->{$package}->{'Package'}) {
-            print $ipackages->{$package}->{'Package'}, "\t";
-            unless ($ipackages->{$package}->{'Status'} =~ /not-installed/ ||
-                $ipackages->{$package}->{'Status'} =~ /config-files/) {
-                print "$ipackages->{$package}->{'Version'}\t";
+        if ($ipackages->{$package}->{$PACKAGE}) {
+            print "$ipackages->{$package}->{$PACKAGE} ";
+            unless ($ipackages->{$package}->{$STATUS} =~ /not-installed/ ||
+                $ipackages->{$package}->{$STATUS} =~ /config-files/) {
+                print "$ipackages->{$package}->{$VERSION} ";
             }
-            print "$ipackages->{$package}->{'Status'}\n";
+            print "$ipackages->{$package}->{$STATUS}\n";
         } else {
             print "Not installed\n";
         }
 
-     # The base of the two indexes $official_handled_idx and $official_idx is 1:
-     # first value of %official_suites.
-        my $official_handled_idx = 0;  # Initialize: nothing handled yet.
+        # Index to @official_suites: Next official suite to mention if missing.
+        my $official_idx = 0;
+        # To guarantee tabular printing of the package's releases some further
+        # variables are needed:
+        my @print_info = ();
+        my $max_package_len = 0;
+        my $max_version_len = 0;
+        # Print preparation loop
         foreach my $pkg (@pkg_releases) {
             # First handle missing official suites to be listed before current
             # release.
-            my $official_idx =
-              $official_suites{$releasenames{$pkg->{$RELEASE}}{$SUITE}};
-            if ($official_idx) {
-                foreach ($official_handled_idx .. $official_idx - 2) {
+            my $cur_idx =
+                $official_suites{$releasenames{$pkg->{$RELEASE}}{$SUITE}};
+            if (defined $cur_idx) {
+                # Current release is an official one:
+                # List prepending missing suites.
+                foreach ($official_idx .. $cur_idx - 1) {
                     if ($used_suites{$official_suites[$_]}) {
-                        print "No $official_suites[$_] version\n";
+                        push @print_info, "No $official_suites[$_] version\n";
                     }
                 }
-                $official_handled_idx = $official_idx;
+                # All official suites including current one are handled.
+                $official_idx = $cur_idx + 1;
             }
-            # Then list current release.
-            print $pkg->{'Package'}, "\t";
-            print $pkg->{'Version'}, "\t";
-            print $releasenames{$pkg->{$RELEASE}}{$NAME}, "\n";
+            # Then handle current release.
+            push @print_info, {$PACKAGE => $pkg->{$PACKAGE},
+                               $VERSION => $pkg->{$VERSION},
+                               $NAME => $releasenames{$pkg->{$RELEASE}}{$NAME}};
+            $max_package_len = &max(length($pkg->{$PACKAGE}), $max_package_len);
+            $max_version_len = &max(length($pkg->{$VERSION}), $max_version_len);
         }
-        # Handle missing official suites for packages without any available
-        # releases.
-        unless (@pkg_releases) {
-            foreach (0 .. $#official_suites) {
-                if ($used_suites{$official_suites[$_]}) {
-                    print "No $official_suites[$_] version\n";
-                }
+        # Finally handle missing official suites after last existing release.
+        foreach ($official_idx .. $#official_suites) {
+            if ($used_suites{$official_suites[$_]}) {
+                push @print_info, "No $official_suites[$_] version\n";
             }
         }
+        # Print loop
+        foreach my $print_info (@print_info) {
+            if (ref $print_info) {
+                printf("%*s %*s %s\n",
+                       -$max_package_len, $print_info->{$PACKAGE},
+                       -$max_version_len, $print_info->{$VERSION},
+                       $print_info->{$NAME});
+            }
+            else {
+                print $print_info;
+            }
+        }
     }
 
-    my $iversion = $ipackages->{$package}->{'Version'};
+    my $iversion = $ipackages->{$package}->{$VERSION};
 
     # print info about upgrade status (only if package is installed)
 
-    if (($ipackages->{$package}->{'Version'}) &&
-        (!($ipackages->{$package}->{'Status'} =~ /config-files/))) {
+    if (($ipackages->{$package}->{$VERSION}) &&
+        (!($ipackages->{$package}->{$STATUS} =~ /config-files/))) {
         # Reorder package version structures to prefer the default release.
         @pkg_releases = &reorder_pkg_releases(@pkg_releases);
         my $found = 0;
         my $aversion = 0;
         foreach (@pkg_releases) {
-            my $version = $_->{'Version'};
+            my $version = $_->{$VERSION};
             if ($version) {
                 $found = print_version($releasenames{$_->{$RELEASE}}{$NAME}, $package,
                     $iversion, $version);
@@ -334,7 +352,7 @@
             # than all available versions.
             my $newer_indic = 1;
             foreach (@pkg_releases) {
-                my $cmp_version = $_->{'Version'};
+                my $cmp_version = $_->{$VERSION};
                 if ($cmp_version and
                     $vs->compare($iversion, $cmp_version) <= 0)
                 {
@@ -356,12 +374,12 @@
         print "$package not installed\n";
     }
 
-    #    my $sversion = $apackages->{$package}{"stable"}->{'Version'};
-    #    my $tversion = $apackages->{$package}{"testing"}->{'Version'};
-    #    my $uversion = $apackages->{$package}{"unstable"}->{'Version'};
+    #    my $sversion = $apackages->{$package}{"stable"}->{$VERSION};
+    #    my $tversion = $apackages->{$package}{"testing"}->{$VERSION};
+    #    my $uversion = $apackages->{$package}{"unstable"}->{$VERSION};
 
     # print info about upgrade status (only if package is installed)
-    #    if ($ipackages->{$package}->{'Version'}) {
+    #    if ($ipackages->{$package}->{$VERSION}) {
     #   print_version("stable", $package, $iversion, $sversion) ||
     #       print_version("testing", $package, $iversion, $tversion) ||
     #           print_version("unstable", $package, $iversion, $uversion) ||
@@ -384,7 +402,7 @@
     my ($key, $value, $package, $packages);
 
     my $release = &determine_pkgfile_release($file);
-    open FILE, $file or die "Can't open file $file: $!";
+    open FILE, $file or die "Can't open file $file: $!\n";
     if ($opts{'verbose'}) {print "Parsing $file...";};
     while (<FILE>) {
         if (/^$/){
@@ -394,19 +412,19 @@
                 # if we did not specify a package or pattern
                 # only include installed packages
                 unless ((!exists $opts{'package'} && !%pkg_names &&
-                        ($package->{'Status'} =~ /not-installed/ ||
-                            $package->{'Status'} =~ /config-files/ ||
+                        ($package->{$STATUS} =~ /not-installed/ ||
+                            $package->{$STATUS} =~ /config-files/ ||
                             # don't print holded packages if requested
-                            ($opts{'nohold'} && $package->{'Status'} =~ /hold/)))) {
-                    $packages->{ $package->{'Package'}} = $package;
+                            ($opts{'nohold'} && $package->{$STATUS} =~ /hold/)))) {
+                    $packages->{ $package->{$PACKAGE}} = $package;
                 }
             }
             else {
-                if (!defined $packages->{$package->{'Package'}} or
-                    $vs->compare($packages->{$package->{'Package'}}{'Version'},
-                        $package->{'Version'}) < 0) {
+                if (!defined $packages->{$package->{$PACKAGE}} or
+                    $vs->compare($packages->{$package->{$PACKAGE}}{$VERSION},
+                        $package->{$VERSION}) < 0) {
                     $package->{$RELEASE} = $release;
-                    $packages->{$package->{'Package'}} = $package;
+                    $packages->{$package->{$PACKAGE}} = $package;
                 }
             }
             undef $package;
@@ -518,6 +536,13 @@
 }
 
 ################################################################################
+# Return the numerically biger of the two specified arguments.
+################################################################################
+sub max {
+    return ($_[0] > $_[1]) ? $_[0] : $_[1];
+}
+
+################################################################################
 # Reorder package releases in a way that within the releases of the same
 # version number the default release gets placed first.
 ################################################################################
@@ -537,8 +562,8 @@
             my $rel_key = $releases[$idx]->{$RELEASE};
             if (defined $move_idx) {
                 # There exists a move candidate.
-                if ($releases[$idx]->{'Version'} eq
-                    $releases[$move_idx]->{'Version'}) {
+                if ($releases[$idx]->{$VERSION} eq
+                    $releases[$move_idx]->{$VERSION}) {
                     # Current release is of same version as move candidate.
                     if ($releasenames{$rel_key}{$SUITE} eq $default_release or
                         $releasenames{$rel_key}{$CODENAME}
@@ -575,7 +600,7 @@
 # 3) Release name
 ################################################################################
 sub sort_pkg_releases {
-    my $cmp_versions = $vs->compare($a->{'Version'}, $b->{'Version'});
+    my $cmp_versions = $vs->compare($a->{$VERSION}, $b->{$VERSION});
     return $cmp_versions if ($cmp_versions);
     my $cmp_suites = (&suite_idx($releasenames{$a->{$RELEASE}}{$SUITE}) <=>
                       &suite_idx($releasenames{$b->{$RELEASE}}{$SUITE}));
@@ -589,7 +614,7 @@
 # Unofficial suites are sorted last.
 ################################################################################
 sub suite_idx {
-    return($official_suites{$_[0]} || length(keys(%official_suites)) + 1);
+    return($official_suites{$_[0]} || $#official_suites + 1);
 }
 
 # script documentation (POD style)




More information about the Pkg-asv-commits mailing list