-
Notifications
You must be signed in to change notification settings - Fork 3
/
common.bash
executable file
·981 lines (861 loc) · 22.5 KB
/
common.bash
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
# NAME
#
# common.bash
#
# DESCRIPTION
#
# 'common.bash' is a set of common script functions that can be
# used by other stand-alone scripts. Typically, these are made
# available by:
#
# source common.bash
#
# in the host script.
#
# DEPENDENCIES
#
# This file should typically be sourced in the "main" section of
# the host script.
#
# Note that there are several dependencies imposed by sourcing
# these common functions on the host script.
#
# Gi_verbose: Verbosity level tracker
# Gb_forceStage: Execution stage override flag
# STAMPLLOG: Specified in main program, defines
# stamp log file
# G_LOGDIR Directory containing output logs and
# expert opts files.
#
# These should be handled by the getopts loop of the host script.
#
###\\\
# Globals are in capital letters. Immutable globals are prefixed by 'G'.
###///
G_SELF=`basename $0`
G_PID=$$
G_COMMON_MIGRATEANALYSISDIR="-x"
G_COMMON_MIGRATEORIGDIR="-x"
# Column formatting
declare -i G_LC=40
declare -i G_RC=40
# Output behaviour
declare -i Gi_verbose=0
# Stage handling
declare -i Gb_forceStage=0
#
# Error handling is performed by using a "poor man's" dictionary type hack of
# the following form:
#
# A_$stem="Action Performed"
# EM_$stem="Error condition/message"
# EC_$stem="Code"
#
# where $stem denotes the name of the particular error. Errors are called
# using "fatal $stem"
#
A_eg="Action performed"
EM_eg="Error condition flagged"
EC_eg="-100"
A_metaLog="checking the meta log file"
EM_metaLog="it seems as though this stage has already run.\n\tYou can force execution with a '-f'"
EC_metaLog=80
###\\\
# Function definitions
###///
function expertOpts_file
{
# ARGS
# $1 process name
#
# DESC
# For the passed <process name>, return the associated
# expertOpts file.
#
local processName=$1
local optsFile=""
optsFile=${G_LOGDIR}/${processName}.opt
echo "$optsFile"
}
function expertOpts_parse
{
# ARGS
# $1 process name
#
# DESC
# Checks for <processName> in $G_LOGDIR.
# If exists, read contents and return, else
# return empty string.
#
local processName=$1
local optsFile=""
OPTS=""
optsFile=$(expertOpts_file $processName)
if (( $Gb_useExpertOptions )) ; then
if [[ -f $optsFile ]] ; then
OPTS=$(cat $optsFile)
fi
fi
OPTS=$(printf " %s " $OPTS)
echo "$OPTS"
}
function expertOpts_rm
{
# ARGS
# $1 process name
#
# DESC
# Removes the expertOpts file
#
local processName=$1
local optsFile=""
optsFile=$(expertOpts_file $processName)
# optsFile=${G_LOGDIR}/${processName}.opt
if (( $Gb_useExpertOptions )) ; then
if [[ -f $optsFile ]] ; then
rm -f $optsFile
fi
fi
}
function expertOpts_write
{
# ARGS
# $1 process name
# $2 option string
#
# DESC
# Appends $2 to <processName>.opt in $G_LOGDIR.
#
local processName=$1
local optsFile=""
OPTS=$2
optsFile=$(expertOpts_file $processName)
if (( $Gb_useExpertOptions )) ; then
echo "$OPTS" >> $optsFile
fi
OPTS=$(printf " %s " $OPTS)
echo "$OPTS"
}
function trim
{
echo $1;
}
function string_clean
{
# ARGS
# $1 target string
#
# DESC
# Removes useless junk characters from <target string>.
#
local targetString=$1
echo "$targetString" | sed 's/[]|&+=<{([)}> ]*//g'
}
function verbosity_check
{
#
# DESC
# If verbosity level is non-zero, set output to stdout, else
# set to /dev/null
#
exec 6>&1
if (( Gi_verbose ))
then
exec >&1
else
exec > "/dev/null"
fi
}
function shut_down
# $1: Exit code
{
# If the migrate analysis was turned on, then at the end we want
# to move the results from the analysis directory to their final
# resting place
if [[ "$G_COMMON_MIGRATEANALYSISDIR" != "-x" ]] ; then
# First remove the symbolic link between original directory
# and analysis directory
cd $(dirname $G_COMMON_MIGRATEORIGDIR)
rm $(basename $G_COMMON_MIGRATEORIGDIR)
# Move the results from the temporary analysis directory
# into their final resting place
mv $G_COMMON_MIGRATEANALYSISDIR .
fi
echo -e "\n$G_SELF:\n\tShutting down with code $1 at $(date).\n"
exit $1
}
function synopsis_show
{
echo "USAGE:"
echo "$G_SYNOPSIS"
}
function error
# $1: Action
# $2: Error string
# $3: Exit code
{
echo -e "\n$G_SELF:\n\tSorry, but there seems to be an error." >&2
echo -e "\tWhile $1," >&2
echo -e "\t$2\n" >&2
shut_down $3
}
function fatal
# $1: variable name - used to construct action/error string/exit code
{
local stem=$1
eval action=\$A_$stem
eval errorString=\$EM_$stem
eval exitCode=\$EC_$stem
error "$action" "$errorString" "$exitCode"
}
function warn
# $1: Action
# $2: Warn string
# $3: Default value
{
echo -e "\n$G_SELF: WARNING\n" >&2
echo -e "\tWhile $1," >&2
echo -e "\t$2\n" >&2
echo -e "\tWarning code: '$3'\n" >&2
}
function beware
# $1: variable name - used to construct action/error string/exit code
{
local stem=$1
eval action=\$A_$stem
eval errorString=\$EM_$stem
eval exitCode=\$EC_$stem
warn "$action" "$errorString" "$exitCode"
}
function NOP
{
#
# DESC
# Do nothing!
return 0
}
function NOP_ret
{
#
# ARGS
# $1 in/out a variable that is directly
# returned
#
# DESC
# Does nothing other than "reflect" a passed variable back to
# the caller.
#
return $1
}
function ret_check
{
# ARGS
# $1 in return value to check
# $2 in (opt) failure text
# $3 in (opt) success text
# DESC
# Checks for the passed return value, and echoes a
# conditional to stdout. Returns this value back to
# the main program.
#
local ret=$1
FAIL="failure"
PASS="ok"
if (( ${#2} )) ; then
FAIL=$2
fi
if (( ${#3} )) ; then
PASS=$3
fi
if [[ $ret != "0" ]] ; then
if (( Gi_verbose )) ; then
printf "\e[1;31m%*s\e[0m\n" $G_RC "[ $FAIL ]"
fi
else
if (( Gi_verbose )) ; then
printf "\e[1;36m%*s\e[0m\n" $G_RC "[ $PASS ]"
fi
fi
return $ret
}
function fileExist_check
{
#
# ARGS
# $1 in file to check
# $2 in (opt) failure text
# $3 in (opt) success text
#
# DESC
# Checks for the existence of a file, and echoes a
# conditional to stdout. Returns this value back to
# the main program:
# 0: no error (file does exist)
# 1: some error (file does not exist)
#
local file=$1
FAIL="failure"
PASS="ok"
if (( ${#2} )) ; then
FAIL=$2
fi
if (( ${#3} )) ; then
PASS=$3
fi
if [[ ! -f $file ]] ; then
if (( Gi_verbose )) ; then
printf "%*s\n" $G_RC "[ $FAIL ]"
fi
return 1
else
if (( Gi_verbose )) ; then
printf "%*s\n" $G_RC "[ $PASS ]"
fi
return 0
fi
}
function file_checkOnPath
{
# ARGS
# $1 in file to check
#
# DESC
# Checks for the existence of a file. If not found,
# return 1, else return 0.
#
local file=$1
type -all $file 2>/dev/null >/dev/null
notFound=$?
if (( notFound )) ; then
printf "%*s\n" $G_RC "[ failure ]"
return 1
else
printf "%*s\n" $G_RC "[ ok ]"
fi
return 0
}
function dirExist_check
{
#
# ARGS
# $1 in dir to check
# $2 in (opt) failure text
# $3 in (opt) success text
#
# DESC
# Checks for the existence of a dir, and echoes a
# conditional to stdout. Returns this value back to
# the main program:
# 0: no error (file does exist)
# 1: some error (file does not exist)
#
local dir=$1
FAIL="failure"
PASS="ok"
if (( ${#2} )) ; then
FAIL=$2
fi
if (( ${#3} )) ; then
PASS=$3
fi
if [[ ! -d $dir ]] ; then
printf "%*s\n" $G_RC "[ $FAIL ]"
return 1
else
printf "%*s\n" $G_RC "[ $PASS ]"
return 0
fi
}
function cprint
{
#
# ARGS
# $1 in left column text
# $2 in right column text
#
# DESC
# Prints two input text strings in two columns: left and
# right respectively.
#
local left=$1
local right=$2
if (( ! Gi_verbose )) ; then return 1 ; fi
printf "%*s" $G_LC "$left"
printf "%*s\n" $G_RC "$right"
}
function lprint
{
#
# ARGS
# $1 in left column text
# $2 in optional color spec
#
# DESC
# Prints left column text string.
#
local left=$1
declare -i b_color
b_color=0
LEFTJUSTIFY=""
if (( ${#2} )) ; then
b_color=1
color=$2
fi
if (( ${#3} )) ; then
LEFTJUSTIFY="-"
fi
if (( ! Gi_verbose )) ; then return 1 ; fi
if (( b_color )) ; then
printf "\e[${color}m%*s\e[0m" "${LEFTJUSTIFY}$G_LC" "$left"
else
printf "%*s" "${LEFTJUSTIFY}$G_LC" "$left"
fi
}
function lprintn
{
#
# ARGS
# $1 in left column text
# $2 in optional color spec
#
# DESC
# Prints left column text string, followed by \n
#
local left=$1
declare -i b_color
b_color=0
LEFTJUSTIFY=""
if (( ${#2} )) ; then
b_color=1
color=$2
fi
if (( ${#3} )) ; then
LEFTJUSTIFY="-"
fi
if (( b_color )) ; then
printf "\e[${color}m%*s\n\e[0m" "${LEFTJUSTIFY}$G_LC" "$left"
else
printf "%*s\n" "${LEFTJUSTIFY}$G_LC" "$left"
fi
}
function rprint
{
#
# ARGS
# $1 in right column text
#
# DESC
# Prints right column text string, followed by \n
#
local right=$1
declare -i b_color
b_color=0
if (( ${#2} )) ; then
b_color=1
color=$2
fi
if (( ! Gi_verbose )) ; then return 1 ; fi
if (( b_color )) ; then
printf "\e[${color}m%*s\n\e[0m" $G_RC "$right"
else
printf "%*s\n" $G_RC "$right"
fi
}
function statusPrint
{
# ARGS
# $1 in message to print
# $2 in possible trailing character or string
#
# DESC
# Prints a status message on the left of the console.
#
local status=$1
local ctrlN=$2
if (( ! Gi_verbose )) ; then return 1 ; fi
printf "%*s$ctrlN" $G_LC "$status"
}
function stage_alreadyRun
{
local stage="$1"
local logFile="$2"
echo $(cat "$logFile" | awk -F \| '{print $3'} | grep "$stage" | wc -l)
}
function stage_check
{
local stage="$1"
local logFile="$2"
if [[ -f "$logFile" ]] ; then
if (( !Gb_forceStage && $(stage_alreadyRun "$stage" "$logFile") )) ; then
fatal metaLog
fi
fi
}
function sysprint
{
local message="$1"
echo -e "$(date) $(hostname) $message"
}
function stage_stamp
{
local stage=$1
local logFile=$2
local clusterUser="$3"
if (( !${#logFile} )) ; then
logFile="stdout"
fi
echo -e "$(date) $(hostname) $clusterUser | $G_SELF | Stage $stage | ok" >> $logFile
if [[ $logFile == "stdout" ]] ; then
cat $logFile
rm $logFile
fi
}
function stage_run
{
#
# ARGS
# $1 name of stage
# $2 command line to execute
# $3 file to log stdout
# $4 file to log stderr
# $5 turn OFF tee
#
# DESCRIPTION
# Run the stage command. If $3 and $4 exist, capture output
# of stdout and stderr respectively.
#
# If a 5th argument is passed, turn OFF the tee.
#
# If G_mailLogTo exists, mail stdout and stderr.
#
local stageName=$1
local stageCMD=$2
local stdout=$3
local stderr=$4
local noTEE=$5
local b_TEE=1
STDOUTFILE=/dev/null
STDERRFILE=/dev/null
if (( ${#3} )) ; then STDOUTFILE=$3 ; fi
if (( ${#4} )) ; then STDERRFILE=$4 ; fi
if (( ${#5} )) ; then b_TEE=0 ; fi
stage_check "$stageName" $STAMPLOG
statusPrint "Running $stageName..."
stage_stamp "RUN $(echo $stageCMD | tr '\n' ' ')" $STAMPLOG
if (( b_TEE )) ; then
((( eval $stageCMD | tee $STDOUTFILE) 3>&1 1>&2 2>&3 |\
tee $STDERRFILE) 3>&1 1>&2 2>&3)
else
eval $stageCMD >$STDOUTFILE 2>$STDERRFILE
fi
ret=$?
ret_check $ret
if (( !$ret )) ; then stage_stamp "$stageName" $STAMPLOG ; fi
return $ret
}
function stage_run_bg
{
#
# ARGS
# $1 name of stage
# $2 command line to execute
# $3 file to log stdout
# $4 file to log stderr
# $5 turn OFF tee
#
# DESCRIPTION
# Run the stage command in the background. If $3 and $4 exist,
# capture output of stdout and stderr respectively.
#
# If a 5th argument is passed, turn OFF the tee.
#
# If G_mailLogTo exists, mail stdout and stderr.
#
local stageName=$1
local stageCMD=$2
local stdout=$3
local stderr=$4
local noTEE=$5
local b_TEE=1
STDOUTFILE=/dev/null
STDERRFILE=/dev/null
if (( ${#3} )) ; then STDOUTFILE=$3 ; fi
if (( ${#4} )) ; then STDERRFILE=$4 ; fi
if (( ${#5} )) ; then b_TEE=0 ; fi
stage_check "$stageName" $STAMPLOG
statusPrint "Running $stageName..."
stage_stamp "RUN $(echo $stageCMD | tr '\n' ' ')" $STAMPLOG
if (( b_TEE )) ; then
((( eval $stageCMD | tee $STDOUTFILE) 3>&1 1>&2 2>&3 |\
tee $STDERRFILE) 3>&1 1>&2 2>&3) &
else
eval $stageCMD >$STDOUTFILE 2>$STDERRFILE &
fi
ret=$?
ret_check $ret
if (( !$ret )) ; then stage_stamp "$stageName" $STAMPLOG ; fi
return $ret
}
function process_kill
{
#
# ARGS
# $1 in process name
#
HOST=$(uname -a | awk '{print $1}')
if [[ "$HOST" == "Darwin" ]] ; then
ps -aux | grep $1 | grep -v grep | awk '{print "kill -9 " $2}' | sh 2>/dev/null >/dev/null
else
ps -Af | grep $1 | grep -v grep | awk '{print "kill -9 " $2}' | sh 2>/dev/null >/dev/null
fi
return 0
}
function mail_reports
{
# ARGS
# $1 - Mailto address(es)
# $2 - Whether to mail stdout ("1" = true, "0" = false)
# $3 - Whether to mail stderr ("1" = true, "0" = false)
#
# DESC
# Email reports to specified user(s).
#
local mailTo=$1
local mailStdOut=$2
local mailStdErr=$3
if (( "$mailStdOut" == "1" )) ; then
if [[ -f ${G_LOGDIR}/${G_SELF}.std ]] ; then
cp ${G_LOGDIR}/${G_SELF}.std ${G_LOGDIR}/${G_SELF}.std.mail
mail -s "stdout: ${G_SELF}" $mailTo < ${G_LOGDIR}/${G_SELF}.std.mail
fi
fi
if (( "$mailStdErr" == "1" )) ; then
if [[ -f ${G_LOGDIR}/${G_SELF}.err ]] ; then
cp ${G_LOGDIR}/${G_SELF}.err ${G_LOGDIR}/${G_SELF}.err.mail
mail -s "stderr: ${G_SELF}" $mailTo < ${G_LOGDIR}/${G_SELF}.err.mail
fi
fi
}
function cluster_schedule
{
# ARGS
# $1 original script command line args
# $2 name of pipeline (e.g., tract, fs, dcmanon, etc.)
#
# DEPENDENCIES
# G_CLUSTERDIR cluster directory where schedule log is found
# G_SCHEDULELOG schedule log file name (e.g., schedule.log)
# G_CLUSTERUSER name of user submitting to cluster
# G_LOGDIR log directory path
#
# DESC
# Creates a custom script in the G_LOGDIR that is essentially
# the original command line. Once scheduled, termination of this
# script ceases, and it is "re-spawned" on the cluster.
#
local cmdArgs=$1
local pipelineName=$2
# Setup the command line args (stripping the -c)
COMARGS=$(echo $cmdArgs | sed 's|-c||')
# Create mini-script to run on cluster and add to schedule.log
STAGE="0-cluster_schedule"
STAGECMD="$G_SELF $COMARGS -f >\
${G_LOGDIR}/${G_SELF}.std 2>\
${G_LOGDIR}/${G_SELF}.err"
STAGECMD=$(echo $STAGECMD | sed 's|/local_mount||g')
CLUSTERSH=${G_LOGDIR}/$pipelineName-cluster.sh
echo "#!/bin/bash" > $CLUSTERSH
echo "export PATH=$PATH" >> $CLUSTERSH
echo "source $FREESURFER_HOME/SetUpFreeSurfer.sh" >> $CLUSTERSH
echo "source $FSL_DIR/etc/fslconf/fsl.sh" >> $CLUSTERSH
echo "export SUBJECTS_DIR=$SUBJECTS_DIR" >> $CLUSTERSH
echo "export DSI_PATH=$(echo $PATH | tr ":" "\n" | grep dtk | head -n 1)/matrices" >> $CLUSTERSH
echo "export PYTHONPATH=$PYTHONPATH" >> $CLUSTERSH
echo "$STAGECMD" >> $CLUSTERSH
chmod 755 $CLUSTERSH
STAGECMD="${G_LOGDIR}/$pipelineName-cluster.sh"
STAGECMD=$(echo $STAGECMD | sed 's|/local_mount||g')
stage_stamp "$STAGECMD" ${G_CLUSTERDIR}/$G_SCHEDULELOG "$G_CLUSTERUSER"
stage_stamp "$STAGE Schedule for cluster" $STAMPLOG
stage_stamp "$STAGE" $STAMPLOG
# Also append to output of XML file used by web front end
LINENUMBER=$(wc -l "${G_CLUSTERDIR}/$G_SCHEDULELOG" | awk '{print $1}')
GENHEADER=""
if [[ ! -f "${G_CLUSTERDIR}/$G_SCHEDULELOG.xml" ]] ; then
GENHEADER="-h"
fi
GENXMLCMD="cluster_genXML.bash -f ${G_CLUSTERDIR}/$G_SCHEDULELOG -l ${LINENUMBER} ${GENHEADER}"
eval $GENXMLCMD >> "${G_CLUSTERDIR}/$G_SCHEDULELOG.xml"
}
function wait_for_lockfile
{
# ARGS
# $1 lock file name
# $2 timeout in seconds
#
# DEPENDENCIES
#
# DESC
# Wait for a lockfile to be available for use. This
# essentially provides process/thread level protection on
# operations that should not occur synchronously
#
local lockFile=$1
TIMEOUT_SEC=3600
if (( ${#2} )) ; then TIMEOUT_SEC=$2 ; fi
# Make sure the file gets deleted if the process
# exits before deleting the file.
trap "rm -f $lockFile" 0 1 2 9 15
lockfile -1 -r ${TIMEOUT_SEC} $lockFile
}
function release_lockfile
{
# ARGS
# $1 lock file name
#
# DEPENDENCIES
#
#
# DESC
# Deletes a lockfile if it exists.
#
local lockFileName=$1
rm -f $lockFileName
}
function migrateAnalysis_enable
{
# ARGS
# $1 Directory to use for migrated analysis
# $2 Final directory to store results in
# DEPENDENCIES
#
#
# DESC
# This function is used for having an alternate directory to use
# for processing the data. It is useful for example where the data
# needs to be moved to a cluster storage space for processing and
# then moved back afterwards.
#
G_COMMON_MIGRATEANALYSISDIR=$1
G_COMMON_MIGRATEORIGDIR=$2
# Create a symbolic link between the original directory and the
# analysis directory that will be used
origDir=$(pwd)
cd $(dirname $G_COMMON_MIGRATEORIGDIR)
ln -s $G_COMMON_MIGRATEANALYSISDIR $(basename $G_COMMON_MIGRATEORIGDIR)
cd $origDir
}
function pipelineStatus_create
{
# ARGS
# $1 Pipeline name
# $2 Remaining arguments should be a string
# with the names of the stages to add.
# Each stage will be numberered starting at 1.
#
# DESC
#
# Create pipeline status with specified stages
local pipelineName=$1
local stages=$2
# Create the pipeline
pipeline_status_cmd.py --createPipeline $pipelineName ${G_LOGDIR}/${G_SELF}.status
# Add the stages
for STAGE in $stages ; do
pipeline_status_cmd.py --addStage $STAGE ${G_LOGDIR}/${G_SELF}.status
done
}
function pipelineStatus_addInput
{
# ARGS
# $1 Stage name
# $2 Root directory
# $3 File path
# $4 Name [optional, default: File path]
# $5 Type [optional, default: None]
#
# DESC
#
# Add input to given stage
NAME=""
TYPETAG=""
if (( ${#4} )) ; then
NAME="--name $4"
fi
if (( ${#5} )) ; then
TYPETAG="--typeTag $5"
fi
(pipeline_status_cmd.py --addInput $1 --rootDir $2 \
--filePath $3 ${NAME} ${TYPETAG} ${G_LOGDIR}/${G_SELF}.status)
return $?
}
function pipelineStatus_addOutput
{
# ARGS
# $1 Stage name
# $2 Root directory
# $3 File path
# $4 Name [optional, default: File path]
# $5 Type [optional, default: None]
#
# DESC
#
# Add output to given stage
NAME=""
TYPETAG=""
if (( ${#4} )) ; then
NAME="--name $4"
fi
if (( ${#5} )) ; then
TYPETAG="--typeTag $5"
fi
(pipeline_status_cmd.py --addOutput $1 --rootDir $2 \
--filePath $3 ${NAME} ${TYPETAG} ${G_LOGDIR}/${G_SELF}.status)
return $?
}
function pipelineStatus_canRun
{
# ARGS
# $1 Stage name
#
# DESC
# Check whether a pipeline can run (e.g., whether its
# dependencies have been satisfied)
FAIL="failure"
PASS="ok"
pipeline_status_cmd.py --queryCanRun $1 ${G_LOGDIR}/${G_SELF}.status
ret=$?
if (( Gi_verbose )) ; then
printf "Checking on stage $1 inputs"
if (( $ret )) ; then
printf "%*s\n" $G_RC "[ $FAIL ]"
else
printf "%*s\n" $G_RC "[ $PASS ]"
fi
fi
return $ret
}
#
# Typical getops loop:
#
# while getopts v:f option ; do
# case "$option"
# in
# v) Gi_verbose=$OPTARG ;;
# f) Gb_forceStage=1 ;;
# \?) synopsis_show
# exit 0;;
# esac
# done
#
# Remember to stamp output log with current command line history:
#
# verbosity_check
# topDir=$(pwd)
# STAMPLOG=${topDir}/${G_SELF}.log
# stage_stamp "Init | ($topDir) $G_SELF $*" $STAMPLOG
#
# Options pruning:
#
# shift $(($OPTIND - 1))
# ARGS=$*