#!/usr/bin/perl -I./ # install prerequisites from CPAN without configuring CPAN =for Copyright . Copyright (c) 2007-2008 Bruce Ravel (bravel AT bnl DOT gov). All rights reserved. . This file is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See The Perl Artistic License. . This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. =cut use strict; use warnings; use CPAN; use Cwd qw(cwd); use Getopt::Long; use File::Copy; use File::Spec; use Pod::Text; BEGIN { ## overload any existing CPAN configuration with one "guaranteed" to work unshift @INC, "./"; $ENV{__INSTALLING_BRPERL} = cwd(); unshift @INC, "./CPAN"; require MyConfig; } my $bundir = File::Spec->catfile(cwd, "Bundle/"); opendir(my $BUNDLE, $bundir) or die "could not open directory $bundir for reading"; my @files = grep {/pm$/} readdir $BUNDLE; closedir $BUNDLE; my $this_package = q{}; foreach my $f (@files) { ($this_package = $1), last if ($f =~ m{(\w*)Bundle\.pm}); }; my ($noforce, $help, $quickhelp, $system, $query, $copy, $proxy, $bundle) = (0, 0, 0, 0, 0, 0, q{}, "Bundle::".$this_package."Bundle"); GetOptions("noforce" => \$noforce, "n" => \$noforce, "bundle=s" => \$bundle, "proxy=s" => \$proxy, # q[192.168.1.140:3128] at BNL "system" => \$system, "s" => \$system, "query" => \$query, "q" => \$query, "help" => \$help, "h" => \$quickhelp, "c" => \$copy, ); ## small chores then exit copyem(), exit if $copy; quickhelp(), exit if $quickhelp; Pod::Text -> new(sentence => 0, width => 78) -> parse_from_file($0), exit if $help; ## remove the overloaded CPAN configuration and import the normal one if ($system) { shift @INC; require CPAN::Config; import CPAN::Config; }; banner("CPAN working directory:" . $CPAN::Config->{cpan_home}); # print join($/, @INC), $/; # exit; ## specify a proxy server if ($proxy) { $CPAN::Config->{http_proxy} = $proxy; $CPAN::Config->{ftp_proxy} = $proxy; }; my @modlist = bundle_list($bundle); foreach my $mod (@modlist) { my @mm = CPAN::Shell->expand("Module", "/\^$mod\$/"); if (not defined($mm[0])) { banner("Cannot determine version number for $mod: forcing install"); CPAN::Shell->force("install", $mod) if (not $query); } elsif (not defined($mm[0]->inst_version)) { banner("$mod not installed: forcing install"); CPAN::Shell->force("install", $mod) if (not $query); } elsif ($mm[0]->inst_version eq 'undef') { banner("Cannot determine version number for $mod: forcing install"); CPAN::Shell->force("install", $mod) if (not $query); } elsif ($mm[0]->inst_version lt $mm[0]->cpan_version) { if ($noforce) { banner(sprintf("Upgrade required for %s from %s to %s: installing", $mod, $mm[0]->inst_version, $mm[0]->cpan_version)); CPAN::Shell->install($mod) if (not $query); } else { banner(sprintf("Upgrade required for %s from %s to %s: forcing install", $mod, $mm[0]->inst_version, $mm[0]->cpan_version)); CPAN::Shell->force("install", $mod) if (not $query); }; } else { banner(sprintf("Module %s is up to date at version %s", $mod, $mm[0]->inst_version)); }; }; # if ($force) { # CPAN::Shell->force("install", $bundle); # } else { # CPAN::Shell->upgrade($bundle); # }; sub bundle_list { my $thisbundle = $_[0] || $bundle; $thisbundle =~ s{::}{/}; ($thisbundle .= '.pm') if ($thisbundle !~ m{\.pm\z}); banner("Reading $thisbundle"); ## snarf modules from the CONTENTS section my ($flag, @modlist) = (0, ()); open(my $BUN, $thisbundle) or die "could not find $thisbundle\n"; while (<$BUN>) { $flag = 1, next if (m{=head1\s+CONTENTS}); $flag = 0, next if (m{=head1}); next if not $flag; next if (m{^\s*$}); chomp; push @modlist, $_; }; close $BUN; return @modlist; }; sub vnum { my ($v) = @_; my @parts = split(/\./, $v); $parts[2] ||= 0; return sprintf("%d.%3.3d%3.3d", @parts); }; sub banner { my ($string) = @_; my $l = length($string) + 2; my $sep = "+" . "=" x $l . "+" . $/; print $/, $sep, "| $string |$/", $sep; }; sub quickhelp { print " install_prereqs : use CPAN to download and install all of the prerequisites for the $this_package package --noforce, -n do not force installation when \"make test\" fails --proxy= specify a proxy server and port (for instance, at Brookhaven, the proxy is \"192.168.1.140:3128\") --system, -s use your system-wide CPAN configuration --query, -q query about which modules need to be updated --bundle= specify a prerequisite Bundle (default = Bundle/${this_package}Bundle.pm) -h short help message --help long help message " }; sub copyem { print "!!! HEY !!! This is for maintenance on Bruce's computers. Do you really need to be here?\n"; foreach my $place (qw(horae demeter aug libperlxray)) { next if (cwd =~ m{$place}); print " copying to $place ... "; my $ok = copy($0, "../$place/install_prereqs"); ($ok) ? print "ok\n" : print "failed\n"; }; }; =head1 NAME install_prereqs - install prerequisites from CPAN without configuring CPAN =head1 SYNOPSIS This script automates the installation of the prerequisites for Bruce Ravel's XAS-related software efforts, including: =over 4 =item I Athena, Artemis, and Hephaestus =item I Perl tools for writing Ifeffit-based programs =item I The Xray::Absorption, Xray::Scattering, Chemistry::Formula, and Ifeffit modules =item I Use the L