Debian Codegraph @

attic/scripts/horae_update

raw
1 #!/usr/bin/perl -w
2 ######################################################################
3 ## horae_update: network updater for Athena, Artemis, and Hephaestus
4 ##
5 ## horae_update is copyright (c) 2004-2005 Bruce Ravel
6 ## ravel _A_T_ phys.washington.edu
7 ## http://cars.uchicago.edu/~ravel/
8 ##
9 ## The latest versions of Athena, Artemis, and horae_update
10 ## can always be found at
11 ## http://cars.uchicago.edu/~ravel/software/
12 ##
13 ## -------------------------------------------------------------------
14 ## All rights reserved. This program is free software; you can
15 ## redistribute it and/or modify it provided that the above notice
16 ## of copyright, these terms of use, and the disclaimer of
17 ## warranty below appear in the source code and documentation, and
18 ## that none of the names of The Argonne National Laboratory, The
19 ## University of Chicago, University of Washington, or the authors
20 ## appear in advertising or endorsement of works derived from this
21 ## software without specific prior written permission from all
22 ## parties.
23 ##
24 ## THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 ## EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
26 ## OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 ## NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
28 ## HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
29 ## WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
30 ## FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
31 ## OTHER DEALINGS IN THIS SOFTWARE.
32 ## -------------------------------------------------------------------
33 ######################################################################
34 ##
35 ## This script is for unix systems (including Mac OSX). This is not
36 ## the way to update the horae package on Windoze machine.
37 ##
38 ######################################################################
39
40
41 ##use strict;
42 use Getopt::Long;
43 use XML::Simple;
44
45 my ($force, $file, $proxy, $timeout, $mirror, $auto, $prefix) = (0,0,"",30,"",0,"");
46 &GetOptions(help => \&usage,
47 h => \&usage,
48 force => \$force,
49 auto => \$auto,
50 "file=s" => \$file,
51 "proxy=s" => \$proxy,
52 "mirror=s" => \$mirror,
53 "prefix=s" => \$prefix,
54 );
55 ($force = 1) if (-e $file);
56
57 sub usage {
58 print <<EOH
59 horae_update : automated web updater for athena, artemis, and hephaestus
60
61 usage: horae_update [--force] [--proxy=<URL>] [--timeout=<seconds>]
62 [--mirror=<mirror>] [--file=<tarball>] [--help] [-h]
63
64 option effect
65 -----------------------------------
66 --help, -h print usage message and exit
67 --proxy specify a proxy server
68 --timeout specify a timeout in seconds (default=30)
69 --mirror SourceForge mirror known to carry the tarball
70 --file specify previously downloaded tarball
71 --force download and install, igoring comparison of version
72 numbers on the server and the local machine
73 --auto loop through available mirrors to find a download,
74 this is useful for cron jobs
75 --prefix specify an installation location
76
77 The only good way to know which mirror might be carrying the tarball
78 is to run this script once or to check on the SourceForge web site.
79
80 Do "perldoc horae_update" for more information
81
82 EOH
83 ;
84 exit;
85 };
86
87 ## need to see what version of the tarball is already installed,
88 ## taking care with the cases of it not being installed and of a
89 ## version prior to 020 being installed
90 my $already_installed = eval "require Ifeffit::Tools;";
91 my $installed_version = 0;
92 if ($already_installed) {
93 no warnings;
94 import Ifeffit::Tools;
95 $installed_version = $Ifeffit::Tools::VERSION;
96 ($installed_version = 0) if ($installed_version =~ /^\s*$/);
97 if ($installed_version =~ /(.+)(\d)rc(\d+)/) {
98 ## if the installed version is 034rc1, this is translated to
99 ## 033.1. that way it will get installed if 033 in on the
100 ## machine, but not if 034 is on.
101 $installed_version = $1 . $2-1 . "." . $3;
102 };
103 };
104
105 ## write progress to a log file
106 $| = 1;
107 open STDOUT, "| tee horae_update.log";
108
109
110 ## set the sourceforge mirror
111 #$mirror ||= 'aleron';
112 #$mirror = lc($mirror);
113 # my %location = (aleron => "Reston, VA, USA",
114 # belnet => "Brussels, Belgium",
115 # umn => "Minneapolis, MN, USA",
116 # unc => "Chapel Hill, NC, USA",
117 # heanet => "Dublin, Ireland",
118 # ovh => "Paris, France",
119 # puzzle => "Bern, Switzerland",
120 # optusnet => "Sydney, Australia",
121 # voxel => "New York, New York, USA",
122 # );
123 # my $location_regex = join("|",keys(%location));
124 # ($mirror = 'aleron') unless ($mirror =~ /^($location_regex)$/);
125
126
127 print STDOUT " = Horae Updater (using LWP::UserAgent) version 0.11\n";
128 print STDOUT " = Attempting SourceForge mirror $mirror\n" if ($mirror);
129 print STDOUT " = Using proxy server $proxy\n" if $proxy;
130 print STDOUT " = Timeout = $timeout seconds\n";
131
132 ## We are going to need LWP. Check to see if it is there, if it is
133 ## not and root is running this script, fetch LWP from CPAN.
134 unless (defined (eval "require LWP::UserAgent;")) {
135 if ( $> ) {
136 print STDOUT <<EOH
137
138 *** You need to install perl\'s web services modules.
139 *** The easiest way to do this is to become root and
140 *** then issue this command:
141 *** perl -MCPAN -e shell
142 *** then, at the CPAN prompt, type
143 *** install LWP
144
145 EOH
146 ;
147 die "\n";
148 } else {
149 print STDOUT <<EOH
150
151 = Hmmm.... you do not seem to have perl\'s web services installed
152 = I am going to attempt to load the CPAN module and grab LWP
153 = from a nearby CPAN site. If you have never used CPAN before,
154 = you may need to answer some questions. This requires that you
155 = have access to the internet.
156
157 EOH
158 ;
159 print STDOUT "* Shall I go ahead and try CPAN? (y/n) ";
160 my $yn = <>;
161 exit unless ($yn =~ /^y/i);
162 require CPAN;
163 CPAN::Shell->install("LWP");
164 };
165 unless (defined (eval "require LWP::UserAgent;")) {
166 die " = Apparently attempting to fetch LWP from CPAN didn't work. I give up!\n";
167 };
168 };
169
170
171 ## since we have successfully imported LWP, set up the user agent for
172 ## use in this transfer, set the prozy and timeout
173 import LWP::UserAgent;
174 my $ua = LWP::UserAgent->new;
175 ## what about the HTTP_PROXY or CGI_HTTP_PROXY environment variables...?
176 $ua->proxy('http', $proxy) if $proxy;
177 $ua->timeout($timeout);
178
179 ## set some variables about where to find the horae tarball on the web
180 my %horae = (site => "feff.phys.washington.edu", # deprecated
181 path => "software/exafs/packages", # deprecated
182 dir => 'http://sourceforge.net/export/rss2_projfiles.php?group_id=80919',
183 latest => 0,
184 );
185
186 ## fetch a directory listing from the main SF site
187 print STDOUT " = Attempting to fetch a release listing from\n = $horae{dir}\n";
188 my $content;
189 my $rss; # hash_ref holding contents of RSS file from SF
190 my $response = $ua->get($horae{dir});
191 if ($response->is_success) {
192 $content = $response->content;
193 $rss = XMLin($content); # slurp RSS file into hash
194 } else {
195 print STDOUT $response -> message;
196 die "could not fetch $horae{dir} from server\n";
197 };
198
199 ## compare the SF version with the version on this computer
200 $horae{latest} = $1 if ($rss->{channel}->{item}->[0]->{title} =~ /(horae-\d+)/);
201 die " *** Yikes! Apparently no horae tarballs were found at SourceForge!\n" unless $horae{latest};
202 my $version = (split(/-/, $horae{latest}))[1];
203 unless ($force) {
204 unless ($version > $installed_version) {
205 warn "\n** Well, the current version on the server is $horae{latest} and you\n";
206 warn " appear to be running horae-$Ifeffit::Tools::VERSION.\n";
207 die " exiting...\n";
208 };
209 };
210
211 ## dispatch a note about the up/downgrade to come
212 print STDOUT "\n* Found the latest version as $horae{latest}\n";
213 if ($version > $installed_version) {
214 print STDOUT " Upgrading from version horae-$Ifeffit::Tools::VERSION\n\n";
215 } else {
216 print STDOUT " Downgrading from version horae-$Ifeffit::Tools::VERSION\n\n";
217 };
218
219
220 ## the --force option should override using the file found in the CWD
221 ## (see if/elsif block just below)
222 if ($force and $file){
223 print STDOUT "* Forcing installation of $file, as requested\n\n";
224 } elsif ($force) {
225 unlink "$horae{latest}.tar.gz" if (-e "$horae{latest}.tar.gz");
226 print STDOUT "* Forcing installation of $horae{latest}.tar.gz from the server, as requested\n\n";
227 };
228
229
230 my $used_preexisting = 0;
231 my $found_tarbell = 1;
232 if ($file and (-e $file) and (-s $file)) {
233 print STDOUT "* using tarball $file as requested\n";
234 $horae{latest} = $1 if ($file =~ /(.+)\.tar\.gz/);
235 $used_preexisting = 1;
236 } elsif ((-e "$horae{latest}.tar.gz") and (-s "$horae{latest}.tar.gz")) {
237 print STDOUT "* It seems you have already downloaded the tarball. I'll use the\n";
238 print STDOUT " one that's already here\n";
239 $used_preexisting = 1;
240 } elsif (not $mirror) {
241 $found_tarball = 0;
242 } else {
243 ## unlink any previous attempt to download this file, unless explicitly using the existing version
244 unlink "$horae{latest}.tar.gz";
245 my $success = fetch($mirror, $horae{latest});
246 if ($success) {
247 open ISHTML, "$horae{latest}.tar.gz";
248 my $this = <ISHTML>;
249 ($found_tarball = 0) if ($this =~ /html/i);
250 close ISHTML;
251 unlink "$horae{latest}.tar.gz";
252 }
253 };
254
255 if (not $found_tarball) {
256 ## if the selected server responds but does not have the tarball, then
257 ## SF responds with a web page asking the user to choose a different
258 ## mirror. I have to scrape this web page to get a selection of
259 ## available mirrors. The --auto flag will loop through these until
260 ## it finds the tarball, otherwise a menu is presented to the user.
261 $ua -> mirror("http://prdownloads.sourceforge.net/ifeffit/$horae{latest}.tar.gz?download",
262 "...horae.sites");
263 my @sites = ();
264 my $choices = "";
265 my $i = 0;
266 open F, "...horae.sites";
267 my $mirrors = 0;
268 while (<F>) {
269 ($mirrors = 1) if (/Download Mirrors/);
270 ($mirrors = 2) if (/Select Preferred Mirror/);
271 last if ($mirrors == 2);
272 next if ($mirrors == 0);
273 next unless (/<td><a href=/);
274 ++$i;
275 my $city = <F>;
276 $city = substr($city, 6, -6);
277 $city =~ s/^\s+//;
278 my $continent = <F>;
279 $continent = substr($continent, 6, -6);
280 my $name = <F>;
281 ($name = $1) if ($name =~ /use_mirror=(.+)\"/);
282 my $url = $name . ".dl.sourceforge.net/sourceforge/ifeffit/";
283 push @sites, [$city, $continent, $name, $url];
284 $choices .= sprintf "%2d %-27s %-15s (%s)\n", $i, $city, $continent, $name;
285 };
286 close F;
287 unlink ("...horae.sites");
288 die "\n\nCould not find any SourceForge servers with the latest tarball.\nYou might want to try manually downloading from SourceForge.\n" unless @sites;
289
290 my $choice = 0;
291 if ($auto) {
292 foreach my $s (@sites) {
293 print "* Trying to fetch from $s->[2] in $s->[0]\n";
294 my $success = fetch($s->[2], $horae{latest});
295 last if $success;
296 };
297 die "Could not download the tarball on any server\n" unless (-e "$horae{latest}.tar.gz");
298 } else {
299 while (($choice < 1) or ($choice > $i)) {
300 print "\n\nThe horae tarball has not yet been found.\n\n";
301 print "Please choose from one of the following servers: (1 - $i or q to quit)\n\n";
302 print $choices;
303 print "Your choice> ";
304 $choice = <STDIN>;
305 exit if (lc($choice) =~ /^\s*[eqx]/);
306 ($choice = 0) unless ($choice =~ /\d+/);
307 };
308 my $cc = $choice -1;
309 print "\nYou chose $sites[$cc]->[0]\n";
310 my $success = fetch($sites[$cc]->[2], $horae{latest});
311 };
312 };
313
314 if ($used_preexisting) {
315 print STDOUT "** Extracting package files from $horae{latest}.tar.gz\n\n";
316 my $unpack = system "gzip -dc $horae{latest}.tar.gz | tar xf -";
317 ## test return value of that system call
318 if ($unpack) {
319 warn "** Uh oh! There was trouble unpacking the pre-existing tarball.\n";
320 die " exiting...\n";
321 };
322 };
323
324
325 die " AARGH! Could not find a copy of the tarball at any SourceForge server!\n"
326 unless ((-e "$horae{latest}.tar.gz") or $used_preexisting);
327
328 ## the package has been downloaded and unpacked at this point, so cd
329 ## to the directory and build the package
330 print STDOUT "\n* Changing directory to $horae{latest}\n\n";
331 chdir $horae{latest};
332 print STDOUT "* Beginning build incantation \"perl Makefile.PL; make; make install\"\n";
333 print STDOUT "** (perl Makefile.PL)\n";
334 if ($prefix) {
335 @ARGV = ("--prefix=$prefix");
336 print STDOUT " ** installing to prefix=$prefix\n";
337 };
338 do "Makefile.PL";
339 print STDOUT "** (make)\n";
340 system "make";
341 if ( $> ) {
342 print STDOUT <<EOH
343
344 ** You must be root to install the horae programs.
345 Become root, then issue this command:
346 make install
347 in the $horae{latest} directory.
348
349 EOH
350 ;
351 } else {
352 print STDOUT "** (make install)\n";
353 system "make install";
354 };
355
356 ## all done!
357 print STDOUT "\n\n* All done!\n";
358
359
360 ## attempt to download and unpack a tarball. return 1 upon success.
361 ## return 0 is the tarball cannot be downloaded or if the file
362 ## downloaded is not a tarball.
363 sub fetch {
364 ## args: SF mirror, horae-NNN version
365 my ($site, $horae) = @_;
366 my $url = 'http://' . $site .'.dl.sourceforge.net/sourceforge/ifeffit/' . $horae . '.tar.gz';
367 print STDOUT "* Attempting to fetch the latest tarball from\n $url\n";
368 my $response = $ua->mirror("$url", "$horae.tar.gz");
369 if ($response->is_success) {
370 print STDOUT "\n** Wrote file $horae.tar.gz\n";
371 ## unzip and untar the tarball
372 print STDOUT "** Extracting package files from $horae.tar.gz\n";
373 my $unpack = system "gzip -dc $horae.tar.gz | tar xf -";
374 ## test return value of that system call
375 if ($unpack) {
376 print STDOUT "** Uh oh! That's not a tarball!\n";
377 return 0;
378 };
379 return 1;
380 } else {
381 print STDOUT "** ", $response -> message, $/;
382 print STDOUT "** Could not fetch $horae.tar.gz from $site\n";
383 return 0;
384 };
385 };
386
387
388 __END__
389
390
391 =head1 NAME
392
393 HORAE_UPDATE - A network updater for Athena, Artemis, and Hephaestus
394
395 =head1 SYNOPSIS
396
397 horae_update [--force] [--proxy=<URL>] [--timeout=<seconds>]
398 [--mirror=<mirror>] [--auto] [--file=<tarball>]
399 [--prefix=<path>] [--help] [-h]
400
401
402 The horae_update script is used to check a web repository for the
403 latest version of the horae package and download that package if it is
404 more recent than what is installed on the local machine. This script
405 can be run by hand from the command line or as a periodic, scheduled
406 process (such as a cron job).
407
408 The script gets the listing of horae package releases from SourceForge
409 and scrapes that web page for the most recent release. If the local
410 machine needs to be updated, a SourceForge mirror will be contacted
411 for downloading the package. Once downloaded, this script will upack
412 the package and install it using the standard procedures for
413 installing perl packages.
414
415 The full installation requires that the script is run as root. If run
416 as a normal user, the package will be downloaded, upacked, and built,
417 but not installed.
418
419 The script uses the LWP package, which is the set of Perl modules for
420 doing network programming. If they are not found on the local
421 machine, the CPAN module will be run in an attempt to download and
422 install LWP. That requires running the script as root.
423
424 This is horae_update version 0.11.
425
426 =head1 COMMAND LINE OPTIONS
427
428 =over 4
429
430 =item --help, -h
431
432 Write a note about the command line switches to the screen and quit.
433
434 =item --force
435
436 This causes the script to ignore the comparison between the currently
437 installed version and the version on SourceForge. With this switch,
438 the most recent version on SourceForge will be downloaded and
439 installed regardless of the version on the local machine.
440
441 =item --proxy=<URL>
442
443 If the local machine must connect to a proxy, use this command line
444 argument to specify the proxy server. The argument should be the URL
445 of the proxy server.
446
447 =item --timeout=<time_in_seconds>
448
449 Set the timeout for the user agent. The default is 30 seconds. After
450 this time, the agent will give up on the current SourceForge server
451 and try the next one in the list. If you make this too short, it is
452 possible that no server will work. If you make it too long, you may
453 get bored waiting for the updater to finish. You may consider setting
454 this longer than 30 seconds if you are in a continent without a
455 SourceForge server.
456
457 =item --mirror=<site>
458
459 Specify a SourceForge server known to carry the tarball. This flag
460 can be used to avoid the interactive step of choosing the mirror from
461 the list of available mirrors. If the tarball is not carried by that
462 server, the interactive step will happen. See the C<--auto> flag for
463 another way of avoiding the interactive step.
464
465 =item --auto
466
467 If the tarball is not found at the selected mirror, SourceForge
468 returns a web page asking for a selection from a list of mirrors that
469 do have the tarball. The default behavior of the script is to present
470 a menu asking you to choose from the available servers. If
471 horae_update is run with the --auto flag, it will loop through the
472 available servers until the tarball is found. The advantage of the
473 --auto flag is that is makes horae_update suitable for a cron job.
474 The disadvantage is that it may try to grab the tarball from someplace
475 very distant. If you run the updater by hand, I recommend you not use
476 this flag. If you run it as a cron job, you B<must> use this flag.
477
478 =item --file=<file>
479
480 If you have downloaded the latest package by some other means, you can
481 specify it with this argument. In that case, the downloading is
482 skipped and this file is unpacked, built, and installed.
483
484 =item --prefix=</installation/path>
485
486 If you want the horae package installed to a particular location, use
487 this command line argument. It's value is passed along to
488 Makefile.PL, which will use this as the installation location and
489 write out short scripts for inclusion in your login scrips.
490
491 =back
492
493 =head1 TO DO
494
495 =over 4
496
497 =item 1
498
499 Use Archive::Tar rather than a system call for unpacking the tarball.
500 Note that A::T has been installed if the CPAN has been installed.
501
502 =back
503
504 =head1 RELEASE HISTORY
505
506 0.11 (11 December 2005) Using RSS file from SF to determine current
507 version number. Modified interaction with mirrors. Tweak
508 screen scraper for determining mirrors carrying tarball.
509 0.10 (14 September 2005) Added a --prefix flag that gets passed
510 along to Makefile.PL
511 0.9 (25 April, 2005) Added a screen scraper to present a menu of
512 available mirrors if the default does nothave the tarball.
513 Added the --auto flag.
514 0.8 (13 October 2004) Fixed a print-related bug that prevented the
515 script from doing the "make install" step. Removed the --devel
516 switch.
517 0.7 (3 August, 2004) Update to include new SF servers. Cycle
518 through SF servers looking for one that answers. Added a
519 configurable timeout. Added the --devel switch.
520 0.6 (25 May, 2004) Switch to LWP::UserAgent and add --proxy
521 argument, use SourceForge rather than Univ. of Washington for
522 downloads, add --mirror argument, wrote a pod
523 0.5 (12 April, 2004) Handle installed versions with rc in their
524 version numbers
525 0.4 (11 December, 2003) Changed some language and added markup so
526 that the log can be read efficiently using outline-mode in emacs
527 0.3 (6 May, 2003) Compare version numbers on local host and on
528 server, also check to see if a tarball is already in CWD, allow
529 several options (--help, --force, and --file) using Getopt::Long,
530 test return value of system call to unpack archive
531 0.2 (31 January, 2003) Use getstore function and check HTTP status,
532 improved regex for distinguishing a tarball from any other file
533 with the string "horae" in it
534 0.1 (28 January, 2003) Initial release
535
536
537 =head1 AUTHOR
538
539 Bruce Ravel, [email protected]
540
541 L<http://cars.uchicago.edu/~ravel/software/exafs/>
542
543 copyright (c) 2004-2005 Bruce Ravel
544
545 =cut