home *** CD-ROM | disk | FTP | other *** search
/ PC World 2003 March / PCWorld_2003-03_cd.bin / Software / Topware / activeperl / ActivePerl / Perl / bin / ppm2 < prev    next >
Encoding:
Text File  |  2002-11-22  |  34.1 KB  |  1,011 lines

  1. #!/usr/bin/perl
  2.  
  3. use Getopt::Long;
  4. use File::Basename;
  5. use Config;
  6. use strict;
  7.  
  8. use PPM;
  9.  
  10. $PPM::VERSION = "2.2.0";
  11.  
  12. my %help;
  13.  
  14. # mapping of POD sections to command topics
  15. my %topic = (
  16.     'Error Recovery' => 'genconfig',
  17.     'Installing'     => 'install',
  18.     'Querying'       => 'query',
  19.     'Removing'       => 'remove',
  20.     'Searching'      => 'search',
  21.     'Summarizing'    => 'summary',
  22.     'Verifying'      => 'verify',
  23.     'Synopsis'       => 'usage',
  24.     'Options'        => 'set',
  25. );
  26.  
  27. $help{'help'} = <<'EOT';
  28. Commands:
  29.     exit              - leave the program.
  30.     help [command]    - prints this screen, or help on 'command'.
  31.     install PACKAGES  - installs specified PACKAGES.
  32.     quit              - leave the program.
  33.     query [options]   - query information about installed packages.
  34.     remove PACKAGES   - removes the specified PACKAGES from the system.
  35.     search [options]  - search information about available packages.
  36.     set [options]     - set/display current options.
  37.     verify [options]  - verifies current install is up to date.
  38.     version           - displays PPM version number
  39.  
  40. EOT
  41.  
  42. # Build the rest of the online help from the POD
  43. $/ = "\n=";
  44. while (<DATA>) {
  45.     next unless my ($topic,$text) = /^(?:item|head[12]) ([^\n]+)\n\n(.*)=/s;
  46.     next unless $topic{$topic};
  47.     ($help{$topic{$topic}} = "\n$text"); # =~ s/\n *([^\n])/\n    $1/sg;
  48. }
  49. $/ = "\n";
  50.  
  51. # Need to do this here, because the user's config file is probably
  52. # hosed.
  53. if ($#ARGV == 0 && $ARGV[0] eq 'genconfig') {
  54.     &genconfig;
  55.     exit 0;
  56. }
  57.  
  58. if ($#ARGV == 0 && $ARGV[0] eq 'getconfig') {
  59.     print $PPM::PPMdat;
  60.     exit 0;
  61. }
  62.  
  63. my %options = PPM::GetPPMOptions();
  64. my $location;
  65.  
  66. my $moremsg = "[Press return to continue or 'q' to quit] ";
  67. my $interactive = 0;
  68.  
  69. my %repositories = PPM::ListOfRepositories();
  70.  
  71. my $prefix_pattern = $^O eq "MSWin32" ? '(--|-|\+|/)' : '(--|-|\+)';
  72.  
  73. $PPM::PPMShell = 1;
  74.  
  75. Getopt::Long::Configure("prefix_pattern=$prefix_pattern");
  76.  
  77. if ($#ARGV == -1 || ($#ARGV == 0 && $ARGV[0] =~ /^${prefix_pattern}location/)) {
  78.     my $prompt = 'PPM> ';
  79.     $interactive = 1;
  80.     GetOptions("location=s" => \$location);
  81.  
  82.     print "PPM interactive shell ($PPM::VERSION) - type 'help' for available commands.\n";
  83.     $| = 1;
  84.     while () {
  85.         print $prompt;
  86.         last unless defined ($_ = <> );
  87.         chomp;
  88.         s/^\s+//;
  89.         @ARGV = split(/\s+/, $_);
  90.         next unless @ARGV;
  91.         # exit/quit
  92.         if (command($ARGV[0], "qu|it") or command($ARGV[0], "|exit")) {
  93.             print "Quit!\n";
  94.             last;
  95.         }
  96.         exec_command();
  97.     }
  98.     exit 0;
  99. }
  100.  
  101. exit exec_command();
  102.  
  103. sub exec_command
  104. {
  105.     my $cmd = lc shift @ARGV;
  106.  
  107.     for (@ARGV) {
  108.         s/::/-/g;
  109.     }
  110.  
  111.     # help
  112.     if (command($cmd, "|help")) {
  113.         help(@ARGV);
  114.     return 0;
  115.     }
  116.     # query
  117.     elsif (command($cmd, "qu|ery")) {
  118.         GetOptions("case!" => \my $case, "abstract" => \my $abstract, 
  119.         "author" => \my $author );
  120.  
  121.         my %summary = InstalledPackageProperties();
  122.         if (@ARGV) {
  123.             my $searchtag;
  124.             if ($abstract || $author) {
  125.                 $searchtag = ($abstract ? 'ABSTRACT' : 'AUTHOR');
  126.             }
  127.             my $RE = shift @ARGV;
  128.             eval { $RE =~ /$RE/ };
  129.             if ($@) {
  130.                 print "'$RE': invalid regular expression.\n";
  131.                 return 1;
  132.             } 
  133.             $case = !$options{'IGNORECASE'} unless defined $case;
  134.             $RE = "(?i)$RE" if ($case == 0);
  135.             foreach(keys %summary) {
  136.                 if ($searchtag) {
  137.                     delete $summary{$_} unless $summary{$_}{$searchtag} =~ /$RE/;
  138.                 }
  139.                 else {
  140.                     delete $summary{$_} unless /$RE/;
  141.                 }
  142.             }
  143.         }
  144.         print_formatted(1, %summary);
  145.     return 0;
  146.     }
  147.     # install
  148.     elsif (command($cmd, "in|stall")) {
  149.         my $location = $location;
  150.         GetOptions("location=s" => \$location);
  151.         unless (@ARGV) {
  152.             if (!$interactive && -d "blib" && -f "Makefile") {
  153.                 return if InstallPackage(location => $location);
  154.                 print "Error installing blib: $PPM::PPMERR\n";
  155.                 return 1;
  156.             }
  157.             print "Package not specified.\n";
  158.             return 1;
  159.         }
  160.  
  161.         my %installed = InstalledPackageProperties();
  162.         foreach my $package (@ARGV) {
  163.             $package =~ s/::/-/g;
  164.             if (my $pkg = (grep {/^$package$/i} keys %installed)[0]) {
  165.                 my $version = $installed{$pkg}{'VERSION'};
  166.                 $version =~ s/(,0)*$//;
  167.                 $version =~ tr/,/./;
  168.                 print "Version $version of '$pkg' is already installed.\n" .
  169.                       "Remove it, or use 'verify --upgrade $pkg'.\n";
  170.                 next;
  171.             }
  172.             elsif ($interactive && $options{'CONFIRM'}) {
  173.                 print "Install package '$package?' (y/N): ";
  174.                 next unless <> =~ /^[yY]/;
  175.             }
  176.             print "Installing package '$package'...\n";
  177.             if(!InstallPackage("package" => $package, "location" => $location)) {
  178.                 print "Error installing package '$package': $PPM::PPMERR\n";
  179.         return 1;
  180.             }
  181.         }
  182.     return 0;
  183.     }
  184.     # remove
  185.     elsif (command($cmd, "|remove")) {
  186.         unless (@ARGV) {
  187.             print "Package not specified.\n";
  188.             return 1;
  189.         }
  190.         foreach my $package (@ARGV) {
  191.             $package =~ s/::/-/g;
  192.             if ($interactive && $options{'CONFIRM'}) {
  193.                 print "Remove package '$package?' (y/N): ";
  194.                 next unless <> =~ /[yY]/;
  195.             }
  196.             unless (RemovePackage("package" => $package)) {
  197.                 print "Error removing $package: $PPM::PPMERR\n";
  198.             }
  199.         }
  200.     return 0;
  201.     }
  202.     # search
  203.     elsif (command($cmd, "se|arch")) {
  204.         my (%summary, $searchtag);
  205.         my $location = $location;
  206.         GetOptions("case!" => \my $case, "location=s" => \$location, 
  207.             "abstract" => \my $abstract, "author" => \my $author );
  208.         my $searchRE = shift @ARGV;
  209.         if (defined $searchRE) {
  210.             eval { $searchRE =~ /$searchRE/ };
  211.             if ($@) {
  212.                 print "'$searchRE': invalid regular expression.\n";
  213.                 return 1;
  214.             }
  215.         }
  216.         $case = !$options{'IGNORECASE'} unless defined $case;
  217.         if ($abstract || $author) {
  218.             $searchtag = ($abstract ? 'ABSTRACT' : 'AUTHOR');
  219.         }
  220.         %summary = search_PPDs("location" => $location, "ignorecase" => !$case,
  221.             "searchtag" => $searchtag, "searchRE" => $searchRE);
  222.         foreach (keys %summary) {
  223.             print "Packages available from $_:\n";
  224.             print_formatted(2, %{$summary{$_}});
  225.         }
  226.     return 0;
  227.     }
  228.     # set
  229.     elsif (command($cmd, "se|t")) {
  230.         unless (set(@ARGV) || $interactive) {
  231.             PPM::SetPPMOptions("options" => \%options, "save" => 1);
  232.         }
  233.     return 0;
  234.     }
  235.     # verify
  236.     elsif (command($cmd, "ver|ify")) {
  237.         my $location = $location;
  238.         GetOptions("force" => \my $force, "location=s" => \$location, 
  239.             "upgrade" => \my $upgrade);
  240.         if ($interactive && $upgrade && $options{'CONFIRM'}) {
  241.             printf "Upgrade package%s? (y/N): ", @ARGV == 1 ? " '$ARGV[0]'" : "s";
  242.             return 0 unless <> =~ /^[yY]/;
  243.         }
  244.         return verify_packages("packages" => \@ARGV, "location" => $location, 
  245.             "upgrade" => $upgrade, "force" => $force);
  246.     }
  247.     elsif (command($cmd, "ver|sion")) {
  248.         print "$PPM::VERSION\n";
  249.     return 0;
  250.     }
  251.     elsif ($cmd eq "purge") {
  252.         my %summary = InstalledPackageProperties();
  253.         foreach(keys %summary) {
  254.             print "Purging $_\n";
  255.             RemovePackage("package" => $_, "force" => 1);
  256.         }
  257.     return 0;
  258.     }
  259.     elsif ($cmd eq 'refresh') {
  260.         my %summary = InstalledPackageProperties();
  261.     my $status = 0;
  262.         foreach(keys %summary) {
  263.             print "Refreshing $_\n";
  264.             if (!InstallPackage("package" => $_)) {
  265.                 print "Error installing package '$_': $PPM::PPMERR\n";
  266.         ++$status;
  267.             }
  268.         }
  269.     return $status;
  270.     }
  271.     else {
  272.         print "Unknown or ambiguous command '$cmd'; type 'help' for commands.\n";
  273.     return 1;
  274.     }
  275. }
  276.  
  277. sub help {
  278.     my $topic = @_ && $help{lc $_[0]} ? lc $_[0] : 'help';
  279.     my $help = $help{$topic};
  280.     $help =~ s/^(\s*)ppm\s+/$1/mg if $interactive;
  281.     print $help;
  282. }
  283.  
  284. sub more
  285. {
  286.     my ($lines) = shift @_;
  287.     if (++$$lines >= $options{'MORE'}) {
  288.         print $moremsg;
  289.         $_ = <>;
  290.         $$lines = $_ eq "q\n" ? -1 : 1;
  291.     }
  292. }
  293.  
  294. # This nasty piece of business splits $pattern into a required prefix 
  295. # and a "match any of this substring" suffix.  E.g. "in|stall" will
  296. # match a $cmd of "ins", "inst", ..., "install".
  297. sub command
  298. {
  299.     my ($cmd, $pattern) = @_;
  300.     my @pattern = split(/\|/, $pattern);
  301.     if ($pattern[1]) {
  302.         my @optchars = split(//, $pattern[1]);
  303.         # build up a "foo(b|ba|bar)" string
  304.         $pattern = "$pattern[0](";
  305.         $pattern[1] = shift @optchars;
  306.         $pattern[1] .= "|$pattern[1]$_" foreach @optchars;
  307.         $pattern .= "$pattern[1])";
  308.     }
  309.     return ($cmd =~ /^${pattern}$/i);
  310. }
  311.  
  312. # This routine prints the output for query and search
  313. # in a nicely formatted way, if $options{'VERBOSE'} is set.
  314. sub print_formatted
  315. {
  316.     my ($lines, %summary) = @_;
  317.     my $package;
  318.  
  319.     unless ($options{'VERBOSE'}) {
  320.         foreach $package (sort keys %summary) {
  321.             print "$package\n";
  322.             &more(\$lines) if $options{'MORE'} && $interactive;
  323.             last if $lines == -1;
  324.         }
  325.         return;
  326.     }
  327.  
  328.     my ($maxname, $maxversion) = (0, 0);
  329.     # find the longest package name and version strings, so we can
  330.     # format them nicely
  331.     $maxname < length($_) and $maxname = length($_) for keys %summary;
  332.     foreach $package (keys %summary) {
  333.         $summary{$package}{'VERSION'} =~ s/(,0)*$//;
  334.         $summary{$package}{'VERSION'} =~ tr/,/./;
  335.         $maxversion = length $summary{$package}{'VERSION'} > $maxversion ? 
  336.             length $summary{$package}{'VERSION'} : $maxversion;
  337.     }
  338.     my $columns = $ENV{COLUMNS} ? $ENV{COLUMNS} : 80;
  339.     my $namefield = "@" . "<" x ($maxname - 1);
  340.     my $versionfield = "@" . "<" x ($maxversion - 1);
  341.     my $abstractfield = "^" . "<" x ($columns - (6 + $maxname + $maxversion));
  342.     my $abstractpad = " " x ($maxname + $maxversion + 3);
  343.  
  344.     foreach $package (sort keys %summary) {
  345.         eval "format STDOUT = \n"
  346.                    . "$namefield [$versionfield] $abstractfield\n"
  347.                    . '$package, $summary{$package}{VERSION}, $summary{$package}{ABSTRACT}'
  348.                    . "\n"
  349.                    . "$abstractpad $abstractfield~~\n"
  350.                    . '$summary{$package}{ABSTRACT}' 
  351.                    . "\n"
  352.                    . ".\n";
  353.  
  354.         my $diff = $-;
  355.         write;
  356.         $diff -= $-;
  357.         $lines += ($diff - 1) if $diff > 1;
  358.         &more(\$lines) if $options{'MORE'} && $interactive;
  359.         last if $lines == -1;
  360.     }
  361. }
  362.  
  363. sub set
  364. {
  365.     my $option = lc shift @_; 
  366.  
  367.     unless ($option) {
  368.         print "Commands will " . ($options{'CONFIRM'} ? "" : "not ") . 
  369.             "be confirmed.\n";
  370.         print "Temporary files will " . ($options{'CLEAN'} ? "" : "not ") .
  371.             "be deleted.\n";
  372.         print "Download status will " . (($options{'DOWNLOADSTATUS'} > 0) ?
  373.             "be updated every $options{'DOWNLOADSTATUS'} bytes.\n" : 
  374.             "not be updated.\n");
  375.         print "Case-" . ($options{'IGNORECASE'} ? "in" : "") . 
  376.             "sensitive searches will be performed.\n";
  377.         print "Package installations will " . 
  378.             ($options{'FORCE_INSTALL'} ? "" : "not ") .
  379.                "continue if a dependency cannot be installed.\n";
  380.         print "Tracing info will " . (($options{'TRACE'} > 0 ) ? 
  381.             "be written to '$options{'TRACEFILE'}'.\n" : "not be written.\n");
  382.         print "Screens will " . ($options{'MORE'} > 0 ? 
  383.             "pause after $options{'MORE'} lines.\n" : "not pause.\n");
  384.         print "Query/search results will " . 
  385.             ($options{'VERBOSE'} ? "" : "not ") . "be verbose.\n";
  386.         if (defined $location) { print "Current PPD repository: $location\n"; }
  387.         else {
  388.             print "Current PPD repository paths:\n";
  389.             foreach $_ (keys %repositories) {
  390.                 print "\t$_: $repositories{$_}\n";
  391.             }
  392.         }
  393.         print "Packages will be installed under: $options{'ROOT'}\n" 
  394.             if ($options{'ROOT'});
  395.         print "Packages will be built under: $options{'BUILDDIR'}\n" 
  396.             if ($options{'BUILDDIR'});
  397.         return;
  398.     }
  399.  
  400.     my $value = shift @_;
  401.     if (command($option, "r|epository")) {
  402.         if ($value =~ /${prefix_pattern}remove/i) {
  403.             $value = join(" ", @_);
  404.             print "Location not specified.\n" and return 1 
  405.                 unless (defined $value);
  406.             PPM::RemoveRepository("repository" => $value);
  407.             %repositories = PPM::ListOfRepositories();
  408.         }
  409.         else {
  410.             my $location = shift @_;
  411.             print "Repository not specified.\n" and return 1
  412.                 unless (defined $value and defined $location);
  413.             PPM::AddRepository("repository" => $value,
  414.                 "location" => $location);
  415.             %repositories = PPM::ListOfRepositories();
  416.         }
  417.     }
  418.     else {
  419.         if (command($option, "c|onfirm")) {
  420.             $options{'CONFIRM'} = defined $value ? 
  421.                 ($value != 0) : ($options{'CONFIRM'} ? 0 : 1);
  422.             print "Commands will " . ($options{'CONFIRM'} ? "" : "not ") . 
  423.                 "be confirmed.\n";
  424.         }
  425.         elsif (command($option, "|save")) {
  426.             PPM::SetPPMOptions("options" => \%options, "save" => 1);
  427.             return 0;
  428.         }
  429.         elsif (command($option, "c|ase")) {
  430.             $options{'IGNORECASE'} = defined $value ? 
  431.                 ($value == 0) : ($options{'IGNORECASE'} ? 0 : 1);
  432.             print "Case-" . ($options{'IGNORECASE'} ? "in" : "") . 
  433.                 "sensitive searches will be performed.\n";
  434.         }
  435.         elsif (command($option, "r|oot")) {
  436.             my $old_root;
  437.             print "Directory not specified.\n" and return 1 unless ($value);
  438.             print "$PPM::PPMERR" and return 1
  439.                     unless ($old_root = PPM::chroot("location" => $value));
  440.             $options{'ROOT'} = $value;
  441.             print "Root is now $value [was $old_root].\n";
  442.         }
  443.         elsif (command($option, "|build")) {
  444.             print "Directory not specified.\n" and return 1 unless ($value);
  445.             print "Directory '$value' does not exist.\n" and return 1 
  446.                 unless (-d $value);
  447.             $options{'BUILDDIR'} = $value;
  448.             print "Build directory is now $value.\n";
  449.         }
  450.         elsif (command($option, "|force_install")) {
  451.             $options{'FORCE_INSTALL'} = defined $value ? ($value != 0) : 
  452.                 ($options{'FORCE_INSTALL'} ? 0 : 1);
  453.             print "Package installations will " .
  454.                   ($options{'FORCE_INSTALL'} ? "" : "not ") .
  455.                   "continue if a dependency cannot be installed.\n";
  456.         }
  457.         elsif (command($option, "c|lean")) {
  458.             $options{'CLEAN'} = defined $value ? 
  459.                 ($value != 0) : ($options{'CLEAN'} ? 0 : 1);
  460.             print "Temporary files will " . ($options{'CLEAN'} ? "" : "not ") . 
  461.                 "be deleted.\n";
  462.         }
  463.         elsif (command($option, "|downloadstatus")) {
  464.             print "Numeric value must be given.\n" and return 1
  465.                 unless (defined $value && $value =~ /^\d+$/);
  466.             $options{'DOWNLOADSTATUS'} = $value;
  467.             print "Download status will " . (($options{'DOWNLOADSTATUS'} > 0) ?
  468.                 "be updated every $options{'DOWNLOADSTATUS'} bytes.\n" : 
  469.                 "not be updated.\n");
  470.         }
  471.         elsif (command($option, "|more")) {
  472.             print "Numeric value must be given.\n" and return 1
  473.                 unless (defined $value && $value =~ /^\d+$/);
  474.             $options{'MORE'} = $value;
  475.             print "Screens will " . ($options{'MORE'} > 0 ? 
  476.                 "pause after $options{'MORE'} lines.\n" : "not pause.\n");
  477.         }
  478.         elsif (command($option, "trace|file")) {
  479.             print "Filename not specified.\n" and return 1 unless ($value);
  480.             $options{'TRACEFILE'} = $value;
  481.             print "Tracing info will be written to $options{'TRACEFILE'}.\n";
  482.         }
  483.         elsif (command($option, "trace")) {
  484.             print "Numeric value between 0 and 4 must be given.\n" and return 1
  485.                 unless (defined $value && 
  486.                     $value =~ /^\d+$/ && $value >= 0 && $value <= 4);
  487.             $options{'TRACE'} = $value;
  488.             print "Tracing info will " . ($options{'TRACE'} > 0 ? 
  489.                 "be written to $options{'TRACEFILE'}.\n" : "not be written.\n");
  490.         }
  491.         elsif (command($option, "|verbose")) {
  492.             $options{'VERBOSE'} = defined $value ? 
  493.                 ($value != 0) : ($options{'VERBOSE'} ? 0 : 1);
  494.             print "Query/search results will " . 
  495.                 ($options{'VERBOSE'} ? "" : "not ") . "be verbose.\n";
  496.         }
  497.         else {
  498.             print "Unknown or ambiguous option '$option'; see 'help set' for available options.\n";
  499.             return 1;
  500.         }
  501.         PPM::SetPPMOptions("options" => \%options);
  502.     }
  503.     return;
  504. }
  505.  
  506. sub search_PPDs
  507. {
  508.     my %argv = @_;
  509.     my @locations = $argv{'location'} || $location;
  510.     my $searchtag = $argv{'searchtag'};
  511.     my $ignorecase = defined $argv{'ignorecase'} ? 
  512.         $argv{'ignorecase'} : $options{'IGNORECASE'};
  513.     my $searchRE = $argv{'searchRE'};
  514.     if (defined $searchRE) {
  515.         eval { $searchRE =~ /$searchRE/ };
  516.         if ($@) {
  517.             print "'$searchRE': invalid regular expression.\n";
  518.             return;
  519.         }
  520.         $searchRE = "(?i)$searchRE" if $ignorecase;
  521.     }
  522.  
  523.     my %packages;
  524.     unless (defined $locations[0]) {
  525.         my %reps = PPM::ListOfRepositories;
  526.         @locations = values %reps;
  527.     }
  528.     foreach my $loc (@locations) {
  529.         my %summary;
  530.  
  531.         # see if the repository has server-side searching
  532.         if (defined $searchRE && (%summary = ServerSearch('location' => $loc, 
  533.                 'searchRE' => $searchRE, 'searchtag' => $searchtag))) {
  534.             # XXX: clean this up
  535.             foreach my $package (keys %{$summary{$loc}}) {
  536.                 $packages{$loc}{$package} = \%{$summary{$loc}{$package}};
  537.             }
  538.             next;
  539.         }
  540.  
  541.         # see if a summary file is available
  542.         %summary = RepositorySummary("location" => $loc);
  543.         if (%summary) {
  544.             foreach my $package (keys %{$summary{$loc}}) {
  545.                 next if (defined $searchtag && 
  546.                     $summary{$loc}{$package}{$searchtag} !~ /$searchRE/);
  547.                 next if (!defined $searchtag && 
  548.                     defined $searchRE && $package !~ /$searchRE/);
  549.                 $packages{$loc}{$package} = \%{$summary{$loc}{$package}};
  550.             }
  551.         }
  552.         else {
  553.             my %ppds = PPM::RepositoryPackages("location" => $loc);
  554.             # No summary: oh my, nothing but 'Net
  555.             foreach my $package (@{$ppds{$loc}}) {
  556.                 my %package_details = RepositoryPackageProperties(
  557.                     "package" => $package, "location" => $loc);
  558.                 next unless %package_details;
  559.                 next if (defined $searchtag && 
  560.                     $package_details{$searchtag} !~ /$searchRE/);
  561.                 next if (!defined $searchtag && 
  562.                     defined $searchRE && $package !~ /$searchRE/);
  563.                 $packages{$loc}{$package} = \%package_details;
  564.             }
  565.         }
  566.     }
  567.     return %packages;
  568. }
  569.  
  570. sub verify_packages
  571. {
  572.     my %argv = @_;
  573.     my @packages = @{$argv{'packages'}};
  574.     my $upgrade = $argv{'upgrade'};
  575.     my $force = $argv{'force'};
  576.     my $location = $argv{'location'} || $location;
  577.     unless ($packages[0]) {
  578.         my %info = QueryInstalledPackages();
  579.         @packages = sort keys %info;
  580.     }
  581.     my $failed = 0;
  582.  
  583.     my $package = shift @packages;
  584.     while ($package) {
  585.         my $status = VerifyPackage("package" => $package, 
  586.             "location" => $location, "upgrade" => $upgrade, "force" => $force);
  587.         if (defined $status) {
  588.             if ($status eq "0") {
  589.                 print "Package \'$package\' is up to date.\n";
  590.             }
  591.             elsif ($upgrade) {
  592.                 print "Package $package upgraded to version $status\n";
  593.             }
  594.             else {
  595.                 print "An upgrade to package \'$package\' is available.\n";
  596.             }
  597.         }
  598.         else {
  599.             # Couldn't find a PPD to compare it with.
  600.             # Hack: no warning if its on of the core PPM 
  601.             # modules (hardcoded below :)
  602.             my $no=0;
  603.             foreach my $corepackage (
  604.             "Compress-Zlib","Archive-Tar","Digest-MD5","File-CounterFile",
  605.             "Font-AFM","HTML-Parser","HTML-Tree","MIME-Base64","URI",
  606.             "libwww-perl","XML-Parser","SOAP-Lite","PPM","libnet",
  607.             "libwin32","HTML-Tagset"
  608.             )
  609.             {
  610.                 if ($corepackage eq $package) {
  611.                     $no=1;
  612.                     last;
  613.                 }
  614.             }
  615.         unless ($no) {
  616.         ++$failed;
  617.         print "While verifying package '$package': $PPM::PPMERR\n";
  618.         }
  619.         }
  620.         $package = shift @packages;
  621.     }
  622.     return $failed;
  623. }
  624.  
  625. sub genconfig
  626. {
  627. my $PerlDir = $Config{'prefix'};
  628. print <<"EOF";
  629. <PPMCONFIG>
  630.     <PPMVER>2,2,0,0</PPMVER>
  631.     <PLATFORM CPU="x86" OSVALUE="$Config{'osname'}" OSVERSION="0,0,0,0" />
  632.     <OPTIONS BUILDDIR="$ENV{'TEMP'}" CLEAN="1" CONFIRM="1" DOWNLOADSTATUS="16384" FORCEINSTALL="1" IGNORECASE="1" MORE="0" ROOT="$PerlDir" TRACE="0" TRACEFILE="PPM.LOG" VERBOSE="1" />
  633.     <REPOSITORY LOCATION="http://www.ActiveState.com/cgibin/PPM/ppmserver.pl?urn:/PPMServer" NAME="ActiveState Package Repository" SUMMARYFILE="fetch_summary"/>
  634.     <PPMPRECIOUS>Compress-Zlib;Archive-Tar;Digest-MD5;File-CounterFile;Font-AFM;HTML-Parser;HTML-Tree;MIME-Base64;URI;libwww-perl;XML-Parser;SOAP-Lite;PPM;libnet;libwin32</PPMPRECIOUS>
  635. </PPMCONFIG>
  636. EOF
  637. }
  638.  
  639. __DATA__
  640.  
  641. =head1 NAME
  642.  
  643. PPM - Perl Package Manager: locate, install, upgrade software packages.
  644.  
  645. =head1 SYNOPSIS
  646.  
  647.  ppm genconfig
  648.  ppm help [command]
  649.  ppm install [--location=location] package1 [... packageN]
  650.  ppm query [--case|nocase] [--abstract|author] PATTERN
  651.  ppm remove package1 [... packageN]
  652.  ppm search [--case|nocase] [--location=location] [--abstract|author] PATTERN
  653.  ppm set [option]
  654.  ppm verify [--location=location] [--upgrade] [--force] [package1 ... packageN]
  655.  ppm version
  656.  ppm [--location=location]
  657.  
  658. =head1 DESCRIPTION
  659.  
  660. ppm is a utility intended to simplify the tasks of locating, installing,
  661. upgrading and removing software packages.  It is a front-end to the
  662. functionality provided in PPM.pm.  It can determine if the most recent
  663. version of a software package is installed on a system, and can install
  664. or upgrade that package from a local or remote host.
  665.  
  666. ppm runs in one of two modes: an interactive shell from which commands
  667. may be entered; and command-line mode, in which one specific action is
  668. performed per invocation of the program.
  669.  
  670. ppm uses files containing an extended form of the Open Software
  671. Description (OSD) specification for information about software packages.
  672. These description files, which are written in Extensible Markup
  673. Language (XML) code, are referred to as 'PPD' files.  Information about
  674. OSD can be found at the W3C web site (at the time of this writing,
  675. http://www.w3.org/TR/NOTE-OSD.html).  The extensions to OSD used by ppm
  676. are documented in PPM.ppd.
  677.  
  678. =head1 Using PPM
  679.  
  680. =over 4
  681.  
  682. =item Interactive mode
  683.  
  684. If ppm is invoked with no command specified, it is started in interactive
  685. mode.  If the '--location' argument is specified, it is used as the
  686. search location, otherwise the repositories specified in the PPM data file
  687. are used. 
  688.  
  689. The syntax of PPM commands is the same in interactive mode as it is in
  690. command-line mode.  The 'help' command lists the available commands.
  691.  
  692. ppm commands may be abbreviated to their shortest unique form.
  693.  
  694. =item Installing
  695.  
  696.  ppm install [--location=location] package1 [... packageN]
  697.  
  698. Installs the specified software packages. Attempts to install from the 
  699. URL or directory 'location' if the '--location' option is specfied. 
  700.  
  701. The 'package' arguments may be either package names ('foo'), pathnames 
  702. (p:/packages/foo.ppd) or URLs (http://www.ActiveState.com/packages/foo.ppd)
  703. to specific PPD files.
  704.  
  705. In the case where a package name is specified, and the '--location'
  706. option is not used, ppm will refer to the default repository locations.
  707.  
  708. See also: 'confirm' option.
  709.  
  710. =item Removing
  711.  
  712.  ppm remove package1 [... packageN]
  713.  
  714. Reads information from the PPD file for the named software package and
  715. removes the package from the system.
  716.  
  717. See also: 'confirm' option.
  718.  
  719. =item Verifying
  720.  
  721.  ppm verify [--location=location] [--upgrade] [--force] [package1 ... packageN]
  722.  
  723. Verifies that the currently installed packages are up to date.  If no
  724. packages are specified as arguments, all installed packages will be verified.
  725.  
  726. If the '--upgrade' option is specified, any package for which an upgrade 
  727. is available will be upgraded.  
  728.  
  729. If the '--location' option is specified, upgrades will be looked for at 
  730. the specified URL or directory.
  731.  
  732. If the '--force' option is specified, all currently installed packages will 
  733. be reinstalled regardless of whether they are out of date or not.
  734.  
  735. See also: 'confirm' option.
  736.  
  737. =item Querying
  738.  
  739.  ppm query [--case|nocase] [--abstract|author] PATTERN
  740.  
  741. Searches for 'PATTERN' (a regular expression) in the name of any installed 
  742. package.  If a search is successful, information about the matching 
  743. package(s) is displayed.  If 'PATTERN' is omitted, information about
  744. all installed packages will be displayed.
  745.  
  746. If either '--abstract' or '--author' is specified, PATTERN will be 
  747. searched for in the <ABSTRACT> or <AUTHOR> tags of the installed packages.
  748.  
  749. The '--case' and '--nocase' options can be used to override the default
  750. case-sensitivity search settings.
  751.  
  752. See also: 'case' option.
  753.  
  754. =item Searching
  755.  
  756.  ppm search [--case|nocase] [--location=location] [--abstract|author] PATTERN
  757.  
  758. Displays a list of any packages matching 'PATTERN' (a regular expression)
  759. available from the specified location.  If 'PATTERN' is omitted, information 
  760. about all available packages will be displayed.
  761.  
  762. If the '--location' option is specified, the specified URL or directory
  763. will be searched.  If '--location' is not specified, the repository location 
  764. as specified in the PPM data file will be searched.
  765.  
  766. If either '--abstract' or '--author' is specified, PATTERN will be 
  767. searched for in the <ABSTRACT> or <AUTHOR> tags of the available packages.
  768.  
  769. The '--case' and '--nocase' options can be used to override the default
  770. case-sensitivity search settings.
  771.  
  772. See also: 'case' option.
  773.  
  774. =item Error Recovery
  775.  
  776.  ppm genconfig
  777.  ppm getconfig
  778.  
  779. The genconfig command will print a valid PPM config file (ppm.xml) to STDOUT.
  780. This can be useful if the PPM config file ever gets damaged leaving PPM
  781. unusable.
  782.  
  783. If required, this command should be run from a shell prompt:
  784.  
  785.     C:\Perl\site\lib> ppm genconfig > ppm.xml
  786.  
  787. The getconfig command prints the location of the PPM configuration file
  788. used at PPM startup.
  789.  
  790. =item Options
  791.  
  792.  ppm set [option value]
  793.  
  794. Sets or displays current options.  With no arguments, current option
  795. settings are displayed.  For options that accept '1' or '0', specifying
  796. '1' sets the option, and '0' unsets it.
  797.  
  798. Available options:
  799.  
  800.     build DIRECTORY
  801.         - Changes the package build directory.
  802.  
  803.     case [1|0]
  804.         - Sets case-sensitive searches.  If one of '1' or '0' is
  805.           not specified, the current setting is toggled.
  806.  
  807.     clean [1|0]
  808.         - Sets removal of temporary files from package's build 
  809.           area, on successful installation of a package.  If one of
  810.           '1' or '0' is not specified, the current setting is
  811.           toggled.
  812.  
  813.     confirm [1|0]
  814.         - Sets confirmation of 'install', 'remove' and 'upgrade'.
  815.           If one of '1' or '0' is not specified, the current
  816.           setting is toggled.
  817.  
  818.     downloadstatus NUMBER
  819.         - If non-zero, updates the download status after each NUMBER 
  820.           of bytes transferred during an 'install'.  This can be
  821.           reassuring when installing a large package (e.g. Tk) over
  822.           a low-speed connection.
  823.  
  824.     force_install [1|0]
  825.         - Continue installing a package even if a dependency cannot
  826.           be installed.
  827.  
  828.     more NUMBER
  829.         - Causes output to pause after NUMBER lines have been
  830.           displayed.  Specifying '0' turns off this capability.
  831.  
  832.     set repository --remove NAME
  833.         - Removes the repository 'NAME' from the list of repositories.
  834.  
  835.     set repository NAME LOCATION
  836.         - Adds a repository to the list of PPD repositories for this
  837.           session.  'NAME' is the name by which this repository will
  838.           be referred; 'LOCATION' is a URL or directory name.
  839.  
  840.     root DIRECTORY
  841.         - Changes the install root directory.  Packages will be
  842.           installed under this new root.
  843.  
  844.     save
  845.         - Saves the current options as default options for future
  846.           sessions.
  847.  
  848.     trace
  849.         - Tracing level--default is 1, maximum is 4, 0 indicates
  850.           no tracing.
  851.  
  852.     tracefile
  853.         - File to contain tracing information, default is 'PPM.LOG'.
  854.  
  855.     verbose [1|0]
  856.         - Display additional package information for 'query' and
  857.           'search' commands.
  858.  
  859. =head1 EXAMPLES
  860.  
  861. =over 4
  862.  
  863. =item ppm
  864.  
  865. Starts ppm in interactive mode, using the repository locations specified
  866. in the PPM data file.  A session might look like this:
  867.  
  868.     [show all available packages]
  869.     PPM> search
  870.     Packages available from P:\PACKAGES:
  871.     bar [2.91 ] supplies bar methods for perl5.
  872.     bax [0.072] module for manipulation of bax archives.
  873.     baz [1.03 ] Interface to baz library
  874.     foo [2.23 ] Foo parser class
  875.     
  876.     [list what has already been installed]
  877.     PPM> query
  878.     bax [0.071] module for manipulation of bax archives.
  879.     baz [1.02 ] Interface to baz library
  880.     
  881.     [install a package]
  882.     PPM> install foo
  883.     Install package foo? (y/N): y
  884.     [...]
  885.     
  886.     [toggle confirmations]
  887.     PPM> set confirm
  888.     Commands will not be confirmed.
  889.     
  890.     [see if 'baz' is up-to-date]
  891.     PPM> verify baz
  892.     An upgrade to package 'baz' is available.
  893.     
  894.     [upgrade 'baz']
  895.     PPM> verify --upgrade baz
  896.     [...]
  897.     
  898.     [forced upgrade of 'baz']
  899.     PPM> verify --upgrade --force baz
  900.     [...]
  901.     
  902.     [toggle case-sensitive searches]
  903.     PPM> set case
  904.     Case-sensitive searches will be performed.
  905.     
  906.     [display all available packages beginning with 'b']
  907.     PPM> search ^b
  908.     bar [2.91 ] supplies bar methods for perl5.
  909.     bax [0.072] module for manipulation of bax archives.
  910.     baz [1.03 ] Interface to baz library
  911.     
  912.     [search for installed packages containing 'baz' in the ABSTRACT tag]
  913.     PPM> query --abstract baz
  914.     Matching packages found at P:\PACKAGES:
  915.     baz [1.03 ] Interface to baz library
  916.     PPM> quit
  917.  
  918. =item ppm install http://www.ActiveState.com/packages/foo.ppd
  919.  
  920. Installs the software package 'foo' based on the information in the PPD
  921. obtained from the specified URL.
  922.  
  923. =item ppm verify --upgrade foo
  924.  
  925. Compares the currently installed version of the software package 'foo'
  926. to the one available according to the PPD obtained from the location
  927. specified for this package in the PPM data file, and upgrades
  928. to a newer version if available.
  929.  
  930. =item ppm verify --location=P:\PACKAGES --upgrade foo
  931.  
  932. Compares the currently installed version of the software package 'foo'
  933. to the one available according to the PPD obtained from the specified
  934. directory, and upgrades to a newer version if available.
  935.  
  936. =item ppm verify --upgrade --force
  937.  
  938. Forces verification and reinstalls every installed package on the system, 
  939. using upgrade locations specified in the PPM data file.
  940.  
  941. =item ppm search --location=http://ppm.ActiveState.com/PPMpackages/5.8plus
  942.  
  943. Displays the packages with PPD files available at the specified location.
  944.  
  945. =item ppm search --location=P:\PACKAGES --author ActiveState
  946.  
  947. Searches the specified location for any package with an <AUTHOR> tag
  948. containing the string 'ActiveState'.  On a successful search, the package
  949. name and the matching string are displayed.
  950.  
  951. =back
  952.  
  953. =head1 ENVIRONMENT VARIABLES
  954.  
  955. =over 4
  956.  
  957. =item HTTP_proxy
  958.  
  959. If the environment variable 'HTTP_proxy' is set, then it will
  960. be used as the address of a proxy server for accessing the Internet.
  961.  
  962. The value should be of the form: 'http://proxy:port'.
  963.  
  964. =back
  965.  
  966. =head1 FILES
  967.  
  968. The following files are fully described in the 'Files' section of PPM:ppm.
  969.  
  970. =over 4
  971.  
  972. =item package.ppd
  973.  
  974. A description of a software package, in extended Open Software Description
  975. (OSD) format.  More information on this file format can be found in
  976. PPM::ppd.
  977.  
  978. =item ppm.xml - PPM data file.
  979.  
  980. An XML format file containing information about the local system,
  981. specifics regarding the locations from which PPM obtains PPD files, and
  982. the installation details for any package installed by ppm.
  983.  
  984. This file usually resides in '[perl]/site/lib'.  If the environment 
  985. variable 'PPM_DAT' is set, its value will be used as the full pathname
  986. to a PPM data file.  If all else fails, ppm will look for a data file
  987. in the current directory.
  988.  
  989. =back
  990.  
  991. =head1 AUTHOR
  992.  
  993. Murray Nesbitt, E<lt>F<murray@ActiveState.com>E<gt>
  994.  
  995. =head1 CREDITS
  996.  
  997. =over 4
  998.  
  999. =item *
  1000.  
  1001. The "geek-pit" at ActiveState.
  1002.  
  1003. =item *
  1004.  
  1005. Paul Kulchenko for his SOAP-Lite package, and for his enthusiastic
  1006. assistance in getting PPM to work with SOAP-Lite.
  1007.  
  1008. =back
  1009.  
  1010. =cut
  1011.