| 1 |
#!/usr/bin/env perl |
| 2 |
|
| 3 |
# Public domain notice for all NCBI EDirect scripts is located at: |
| 4 |
# https://www.ncbi.nlm.nih.gov/books/NBK179288/#chapter6.Public_Domain_Notice |
| 5 |
|
| 6 |
use warnings; |
| 7 |
use strict; |
| 8 |
|
| 9 |
use File::Path; |
| 10 |
use Net::FTP; |
| 11 |
use POSIX qw(uname); |
| 12 |
|
| 13 |
my $cache_dir = "$ENV{HOME}/.cache/ncbi-converters"; |
| 14 |
if (defined $ENV{NCBI_CONVERTER_DIR}) { |
| 15 |
$cache_dir = $ENV{NCBI_CONVERTER_DIR}; |
| 16 |
} |
| 17 |
|
| 18 |
my $server = 'ftp.ncbi.nlm.nih.gov'; |
| 19 |
my $platform = DetectPlatform(); |
| 20 |
my $dir = "/toolbox/ncbi_tools/converters/by_platform/$platform"; |
| 21 |
my $ext = ($platform eq 'win') ? 'zip' : 'gz'; |
| 22 |
my $binext = ($platform eq 'win') ? '.exe' : ''; |
| 23 |
my $archive = "$ARGV[0].$platform.$ext"; |
| 24 |
my $executable = "$cache_dir/" . $ARGV[0] . $binext; |
| 25 |
|
| 26 |
if ( ! -d $cache_dir ) { |
| 27 |
File::Path::make_path($cache_dir) |
| 28 |
or die "Unable to ensure the existence of $cache_dir: $!"; |
| 29 |
} |
| 30 |
|
| 31 |
my $ftp = new Net::FTP($server, Passive => 1) |
| 32 |
or die "Unable to connect to FTP server: $!"; |
| 33 |
$ftp->login or die "Unable to log in to FTP server"; |
| 34 |
$ftp->cwd($dir) or die "Unable to change to $dir"; |
| 35 |
$ftp->binary or warn "Unable to set binary mode"; |
| 36 |
|
| 37 |
my $time = $ftp->mdtm($archive); |
| 38 |
my @stats = stat "$cache_dir/$ARGV[0]$binext"; |
| 39 |
if ( !@stats || $stats[9] < $time) { |
| 40 |
$ftp->get("$archive", "$cache_dir/$archive") |
| 41 |
or die "Unable to download $archive"; |
| 42 |
utime $time, $time, "$cache_dir/$archive"; |
| 43 |
my $pid = fork(); |
| 44 |
if ($pid < 0) { |
| 45 |
die "Unable to fork for unpacking: $!"; |
| 46 |
} elsif ($pid > 0) { |
| 47 |
waitpid($pid, 0); |
| 48 |
chmod(0777 &~ umask, $executable); |
| 49 |
utime $time, $time, $executable; |
| 50 |
} else { |
| 51 |
chdir($cache_dir); |
| 52 |
if ($platform eq 'win') { |
| 53 |
exec('unzip', $archive); |
| 54 |
} else { |
| 55 |
system('gunzip', '-n', $archive); |
| 56 |
rename("$ARGV[0].$platform", $ARGV[0]); |
| 57 |
exit 0; |
| 58 |
} |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
shift; |
| 63 |
exec($executable, @ARGV); |
| 64 |
|
| 65 |
sub DetectPlatform |
| 66 |
{ |
| 67 |
my @uname = uname(); |
| 68 |
my $OS = $uname[0]; |
| 69 |
my $CPU = $uname[4]; |
| 70 |
my $pf; |
| 71 |
my $last_built; |
| 72 |
if ($OS =~ /^CYGWIN/) { |
| 73 |
$pf = 'win'; |
| 74 |
} elsif ($OS eq 'Darwin') { |
| 75 |
$pf = 'mac'; |
| 76 |
} elsif ($OS eq 'Linux') { |
| 77 |
if ($CPU =~ /i\d86/) { |
| 78 |
$pf = 'linux32'; |
| 79 |
$last_built = 'November 2014'; |
| 80 |
} elsif ($CPU eq 'x86_64') { |
| 81 |
$pf = 'linux64'; |
| 82 |
} |
| 83 |
} elsif ($OS eq 'SunOS') { |
| 84 |
if ($CPU =~ /^s/) { |
| 85 |
$pf = 'solaris'; |
| 86 |
$last_built = 'March 2014'; |
| 87 |
} else { |
| 88 |
$pf = 'solaris-x86'; |
| 89 |
$last_built = 'September 2014'; |
| 90 |
} |
| 91 |
} else { |
| 92 |
die "No prebuilt binaries available for $OS/$CPU"; |
| 93 |
} |
| 94 |
|
| 95 |
if (defined $last_built) { |
| 96 |
warn "NCBI no longer builds for $OS/$CPU; using a binary from" |
| 97 |
. " $last_built"; |
| 98 |
} |
| 99 |
return $pf; |
| 100 |
} |