-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathFileUtils.pm
executable file
·241 lines (187 loc) · 5.82 KB
/
FileUtils.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#Sept 18, 2017, Adrian, fixed bug in run_cmd function to check both the success code and the exit code
package FileUtils;
use strict;
use warnings;
use File::Basename;
#use YAML::Syck;
use Sys::Hostname;
use Data::Dumper;
use IO::CaptureOutput qw/capture_exec/;
############################################################################################################################################
#
# FileUtils.pm : This module contains subtroutines to manage files. (mv, rm, etc.)
#
#############################################################################################################################################
####################
# Global Variables #
####################
###############
# Subroutines #
###############
#Accessor
sub get_exec {
my $tool=$_[0];
my $config_file=$_[1];
my $host = hostname;
#print "get_utils_exec_path\t$tool\t$config_file\t$host\n";
if (file_exists($config_file) eq "no") {die("ERROR: file $config_file not Found!\n");}
my $config = LoadFile($config_file);
foreach my $field (sort keys %{ $config }) {
if ($host =~ /$field/) {
return $config->{$field}->{$tool."_EXEC"};
}
}
#else return not available
return "NA";
}
#move the relevant output files to target directory
sub move_files {
my $file=$_[0];
my $target_dir=$_[1];
print "Moving file:$file\n";
my($this_file, $this_dir) = fileparse($file);
my $desired_file = "$target_dir"."$this_file";
my $cmd ="find $this_dir -name \"$this_file\"";
#print $cmd,"\n";
my @files = `$cmd`;
foreach my $file_to_move (@files) {
$file_to_move=~s/\n//g;
print "Moving $file_to_move file under $target_dir\n";
if(-e $file_to_move && -f $file_to_move) {
if(-e $desired_file && -f $desired_file) {
print "$desired_file already present in $target_dir\n";
} else {
my $success = system("mv $file_to_move $target_dir");
check_status($success);
}
} else {
print "File: $file not found\n";
}
}
}
#check the return status from system command...
sub check_status {
my $success = $_[0];
my $task = $_[1];
if($success == 0) {
if($task){print("$task ")};
print "task successfully executed (exit code $success)\n";
} else {
print "ERROR($success): Command failed\n";
#die;
}
}
sub file_exists{
my ($file_name) = $_[0];
if(-f $file_name && -e $file_name){
#print "File $file_name exists!\n";
return "yes";
}else{
print "File $file_name does NOT exist!\n";
return "no";
}
}
#check if directory exists or not. create one if not...
sub check_dir{
my ($dir_name) = @_;
if (-d $dir_name){
print "directory $dir_name already exists!\n";
}else{
my $ret=system("mkdir $dir_name");
if ($ret == 0 ) {print "directory $dir_name successfully created!\n";}
else{ print "error : failed to create $dir_name directory!\n";}
}
}
sub remove_files{
my ($file) = $_[0];
#print "Removing file:$file\n";
my($this_file, $this_dir) = fileparse($file);
my $cmd ="find $this_dir -name \"$this_file\"";
print $cmd,"\n";
my @files = `$cmd`;
foreach my $file_to_remove (@files) {
$file_to_remove=~s/\n//g;
print "Removing $file_to_remove file\n";
my $success = system("rm $file_to_remove");
check_status($success);
}
}
sub file_line_counts{
my $file = $_[0];
my $cmd = "wc -l $file | cut -d\" \" -f1";
my $num_lines = `$cmd`;
chomp($num_lines);
return $num_lines;
}
sub file_gz_line_counts{
my $file = $_[0];
my $cmd = "zcat $file | wc -l ";
my $num_lines = `$cmd`;
chomp($num_lines);
return $num_lines;
}
# find latest log file based on job id number, when searched with wildcard in a file name.
#
sub find_latest_log{
my $log_file = $_[0];
my @files=`ls $log_file 2>/dev/null`;
my $max_job_id = 0;
for (my $i=0; $i < scalar(@files); $i++){
my @cols = split(/\./,$files[$i]);
my $id = $cols[$#cols];
$id =~ s/e|o//g;
if ($max_job_id < $id){ $max_job_id = $id;}
}
#my $latest_log = `ls $log_file | grep $max_job_id 2>/dev/null`;
my $latest_log = `ls $log_file 2>/dev/null| grep $max_job_id`;
chomp($latest_log);
if (-f $latest_log && -e $latest_log){
return $latest_log;
}else{
return "none";
}
}
# find oldest log file based on job id number, when searched with wildcard in a file name.
#
sub find_oldest_log{
my $log_file = $_[0];
my @files=`ls $log_file`;
my $min_job_id =99999999;
for (my $i=0; $i < scalar(@files); $i++){
my @cols = split(/\./,$files[$i]);
my $id = $cols[$#cols];
$id =~ s/e|o//g;
if ($min_job_id > $id){ $min_job_id = $id;}
}
my $oldest_log = `ls $log_file | grep $min_job_id`;
chomp($oldest_log);
return $oldest_log;
}
#run a system command and return a qsub job from a given command and return the job id.
sub run_cmd {
my $cmd = $_[0];
my $task = $_[1]; #this is still under construction
print "\nRunning command: $cmd\n";
my ($stdout, $stderr, $success, $exit_code) = capture_exec($cmd);
#For succeful executions, usually success is 1 and exit code is 0
#print "stdout: $stdout\n";
#print "stderr: $stderr\n";
#print "Success: $success\n";
#print "Exit code: $exit_code\n";
if ($exit_code != 0) {die "ERROR($exit_code): Command FAILED: $cmd \n $stderr \n";}
if ($stdout){
if ($task){
FileUtils::check_status($exit_code, $task);
return $stdout;
} else{
my @return_val_string = split (/\s/, $stdout);
my $job_id = $return_val_string[2];
print "Running job:$job_id\n";
return $job_id;
}
} else {
FileUtils::check_status($exit_code, $task);
return 0;
}
}
1;