#------------------------------------------------------------------------------=head2 METHOD assert_no_already_attached In general, it's a really bad practice to allow files to be attached to multiple workflows at the same time. This method will inspect all of the filespassed in the @files argument and determine which, if any, of the files are already associated to another job. If any are, this method returns an errorstring declaring which files are offending. If no files are offending, this method returns the empty string. E.g. C<my $err_str = assert_no_arleady_attached(1234, $wa, @attached_files);>B<ARGUMENTS>=over=item $ok_jobid - a jobid that you don't want to consider as being invalidPassing a value <= 0 for the $ok_jobid implies the assert should make sure thefiles are not attached to *any* other workflow. Passing a value of the current task's $jobId implies the assert should make sure the files are notattached to any other workflow but its own.=item $area_vpath - vpath to the workarea containing the files to testFor example, "/default/main/blah";=item @files - an array of files to test. Files are relative to the $area_vpathparameter=back=cut#------------------------------------------------------------------------------sub assert_no_already_attached { my($ok_jobid, $area_vpath, @files) = @_; my $errors = ""; foreach my $file (@files) { my %hash = get_file_jobs($area_vpath . "/" . $file); if(%hash) { my $jobids = ""; foreach my $jobid (keys %hash) { unless($jobid eq $ok_jobid) { $jobids .= $jobid . " "; } } if($jobids) { $errors .= "File '$file' is already attached to job(s) $jobids\n"; } } } return $errors;}
#------------------------------------------------------------------------------=head2 METHOD get_file_jobs Wrapper around the iwgetfilejobs CLT, this method will return a hashtable of(jobid, taskid) pairs for each and every workflow the specified file isassociated with. If the file specified is not attached to any workflows, an empty hash tableis returned.B<Arguments>=over=item $file - vpath to a file to run the iwgetfilejobs on. For example, "/default/main/Blah/WORKAREA/Workarea01/whatever.txt"=back=cut#------------------------------------------------------------------------------sub get_file_jobs { my($file) = @_; my %hash = (); my $cmd = qq!$iwhome/bin/iwgetfilejobs "$file"!; open(CMD, "$cmd|") or warn "Unable to execute '$cmd' $!"; while(<CMD>) { if($_ =~ /job=(\d*).*task=(\d*)/) { $hash{$1} = $2; } } close(CMD); return %hash;}
Hmm, I heard one could put comments in Perl, but it never dawned on me to do that. My code is self-documenting.
My perl style isn't perfect (I tend not to use 'shift', I prefer explicitly opening commands instead of using qx, and I kinda code like it's C++ or Java), but this works for me
#================================ check for file already in some other workflow ================================================# make sure the file list is definedmy @files = (); if (__ELEM__('iw_file')) # form, then html-encoded { # by hand via CGI_lite @files = map { TeamSite::CGI_lite::escape_html_data($_); } sort __VALUE__('iw_file'); } my $FlagAlreadyExists=assert_no_already_attached(0,$vpath,@files);debug("assert_no_already_attached: ".$FlagAlreadyExists);sub assert_no_already_attached { my($ok_jobid, $area_vpath, @files) = @_; my $errors = ""; foreach my $file (@files) { my %hash = get_file_jobs($area_vpath . "/" . $file); if(%hash) { my $jobids = ""; foreach my $jobid (keys %hash) { unless($jobid eq $ok_jobid) { $jobids .= $jobid . " "; } } if($jobids) { $errors .= "File '$file' is already attached to job(s) $jobids\n"; } } } return $errors;}sub get_file_jobs { my($file) = @_; my %hash = (); my $cmd = qq!$iwhome/bin/iwgetfilejobs "$file"!; open(CMD, "$cmd|") or warn "Unable to execute '$cmd' $!"; while() { if($_ =~ /job=(\d*).*task=(\d*)/) { $hash{$1} = $2; } } close(CMD); return %hash;}#============================= End ===================================================
if ($FlagAlreadyExists ne ""){ TAG_info( iw_FlagAlreadyExists_mode => [ html => "$FlagAlreadyExists", label => "DO NOT PROCEED" , is_required => "true", ], );}else{ # your normal code to show fields for workflow here}