| 1 | package CGI::AppBuilder::MapDisp2; | ||
| 2 | |||
| 3 | # Perl standard modules | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | use Getopt::Std; | ||
| 7 | use POSIX qw(strftime); | ||
| 8 | use Carp; | ||
| 9 | use CGI; | ||
| 10 | use CGI::AppBuilder; | ||
| 11 | use CGI::AppBuilder::Message qw(:echo_msg); | ||
| 12 | use CGI::AppBuilder::HTML qw(:all); | ||
| 13 | use File::Path; | ||
| 14 | use File::Copy; | ||
| 15 | use File::Basename; | ||
| 16 | use Archive::Tar; | ||
| 17 | use IO::File; | ||
| 18 | use Net::Rexec 'rexec'; | ||
| 19 | |||
| 20 | our $VERSION = 0.12; | ||
| 21 | require Exporter; | ||
| 22 | our @ISA = qw(Exporter CGI::AppBuilder); | ||
| 23 | our @EXPORT = qw(); | ||
| 24 | our @EXPORT_OK = qw(upload_sas_script | ||
| 25 | get_scrnames | ||
| 26 | backup_file mk_dir | ||
| 27 | ); | ||
| 28 | our %EXPORT_TAGS = ( | ||
| 29 | sas_scr => [qw(upload_sas_script)], | ||
| 30 | all => [@EXPORT_OK] | ||
| 31 | ); | ||
| 32 | |||
| 33 | =head1 NAME | ||
| 34 | |||
| 35 | CGI::AppBuilder::MapDisp2 - Display tasks | ||
| 36 | |||
| 37 | =head1 SYNOPSIS | ||
| 38 | |||
| 39 | use CGI::AppBuilder::MapDisp2; | ||
| 40 | |||
| 41 | my $sec = CGI::AppBuilder::MapDisp2->new(); | ||
| 42 | my ($sta, $msg) = $sec->exe_sql($ar); | ||
| 43 | |||
| 44 | =head1 DESCRIPTION | ||
| 45 | |||
| 46 | This class provides methods for reading and parsing configuration | ||
| 47 | files. | ||
| 48 | |||
| 49 | =cut | ||
| 50 | |||
| 51 | =head2 new (ifn => 'file.cfg', opt => 'hvS:') | ||
| 52 | |||
| 53 | This is a inherited method from CGI::AppBuilder. See the same method | ||
| 54 | in CGI::AppBuilder for more details. | ||
| 55 | |||
| 56 | =cut | ||
| 57 | |||
| 58 | sub new { | ||
| 59 | my ($s, %args) = @_; | ||
| 60 | return $s->SUPER::new(%args); | ||
| 61 | } | ||
| 62 | |||
| 63 | =head2 upload_sas_script($q,$ar) | ||
| 64 | |||
| 65 | Input variables: | ||
| 66 | |||
| 67 | $q - CGI class | ||
| 68 | $ar - array ref containing the following variables: | ||
| 69 | |||
| 70 | Variables used or routines called: | ||
| 71 | |||
| 72 | None | ||
| 73 | |||
| 74 | How to use: | ||
| 75 | |||
| 76 | Return: None | ||
| 77 | |||
| 78 | =cut | ||
| 79 | |||
| 80 | sub upload_sas_script { | ||
| 81 | my ($s, $q, $ar) = @_; | ||
| 82 | |||
| 83 | my @c0 = caller(0); my @c1 = caller(1); | ||
| 84 | my $cls = (exists $c1[3]) ? $c1[3] : ''; | ||
| 85 | my $prg = "$cls [$c0[2]] -> $c0[3]"; | ||
| 86 | |||
| 87 | $s->disp_header($q,$ar,1); | ||
| 88 | |||
| 89 | # get parameters | ||
| 90 | my $vs = 'task,file_name,app_user,dir_sep,sel_sn1'; | ||
| 91 | my ($tsk,$sfn,$apu,$ds,$svr) = $s->get_params($vs, $ar); | ||
| 92 | $ds = ($ds) ? $ds : '/'; | ||
| 93 | if (! $svr) { | ||
| 94 | $s->echo_msg("ERR: ($prg) sever name has not been defined.",0); | ||
| 95 | return; | ||
| 96 | } | ||
| 97 | if (! $sfn) { | ||
| 98 | $s->echo_msg("ERR: ($prg) no upload file is specified.",0); | ||
| 99 | return; | ||
| 100 | } | ||
| 101 | my $adr = eval $s->set_param('all_dir', $ar); | ||
| 102 | |||
| 103 | my $sdr = (exists $adr->{$svr}{sas}) ? $adr->{$svr}{sas} : ''; | ||
| 104 | if (! $sdr) { | ||
| 105 | $s->echo_msg("ERR: ($prg) target folder has not been defined.",0); | ||
| 106 | return; | ||
| 107 | } | ||
| 108 | if (! $apu) { | ||
| 109 | $s->echo_msg("ERR: ($prg) no application user name is specified/available.",0); | ||
| 110 | return; | ||
| 111 | } | ||
| 112 | my $udr = join $ds, $sdr, $apu; | ||
| 113 | $ar->{upload_dir} = $udr; | ||
| 114 | |||
| 115 | $s->mk_dir($sdr); | ||
| 116 | $s->mk_dir($udr); | ||
| 117 | |||
| 118 | $s->upload_file($q, $ar); | ||
| 119 | |||
| 120 | $s->echo_msg("INFO: ($prg) File - $sfn is uploaded to $sdr.", 1); | ||
| 121 | |||
| 122 | return; | ||
| 123 | } | ||
| 124 | |||
| 125 | |||
| 126 | sub get_scrnames { | ||
| 127 | my ($s, $ar) = @_; | ||
| 128 | |||
| 129 | my @c0 = caller(0); my @c1 = caller(1); | ||
| 130 | my $cls = (exists $c1[3]) ? $c1[3] : ''; | ||
| 131 | my $prg = "$cls [$c0[2]] -> $c0[3]"; | ||
| 132 | |||
| 133 | my $ds = (exists $ar->{dir_sep}) ? $ar->{dir_sep} : ''; | ||
| 134 | $ds = ($^O =~ /MSWin/i)? '\\': '/' if ! $ds; | ||
| 135 | |||
| 136 | # get parent id, server id, study id and list id | ||
| 137 | my $vs = 'pid,sid,study_id,list_id,app_user'; | ||
| 138 | my ($pid,$sn,$sid,$lid,$apu) = $s->get_params($vs,$ar); | ||
| 139 | $sn = $ar->{sel_sn1} if !$sn && exists $ar->{sel_sn1}; | ||
| 140 | $sid = 0 if !$sid; | ||
| 141 | $lid = 0 if !$lid; | ||
| 142 | |||
| 143 | my $ad = eval $s->set_param('all_dir', $ar); # all dir array | ||
| 144 | my $dir = $ad->{$sn}{sas}; # sas dir | ||
| 145 | $dir = join $ds, $dir, $apu; | ||
| 146 | |||
| 147 | my $r = []; # result array | ||
| 148 | if (!$sn) { | ||
| 149 | $s->echo_msg("ERR: ($prg) server id is not provided.", 0); | ||
| 150 | return wantarray ? @$r : $r; | ||
| 151 | } else { | ||
| 152 | $s->echo_msg("INFO: ($prg) server id is $sn.", 3); | ||
| 153 | } | ||
| 154 | if (! -d $dir) { | ||
| 155 | $s->echo_msg("WARN: ($prg) could not find dir - $dir.", 1); | ||
| 156 | return wantarray ? @$r : $r; | ||
| 157 | } else { | ||
| 158 | $s->echo_msg("INFO: ($prg) spec dir is $dir.", 3); | ||
| 159 | } | ||
| 160 | opendir DD, "$dir" or die "ERR: could not opendir - $dir: $!\n"; | ||
| 161 | my @a = sort (grep { !/^\./ && !/\.bak$/ && -f "$dir/$_" } readdir DD); | ||
| 162 | closedir DD; | ||
| 163 | for my $i (0..$#a) { push @$r, [$a[$i],$a[$i],0]; } | ||
| 164 | unshift @$r, ['', '__Select__',1]; | ||
| 165 | |||
| 166 | wantarray ? @$r : $r; | ||
| 167 | } | ||
| 168 | |||
| 169 | |||
| 170 | sub mk_dir { | ||
| 171 | my ($s, $dir) = @_; | ||
| 172 | # $dir - directory | ||
| 173 | |||
| 174 | # $package, $filename, $line, $subroutine, $hasargs, | ||
| 175 | # $wantarray, $evaltext, $is_require, $hints, $bitmask | ||
| 176 | my @c0 = caller(0); my @c1 = caller(1); | ||
| 177 | my $cls = (exists $c1[3]) ? $c1[3] : ''; | ||
| 178 | my $prg = "$cls [$c0[2]] -> $c0[3]"; | ||
| 179 | |||
| 180 | if (! -d $dir) { | ||
| 181 | eval { mkpath($dir,0,0777) }; | ||
| 182 | if ($@) { | ||
| 183 | my $m = "ERR: ($prg [" . __LINE__ . "]) "; | ||
| 184 | $m .= "could not mkdir - $dir: $!: $@<br>\n"; | ||
| 185 | $s->echo_msg($m,0); | ||
| 186 | return; | ||
| 187 | } | ||
| 188 | if ($^O !~ /^MSWin/i) { # non window | ||
| 189 | system("chmod -R ugo+w $dir"); | ||
| 190 | } | ||
| 191 | } | ||
| 192 | } | ||
| 193 | |||
| 194 | sub backup_file { | ||
| 195 | my ($s, $ffn, $ar) = @_; | ||
| 196 | # $ffn - file name | ||
| 197 | # $ar - parameter array | ||
| 198 | # $bdr - backup dir | ||
| 199 | |||
| 200 | my @c0 = caller(0); my @c1 = caller(1); | ||
| 201 | my $cls = (exists $c1[3]) ? $c1[3] : ''; | ||
| 202 | my $prg = "$cls [$c0[2]] -> $c0[3]"; | ||
| 203 | |||
| 204 | my $ds = ($^O =~ /MSWin/i)? '\\': '/'; | ||
| 205 | my ($bcp) = $s->get_params('bak_copies',$ar); | ||
| 206 | $bcp = ($bcp) ? $bcp : 10; # default it to 10 copies | ||
| 207 | my ($fname, $path, $sfx) = fileparse($ffn,qr{\..*}); | ||
| 208 | |||
| 209 | my $bdr = "${path}baks"; | ||
| 210 | my $f1 = $ffn; | ||
| 211 | my $f2 = join $ds, $bdr, "$fname$sfx"; | ||
| 212 | |||
| 213 | $s->mk_dir($bdr) if (! -d $bdr); | ||
| 214 | |||
| 215 | if (! -f $f2) { | ||
| 216 | copy($f1,$f2) or | ||
| 217 | $s->echo_msg("ERR: ($prg [" .__LINE__ . | ||
| 218 | "]) Copy failed from $f1 to $f2: $!",0); | ||
| 219 | return; | ||
| 220 | } | ||
| 221 | |||
| 222 | opendir DD, "$bdr" or croak "ERR: ($prg) could not opendir - $bdr: $!\n"; | ||
| 223 | my @a = sort { (stat("$bdr/$a"))[9] <=> (stat("$bdr/$b"))[9] } | ||
| 224 | (grep { /$fname/ && !/^\./ } readdir DD); | ||
| 225 | closedir DD; | ||
| 226 | |||
| 227 | my $n = 0; | ||
| 228 | my ($m) = ($a[$#a] =~ /\.(\d+)$/); | ||
| 229 | $m = 1 + $m; | ||
| 230 | $n = ($m) % $bcp; | ||
| 231 | $n = sprintf "%03d", $n; | ||
| 232 | $f2 = "$f2.$n"; | ||
| 233 | copy($f1,$f2) or | ||
| 234 | $s->echo_msg("ERR: ($prg [" .__LINE__ . | ||
| 235 | "]) Copy failed from $f1 to $f2: $!",0); | ||
| 236 | return; | ||
| 237 | } | ||
| 238 | |||
| 239 | 1; | ||
| 240 | |||
| 241 | =head1 HISTORY | ||
| 242 | |||
| 243 | =over 4 | ||
| 244 | |||
| 245 | =item * Version 0.10 | ||
| 246 | |||
| 247 | This version extracted from MapDips on 07/22/2013. | ||
| 248 | |||
| 249 | =item * Version 0.20 | ||
| 250 | |||
| 251 | 07/22/2013 (htu): start this PM | ||
| 252 | |||
| 253 | =cut | ||
| 254 | |||
| 255 | =head1 SEE ALSO (some of docs that I check often) | ||
| 256 | |||
| 257 | Oracle::Loader, Oracle::Trigger, CGI::AppBuilder, File::Xcopy, | ||
| 258 | CGI::AppBuilder::Message | ||
| 259 | |||
| 260 | =head1 AUTHOR | ||
| 261 | |||
| 262 | Copyright (c) 2013 Hanming Tu. All rights reserved. | ||
| 263 | |||
| 264 | This package is free software and is provided "as is" without express | ||
| 265 | or implied warranty. It may be used, redistributed and/or modified | ||
| 266 | under the terms of the Perl Artistic License (see | ||
| 267 | http://www.perl.com/perl/misc/Artistic.html) | ||
| 268 | |||
| 269 | =cut | ||
| 270 |