scripts/update_connectorsets.pl untested

filter: Statementstmt 0.0% 0 / 148 Branchbran 0.0% 0 / 40 Conditioncond 0.0% 0 / 31 MC/DCmcdc 0.0% 0 / 18 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 257 CC 6 SCAR 16.9
CC
6
Cov
0%
is_new
12.0
cat_date
12.0
modify_cnf_version
6.0
CRAP 5.4
1 # Updates the current connectorsets by
2 # downloading the new version of a database, unloading the old version and
3 # loading in the new version.
4  
5 use Data::Dumper;
6  
7 use Carp;
8 use lib qw( ../lib ../../..);
9 use Getopt::Long;
10 use File::Path;
11 use Bio::ConnectDots::Config;
12 use Bio::ConnectDots::DB;
13 use Net::FTP::Common;
14 use File::stat;
15 use strict;
16  
17 my($HELP,$VERBOSE,$ECHO_CMD,$DATABASE,$HOST,$USER,$PASSWORD,$LOADDIR,$LOADSAVE,$CONNECTORSET,$REMOVEDOTS,$VERSION,$REMOVEOLD,$CONNECTORSET);
18  
19 GetOptions ('help' => \$HELP,
20 'verbose' => \$VERBOSE,
21 'X|echo' => \$ECHO_CMD,
22 'database=s'=>\$DATABASE,
23 'db=s'=>\$DATABASE,
24 'host=s'=>\$HOST,
25 'user=s'=>\$USER,
26 'password=s'=>\$PASSWORD,
27 'loaddir=s'=>\$LOADDIR,
28 'loadsave=s'=>\$LOADSAVE,
29 'removeold=s'=>\$REMOVEOLD,
30 'connectorset=s'=>\$CONNECTORSET
31 ) and !$HELP or die <<USAGE;
32 Usage: $0 [options]
33  
34 Options
35 -------
36 --help Print this message
37 --verbose (for testing)
38 -X or --echo Echo command line (for testing and use in scripts)
39 --database Postgres database (default: --user)
40 --db Synonym for --database
41 --host Postgres database (default: socks)
42 --user Postgres user (default: ngoodman)
43 --password Postgres password (default: undef)
44 --loaddir Directory for load files (default: /usr/tmp/user_name)
45 --loadsave Specifies whether to save load files
46 Options: 'none', 'last', 'all'. Default: 'none'
47 --connectorset Specifies the name of the ConnectorSet to update (default is all)
48 --removeold automatically remove old connectorset version. Default is to retain.
49  
50 Options may be abbreviated. Values are case insenstive.
51  
52 USAGE
53 ;
54  
55 my $dbinfo = Bio::ConnectDots::Config::db('production');
56 $HOST or $HOST=$dbinfo->{host};
57 $USER or $USER=$dbinfo->{user};
58 $PASSWORD or $PASSWORD=$dbinfo->{password};
59 $DATABASE or die "Must provide a database to update: --database name";
60  
61 # locals
62 my ($current_year) = localtime() =~ /(....)$/;
63 my %month2num = ('jan',1,'feb',2,'mar',3,'apr',4,'may',5,'jun',6,'jul',7,'aug',8,'sep',9,'oct',10,'nov',11,'dec',12);
64  
65 print "### Checking for new versions of installed ConnectorSets\n";
66  
67 my $db=new Bio::ConnectDots::DB
68 (-database=>$DATABASE,-host=>$HOST,-user=>$USER,-password=>$PASSWORD,-ext_directory=>$LOADDIR);
69 my $dbh = $db->{dbh};
70  
71  
72 ### process list of connectorsets
73 my $iterator = $dbh->prepare("SELECT name,file_name,version,ftp,ftp_files FROM connectorset");
74 $iterator->execute();
75 while( my ($cs_name,$localfile,$version,$ftpsite,$ftp_files) = $iterator->fetchrow_array() ) {
76 if($ftpsite && $ftp_files) {
77 if($CONNECTORSET) { next unless $cs_name eq $CONNECTORSET; } # limit to one CS
78 print "### Checking for new version of $cs_name from $ftpsite\n";
79 my $directories = parse_files($ftp_files); # hash on directory of a list of files to get.
80
81 # determine version of local file
82 my $local_version = get_file_date($localfile);
83
84 # initialize the ftp server
85 my %netftp_cfg = (Debug => 0, Timeout => 120);
86 my %common_cfg = ( User => 'anonymous',
87 Pass => 'anonymous@here.net',
88 Host => $ftpsite,
89 RemoteDir => '/', # automatic CD on remote machine to RemoteDir
90 Type => 'A' # overwrite I (binary) TYPE default
91 );
92 my $ftp = Net::FTP::Common->new(\%common_cfg, %netftp_cfg);
93  
94 # check files exist and if new
95 my $existnew=0; # true if any files are newer than local file
96 my $errorCs=0; # true if can't file all files
97 my $newversion;
98 foreach my $dir (keys %$directories) {
99 my %ls = $ftp->dir(RemoteDir=>$dir);
100 # check if files exist and if newer
101 foreach my $file (@{$directories->{$dir}}) {
102 if ($ftp->exists(RemoteDir=>$dir, RemoteFile=>$file)) {
103 my %remote_version;
104 $remote_version{month} = $month2num{ lc($ls{$file}->{month}) };
105 $remote_version{day} = $ls{$file}->{day};
106 $remote_version{year} = $ls{$file}->{yearOrTime};
107 $remote_version{year} = $current_year if $remote_version{year} =~ /:/;
108 $newversion .= '_' if $newversion;
109 $newversion .= cat_date(\%remote_version);
110 $existnew=1 if is_new($local_version, \%remote_version);
111 } else {
112 print "### ERROR ($cs_name): File ". $ftpsite.$dir.$file ." does not exist! Can NOT update $cs_name\n";
113 $errorCs=1;
114 }
115 }
116 }
117 next if $errorCs; # can not download all the required files so skip
118
119 # download files if needed
120 if($existnew) {
121 print "### Downloading $cs_name from $ftpsite: $ftp_files\n";
122 my @cat_files;
123 foreach my $dir (keys %$directories) {
124 foreach my $file (@{$directories->{$dir}}) {
125 my $localdir = "/tmp";
126 print "# Downloading file: $file\n";
127 $ftp->get(RemoteDir=>$dir, RemoteFile=>$file, LocalDir=>$localdir);
128
129 # unzip if needed
130 if($file =~ /\.gz$/) {
131 system "gunzip $localdir/$file";
132 $file =~ s/\.gz$//i;
133 } elsif ($file =~ /\.z$/i || $file =~ /\.zip$/i) {
134 system "unzip $localdir/$file";
135 $file =~ s/\.z(.{2})*$//i;
136 }
137 push @cat_files, "$localdir/$file";
138 }
139 }
140
141 # concatenate multiple downloaded files into db file if needed
142 if(@cat_files > 1) {
143 my $catcmd = "cat ". join(' ', @cat_files) ." > $localfile";
144 my $rmcmd = "rm ". join(' ', @cat_files);
145 system "$catcmd";
146 system "$rmcmd";
147 } elsif(@cat_files == 1) {
148 system "mv ". $cat_files[0] ." $localfile";
149 } else {
150 print "### ERROR ($cs_name): Problem with downloaded files\n";
151 }
152
153 # reload the connectorset into connect the dots database
154 system "perl unload.pl --connectorset $cs_name" if $REMOVEOLD;
155 modify_cnf_version($cs_name,$newversion);
156 my $loadcmd = "perl load.pl --database $DATABASE --user $USER ";
157 $loadcmd .= " --password ". $PASSWORD if $PASSWORD;
158 $loadcmd .= " ../ConnectorSet/$cs_name.cnf $localfile";
159 system "$loadcmd";
160
161 } # end exist new
162 } # end if ftp attributes exist
163 } # end fetchrow loop
164  
165  
166  
167 # recieves a comma seperated list of files with their direct paths
168 # returns a hashref keyed on directory, value is filename
169 sub parse_files {
170 my ($ftp_files) = @_;
171 my %files;
172 my @entries = split(/,/, $ftp_files);
173 foreach (@entries) {
174 my ($dir,$file) = /(.*\/)(.+)$/;
175 push @{$files{$dir}}, $file;
176 }
177 return \%files;
178 }
179  
180 # recieves a file and returns hashref of day,month,year it was last modified in the local file system
181 sub get_file_date {
182 my ($filename) = @_;
183 my %out;
184 open(FILE,$filename) or die "Can not open $filename\n";
185 my $stat = stat($filename);
186 my $lastmodified = $stat->mtime;
187 my @times = localtime($lastmodified);
188 $out{day} = $times[3];
189 $out{month} = $times[4];
190 $out{year} = $times[5]+1900;
191 return \%out;
192 }
193  
194 # returns true when remote(year,month,day) is newer than local
195 sub is_new {
196 my ($local, $remote) = @_;
197 return 0 unless $local && $remote;
198 my $ldate = cat_date($local);
199 my $rdate = cat_date($remote);
200 return 1 if $rdate > $ldate;
201 return 0;
202 }
203  
204 # returns concatentated data
205 sub cat_date {
206 my $remote = shift;
207 my $return = $remote->{year};
208 if($remote->{month} < 10) {
209 $return .= '0'. $remote->{month};
210 } else {
211 $return .= $remote->{month};
212 }
213 if($remote->{day} < 10) {
214 $return .= '0'. $remote->{day};
215 } else {
216 $return .= $remote->{day};
217 }
218 return $return;
219 }
220  
221 sub modify_cnf_version {
222 my ($cs_name, $newversion) = @_;
223 my $filename = "../ConnectorSet/$cs_name.cnf";
224 my @lines;
225 open(IN, "$filename") or die "Can not open $filename\n";
226 while(<IN>) {
227 if(/^version/) {
228 push @lines, "version=$newversion\n";
229 } else {
230 push @lines, $_;
231 }
232 }
233 close(IN);
234 open(OUT, ">$filename") or die "Can not open $filename for writing\n";
235 foreach (@lines) {
236 print OUT $_;
237 }
238 close(OUT);
239 }
240  
241  
242  
243  
244