| 1 |
#!/usr/bin/perl -I./ |
| 2 |
# install prerequisites from CPAN without configuring CPAN |
| 3 |
|
| 4 |
=for Copyright |
| 5 |
. |
| 6 |
Copyright (c) 2007-2008 Bruce Ravel (bravel AT bnl DOT gov). |
| 7 |
All rights reserved. |
| 8 |
. |
| 9 |
This file is free software; you can redistribute it and/or |
| 10 |
modify it under the same terms as Perl itself. See The Perl |
| 11 |
Artistic License. |
| 12 |
. |
| 13 |
This program is distributed in the hope that it will be useful, |
| 14 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 16 |
|
| 17 |
=cut |
| 18 |
|
| 19 |
use strict; |
| 20 |
use warnings; |
| 21 |
use CPAN; |
| 22 |
use Cwd qw(cwd); |
| 23 |
use Getopt::Long; |
| 24 |
use File::Copy; |
| 25 |
use File::Spec; |
| 26 |
use Pod::Text; |
| 27 |
|
| 28 |
BEGIN { |
| 29 |
## overload any existing CPAN configuration with one "guaranteed" to work |
| 30 |
unshift @INC, "./"; |
| 31 |
$ENV{__INSTALLING_BRPERL} = cwd(); |
| 32 |
unshift @INC, "./CPAN"; |
| 33 |
require MyConfig; |
| 34 |
} |
| 35 |
|
| 36 |
my $bundir = File::Spec->catfile(cwd, "Bundle/"); |
| 37 |
opendir(my $BUNDLE, $bundir) or die "could not open directory $bundir for reading"; |
| 38 |
my @files = grep {/pm$/} readdir $BUNDLE; |
| 39 |
closedir $BUNDLE; |
| 40 |
my $this_package = q{}; |
| 41 |
foreach my $f (@files) { |
| 42 |
($this_package = $1), last if ($f =~ m{(\w*)Bundle\.pm}); |
| 43 |
}; |
| 44 |
|
| 45 |
my ($noforce, $help, $quickhelp, $system, $query, $copy, $proxy, $bundle) = |
| 46 |
(0, 0, 0, 0, 0, 0, q{}, "Bundle::".$this_package."Bundle"); |
| 47 |
GetOptions("noforce" => \$noforce, "n" => \$noforce, |
| 48 |
"bundle=s" => \$bundle, |
| 49 |
"proxy=s" => \$proxy, # q[192.168.1.140:3128] at BNL |
| 50 |
"system" => \$system, "s" => \$system, |
| 51 |
"query" => \$query, "q" => \$query, |
| 52 |
"help" => \$help, "h" => \$quickhelp, |
| 53 |
"c" => \$copy, |
| 54 |
); |
| 55 |
|
| 56 |
## small chores then exit |
| 57 |
copyem(), exit if $copy; |
| 58 |
quickhelp(), exit if $quickhelp; |
| 59 |
Pod::Text -> new(sentence => 0, width => 78) -> parse_from_file($0), exit if $help; |
| 60 |
|
| 61 |
## remove the overloaded CPAN configuration and import the normal one |
| 62 |
if ($system) { |
| 63 |
shift @INC; |
| 64 |
require CPAN::Config; |
| 65 |
import CPAN::Config; |
| 66 |
}; |
| 67 |
banner("CPAN working directory:" . $CPAN::Config->{cpan_home}); |
| 68 |
# print join($/, @INC), $/; |
| 69 |
# exit; |
| 70 |
|
| 71 |
## specify a proxy server |
| 72 |
if ($proxy) { |
| 73 |
$CPAN::Config->{http_proxy} = $proxy; |
| 74 |
$CPAN::Config->{ftp_proxy} = $proxy; |
| 75 |
}; |
| 76 |
|
| 77 |
my @modlist = bundle_list($bundle); |
| 78 |
foreach my $mod (@modlist) { |
| 79 |
my @mm = CPAN::Shell->expand("Module", "/\^$mod\$/"); |
| 80 |
|
| 81 |
if (not defined($mm[0])) { |
| 82 |
banner("Cannot determine version number for $mod: forcing install"); |
| 83 |
CPAN::Shell->force("install", $mod) if (not $query); |
| 84 |
|
| 85 |
} elsif (not defined($mm[0]->inst_version)) { |
| 86 |
banner("$mod not installed: forcing install"); |
| 87 |
CPAN::Shell->force("install", $mod) if (not $query); |
| 88 |
|
| 89 |
} elsif ($mm[0]->inst_version eq 'undef') { |
| 90 |
banner("Cannot determine version number for $mod: forcing install"); |
| 91 |
CPAN::Shell->force("install", $mod) if (not $query); |
| 92 |
|
| 93 |
} elsif ($mm[0]->inst_version lt $mm[0]->cpan_version) { |
| 94 |
if ($noforce) { |
| 95 |
banner(sprintf("Upgrade required for %s from %s to %s: installing", |
| 96 |
$mod, $mm[0]->inst_version, $mm[0]->cpan_version)); |
| 97 |
CPAN::Shell->install($mod) if (not $query); |
| 98 |
} else { |
| 99 |
banner(sprintf("Upgrade required for %s from %s to %s: forcing install", |
| 100 |
$mod, $mm[0]->inst_version, $mm[0]->cpan_version)); |
| 101 |
CPAN::Shell->force("install", $mod) if (not $query); |
| 102 |
}; |
| 103 |
|
| 104 |
} else { |
| 105 |
banner(sprintf("Module %s is up to date at version %s", $mod, $mm[0]->inst_version)); |
| 106 |
|
| 107 |
}; |
| 108 |
|
| 109 |
}; |
| 110 |
|
| 111 |
# if ($force) { |
| 112 |
# CPAN::Shell->force("install", $bundle); |
| 113 |
# } else { |
| 114 |
# CPAN::Shell->upgrade($bundle); |
| 115 |
# }; |
| 116 |
|
| 117 |
sub bundle_list { |
| 118 |
my $thisbundle = $_[0] || $bundle; |
| 119 |
$thisbundle =~ s{::}{/}; |
| 120 |
($thisbundle .= '.pm') if ($thisbundle !~ m{\.pm\z}); |
| 121 |
banner("Reading $thisbundle"); |
| 122 |
|
| 123 |
## snarf modules from the CONTENTS section |
| 124 |
my ($flag, @modlist) = (0, ()); |
| 125 |
open(my $BUN, $thisbundle) or die "could not find $thisbundle\n"; |
| 126 |
while (<$BUN>) { |
| 127 |
$flag = 1, next if (m{=head1\s+CONTENTS}); |
| 128 |
$flag = 0, next if (m{=head1}); |
| 129 |
next if not $flag; |
| 130 |
next if (m{^\s*$}); |
| 131 |
chomp; |
| 132 |
push @modlist, $_; |
| 133 |
}; |
| 134 |
close $BUN; |
| 135 |
|
| 136 |
return @modlist; |
| 137 |
}; |
| 138 |
|
| 139 |
sub vnum { |
| 140 |
my ($v) = @_; |
| 141 |
my @parts = split(/\./, $v); |
| 142 |
$parts[2] ||= 0; |
| 143 |
return sprintf("%d.%3.3d%3.3d", @parts); |
| 144 |
}; |
| 145 |
|
| 146 |
|
| 147 |
sub banner { |
| 148 |
my ($string) = @_; |
| 149 |
my $l = length($string) + 2; |
| 150 |
my $sep = "+" . "=" x $l . "+" . $/; |
| 151 |
print $/, |
| 152 |
$sep, |
| 153 |
"| $string |$/", |
| 154 |
$sep; |
| 155 |
}; |
| 156 |
|
| 157 |
sub quickhelp { |
| 158 |
print " |
| 159 |
|
| 160 |
install_prereqs : use CPAN to download and install all of the prerequisites for the |
| 161 |
$this_package package |
| 162 |
|
| 163 |
--noforce, -n do not force installation when \"make test\" fails |
| 164 |
--proxy=<URL:PORT> specify a proxy server and port (for instance, at Brookhaven, |
| 165 |
the proxy is \"192.168.1.140:3128\") |
| 166 |
--system, -s use your system-wide CPAN configuration |
| 167 |
--query, -q query about which modules need to be updated |
| 168 |
--bundle=<bundle> specify a prerequisite Bundle (default = Bundle/${this_package}Bundle.pm) |
| 169 |
-h short help message |
| 170 |
--help long help message |
| 171 |
|
| 172 |
" |
| 173 |
}; |
| 174 |
|
| 175 |
|
| 176 |
sub copyem { |
| 177 |
print "!!! HEY !!! This is for maintenance on Bruce's computers. Do you really need to be here?\n"; |
| 178 |
foreach my $place (qw(horae demeter aug libperlxray)) { |
| 179 |
next if (cwd =~ m{$place}); |
| 180 |
print " copying to $place ... "; |
| 181 |
my $ok = copy($0, "../$place/install_prereqs"); |
| 182 |
($ok) ? print "ok\n" : print "failed\n"; |
| 183 |
}; |
| 184 |
}; |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
=head1 NAME |
| 189 |
|
| 190 |
install_prereqs - install prerequisites from CPAN without configuring CPAN |
| 191 |
|
| 192 |
=head1 SYNOPSIS |
| 193 |
|
| 194 |
This script automates the installation of the prerequisites for Bruce |
| 195 |
Ravel's XAS-related software efforts, including: |
| 196 |
|
| 197 |
=over 4 |
| 198 |
|
| 199 |
=item I<Horae> |
| 200 |
|
| 201 |
Athena, Artemis, and Hephaestus |
| 202 |
|
| 203 |
=item I<Demeter> |
| 204 |
|
| 205 |
Perl tools for writing Ifeffit-based programs |
| 206 |
|
| 207 |
=item I<libperlxray> |
| 208 |
|
| 209 |
The Xray::Absorption, Xray::Scattering, Chemistry::Formula, and Ifeffit modules |
| 210 |
|
| 211 |
=item I<The Athena User's Guide> |
| 212 |
|
| 213 |
Use the L<Template Toolkit|http://template-toolkit.org/> to generate documentation as html, PDF, and |
| 214 |
pod. |
| 215 |
|
| 216 |
=back |
| 217 |
|
| 218 |
=head1 USAGE |
| 219 |
|
| 220 |
This script almost certainly needs to be run as root or using sudo, as |
| 221 |
in |
| 222 |
|
| 223 |
sudo install_prereqs |
| 224 |
|
| 225 |
See below for descriptions of the command line options. |
| 226 |
|
| 227 |
=head1 DESCRIPTION |
| 228 |
|
| 229 |
Configuring CPAN is a hassle for the experienced and flumoxing for |
| 230 |
everyone else. Nonetheless, using CPAN to install prerequisites is |
| 231 |
desirable. It is the best way to guarantee that the user ends up with |
| 232 |
the latest and greatest versions of the Perl modules that the various |
| 233 |
software packages depend upon. |
| 234 |
|
| 235 |
To address this, each of Bruce's packages ships with this script along |
| 236 |
with a Bundle file defining the list of required modules and a CPAN |
| 237 |
configuration file which uses the current working directory to build |
| 238 |
the files and C<http://www.perl.com/CPAN/> as the download source. |
| 239 |
These are found in the F<Bundle/> and F<CPAN/> folders. |
| 240 |
|
| 241 |
For those who have already configured CPAN in the normal, the use of |
| 242 |
the configuration file that comes with this script can be overridden |
| 243 |
by the C<--system> flag. Most people should be happy with the CPAN |
| 244 |
configuration that comes with the package. |
| 245 |
|
| 246 |
This script attempts to avoid installing modules that are up to date. |
| 247 |
It's not always successful in determining versioning, so it errs on |
| 248 |
the side of installing. |
| 249 |
|
| 250 |
For those behind a proxy, the proxy can be specified from the command |
| 251 |
line. For instance, those at Brookhaven National Laboratory will want |
| 252 |
to use C<--proxy=192.168.1.140:3128> at the command line. |
| 253 |
|
| 254 |
=head1 COMMAND LINE OPTIONS |
| 255 |
|
| 256 |
=over 4 |
| 257 |
|
| 258 |
=item C<--noforce>, C<-n> |
| 259 |
|
| 260 |
Do not force the installation of any modules. That is, do not install |
| 261 |
modules when the "make test" stage fails. The default is to force |
| 262 |
installation even in the event of test failures. |
| 263 |
|
| 264 |
=item C<--proxy=URL:PORT> |
| 265 |
|
| 266 |
Specify a proxy server and port. For instance, at Brookhaven, the |
| 267 |
proxy is "192.168.1.130:3128". |
| 268 |
|
| 269 |
=item C<--query>, C<-q> |
| 270 |
|
| 271 |
Query about which modules will be updated, reporting to the screen |
| 272 |
what would be done, but do not actually install anything. |
| 273 |
|
| 274 |
=item C<--system>, C<-s> |
| 275 |
|
| 276 |
Use your system-wide CPAN configuration. This is appropriate for |
| 277 |
someone who has already configured CPAN for his computer, presumably |
| 278 |
using a fast, geographically close CPAN mirror and other local |
| 279 |
configurations. |
| 280 |
|
| 281 |
=item C<--bundle=/path/to/BundleFile.pm> |
| 282 |
|
| 283 |
Specify a prerequisite Bundle other than the default which ships with |
| 284 |
the package. |
| 285 |
|
| 286 |
=item C<--help> |
| 287 |
|
| 288 |
Write this POD to STDOUT. |
| 289 |
|
| 290 |
=item C<-h> |
| 291 |
|
| 292 |
Show a quick summary of command line options. |
| 293 |
|
| 294 |
=back |
| 295 |
|
| 296 |
=head1 COPYRIGHT |
| 297 |
|
| 298 |
Copyright (c) 2007-2008 Bruce Ravel (bravel AT bnl DOT gov). |
| 299 |
All rights reserved. |
| 300 |
|
| 301 |
This file is free software; you can redistribute it and/or |
| 302 |
modify it under the same terms as Perl itself. See The Perl |
| 303 |
Artistic License. |
| 304 |
|
| 305 |
This program is distributed in the hope that it will be useful, |
| 306 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 307 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 308 |
|
| 309 |
=cut |
| 310 |
|