]> git.donarmstrong.com Git - biopieces.git/blob - code_perl/Maasha/Biopieces.pm
d9af0f1a53875f5b5b6b710fc5b54eba106de43c
[biopieces.git] / code_perl / Maasha / Biopieces.pm
1 package Maasha::Biopieces;
2
3
4 # Copyright (C) 2007-2009 Martin A. Hansen.
5
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version.
10
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
20 # http://www.gnu.org/copyleft/gpl.html
21
22
23 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DESCRIPTION <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
24
25
26 # Routines for manipulation, parsing and emitting of human/machine readable biopieces records.
27
28
29 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
30
31
32 use Getopt::Long qw( :config bundling );
33 use Data::Dumper;
34 use Maasha::Match;
35 use Maasha::Common;
36 use Maasha::Filesys;
37 use vars qw( @ISA @EXPORT_OK );
38
39 require Exporter;
40
41 @ISA = qw( Exporter );
42
43 @EXPORT_OK = qw(
44     read_stream
45     write_stream
46     get_record
47     put_record
48 );
49
50
51 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SIGNAL HANDLER <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
52
53
54 $SIG{ '__DIE__' } = \&sig_handler;
55 $SIG{ 'INT' }     = \&sig_handler;
56 $SIG{ 'TERM' }    = \&sig_handler;
57
58
59 sub status_set
60 {
61     my ( $time_stamp, $script, $user, $pid, $file, $fh );
62
63     $time_stamp = Maasha::Common::time_stamp();
64     $user       = Maasha::Common::get_user();
65     $script     = Maasha::Common::get_scriptname();
66     $pid        = Maasha::Common::get_processid();
67
68     $file = "$ENV{ 'BP_TMP' }/" . join( ".", $user, $script, $pid ) . ".status";
69     $fh   = Maasha::Filesys::file_write_open( $file );
70
71     print $fh join( ";", $time_stamp, join( " ", @ARGV ) ) . "\n";
72
73     close $fh;
74 }
75
76
77 sub status_log
78 {
79     # Martin A. Hansen, June 2009.
80
81     # Retrieves initial status information written with status_set and uses this
82     # to write a status entry to the log file.
83
84     my ( $status,   # status - OPTIONAL
85        ) = @_;
86
87     # Returns nothing.
88
89     my ( $time0, $time1, $script, $user, $pid, $file, $fh, $elap, $fh_global, $fh_local, $line, $args, $tmp_dir );
90
91     $status ||= "OK";
92
93     $time1      = Maasha::Common::time_stamp();
94     $user       = Maasha::Common::get_user();
95     $script     = Maasha::Common::get_scriptname();
96     $pid        = Maasha::Common::get_processid();
97
98     $file = "$ENV{ 'BP_TMP' }/" . join( ".", $user, $script, $pid ) . ".status";
99
100     return if not -f $file;
101
102     $fh   = Maasha::Filesys::file_read_open( $file );
103     $line = <$fh>;
104     chomp $line;
105     close $fh;
106
107     unlink $file;
108
109     ( $time0, $args, $tmp_dir ) = split /;/, $line;
110
111     Maasha::Filesys::dir_remove( $tmp_dir ) if defined $tmp_dir;
112
113     $elap = Maasha::Common::time_stamp_diff( $time0, $time1 );
114
115     $fh_global = Maasha::Filesys::file_append_open( "$ENV{ 'BP_LOG' }/biopieces.log" );
116     $fh_local  = Maasha::Filesys::file_append_open( "$ENV{ 'HOME' }/.biopieces.log" );
117
118     print $fh_global join( "\t", $time0, $time1, $elap, $user, $status, "$script $args" ) . "\n";
119     print $fh_local  join( "\t", $time0, $time1, $elap, $user, $status, "$script $args" ) . "\n";
120
121     $fh_global->autoflush( 1 );
122     $fh_local->autoflush( 1 );
123
124     close $fh_global;
125     close $fh_local;
126 }
127
128
129 sub log_biopiece
130 {
131     # Martin A. Hansen, January 2008.
132
133     # Log messages to logfile.
134
135     # Returns nothing.
136
137     my ( $time_stamp, $user, $script, $fh_global, $fh_local );
138
139     $time_stamp = Maasha::Common::time_stamp();
140     $user       = Maasha::Common::get_user();
141     $script     = Maasha::Common::get_scriptname();
142
143     $fh_global  = Maasha::Filesys::file_append_open( "$ENV{ 'BP_LOG' }/biopieces.log" );
144     $fh_local   = Maasha::Filesys::file_append_open( "$ENV{ 'HOME' }/.biopieces.log" );
145
146     print $fh_global "$time_stamp\t$user\t$script ", join( " ", @ARGV ), "\n";
147     print $fh_local  "$time_stamp\t$user\t$script ", join( " ", @ARGV ), "\n";
148
149     $fh_global->autoflush( 1 );
150     $fh_local->autoflush( 1 );
151
152     close $fh_global;
153     close $fh_local;
154 }
155
156
157 sub read_stream
158 {
159     # Martin A. Hansen, July 2007.
160
161     # Opens a stream to STDIN or a file,
162
163     my ( $file,   # file - OPTIONAL
164        ) = @_;
165
166     # Returns filehandle.
167
168     my ( $fh );
169
170     if ( not -t STDIN ) {
171         $fh = Maasha::Filesys::stdin_read(); 
172     } elsif ( not $file ) {
173         # Maasha::Common::error( qq(no data stream) );
174     } else {
175         $fh = Maasha::Filesys::file_read_open( $file );
176     }
177     
178     return $fh;
179 }
180
181
182 sub write_stream
183 {
184     # Martin A. Hansen, August 2007.
185
186     # Opens a stream to STDOUT or a file.
187
188     my ( $path,   # path          - OPTIONAL
189          $gzip,   # compress data - OPTIONAL
190        ) = @_;
191
192     # Returns filehandle.
193
194     my ( $fh );
195
196     if ( $path ) {
197         $fh = Maasha::Filesys::file_write_open( $path, $gzip );
198     } else {
199         $fh = Maasha::Filesys::stdout_write();
200     }
201
202     return $fh;
203 }
204
205
206 sub close_stream
207 {
208     # Martin A. Hansen, May 2009.
209
210     # Close stream if open.
211
212     my ( $fh,   # filehandle
213        ) = @_;
214
215     # Returns nothing.
216
217     close $fh if defined $fh;
218 }
219
220
221 sub get_record
222 {
223     # Martin A. Hansen, July 2007.
224
225     # Reads one record at a time and converts that record
226     # to a Perl data structure (a hash) which is returned.
227
228     my ( $fh,   # handle to stream
229        ) = @_;
230
231     # Returns a hash. 
232
233     my ( $block, @lines, $line, $key, $value, %record );
234
235     return if not defined $fh;
236
237     local $/ = "\n---\n";
238
239     $block = <$fh>;
240
241     return if not defined $block;
242
243     chomp $block;
244
245     @lines = split "\n", $block;
246
247     foreach $line ( @lines )
248     {
249         ( $key, $value ) = split ": ", $line, 2;
250
251         $record{ $key } = $value;
252     }
253
254     return wantarray ? %record : \%record;
255 }
256
257
258 sub put_record
259 {
260     # Martin A. Hansen, July 2007.
261
262     # Given a Perl datastructure (a hash ref) emits this to STDOUT or a filehandle.
263
264     my ( $data,   # data structure
265          $fh,     # file handle - OPTIONAL
266        ) = @_;
267
268     # Returns nothing.
269
270     if ( scalar keys %{ $data } )
271     {
272         if ( $fh )
273         {
274             map { print $fh "$_: $data->{ $_ }\n" } keys %{ $data };
275             print $fh "---\n";
276         }
277         else
278         {
279             map { print "$_: $data->{ $_ }\n" } keys %{ $data };
280             print "---\n";
281         }
282     }
283
284     undef $data;
285 }
286
287
288 sub parse_options
289 {
290     # Martin A. Hansen, May 2009
291
292     # Parses and checks options for Biopieces.
293
294     # First the argument list is checked for duplicates and then
295     # options are parsed from ARGV after which it is checked if
296     # the Biopieces usage information should be printed. Finally,
297     # all options from ARGV are checked according to the argument list.
298
299     my ( $arg_list,   # data structure with argument description
300        ) = @_;
301
302     # Returns hashref.
303
304     my ( $arg, @list, $options );
305
306     # ---- Adding the mandatory arguments to the arg_list ----
307
308     push @{ $arg_list }, (
309         { long => 'help',       short => '?', type => 'flag',  mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
310         { long => 'stream_in',  short => 'I', type => 'file!', mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
311         { long => 'stream_out', short => 'O', type => 'file',  mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
312         { long => 'verbose',    short => 'v', type => 'flag',  mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
313     );
314
315     check_duplicates_args( $arg_list );
316
317     # ---- Compiling options list ----
318
319     foreach $arg ( @{ $arg_list } )
320     {
321         if ( $arg->{ 'type' } eq 'flag' ) {
322             push @list, "$arg->{ 'long' }|$arg->{ 'short' }";
323         } else {
324             push @list, "$arg->{ 'long' }|$arg->{ 'short' }=s";
325         }
326     }
327
328     # ---- Parsing options from @ARGV ----
329
330     $options = {};
331
332     Getopt::Long::GetOptions( $options, @list );
333
334     # print Dumper( $options );
335
336     check_print_usage( $options );
337
338     # ---- Expanding and checking options ----
339
340     foreach $arg ( @{ $arg_list } )
341     {
342         check_mandatory(  $arg, $options );
343         set_default(      $arg, $options );
344         check_uint(       $arg, $options );
345         check_int(        $arg, $options );
346         set_list(         $arg, $options );
347         check_dir(        $arg, $options );
348         check_file(       $arg, $options );
349         set_files(        $arg, $options );
350         check_files(      $arg, $options );
351         check_allowed(    $arg, $options );
352         check_disallowed( $arg, $options );
353     }
354
355     # print Dumper( $options );
356
357     # return wantarray ? $options : %{ $options }; # WTF! Someone changed the behaviour of wantarray???
358
359     return $options;
360 }
361
362
363 sub check_duplicates_args
364 {
365     # Martin A. Hansen, May 2009
366
367     # Check if there are duplicate long or short arguments,
368     # and raise an error if so.
369
370     my ( $arg_list,   # List of argument hashrefs,
371        ) = @_;
372
373     # Returns nothing.
374
375     my ( $arg, %check_hash );
376
377     foreach $arg ( @{ $arg_list } )
378     {
379         Maasha::Common::error( qq(Duplicate long argument: $arg->{ 'long' }) )   if exists $check_hash{ $arg->{ 'long' } };
380         Maasha::Common::error( qq(Duplicate short argument: $arg->{ 'short' }) ) if exists $check_hash{ $arg->{ 'short' } };
381
382         $check_hash{ $arg->{ 'long' } } = 1;
383         $check_hash{ $arg->{ 'short' } } = 1;
384     }
385 }
386
387
388 sub check_print_usage
389 {
390     # Martin A. Hansen, May 2009.
391
392     # Check if we need to print usage and print usage
393     # and exit if that is the case.
394
395     my ( $options,   # option hash
396        ) = @_;
397
398     # Returns nothing.
399
400     my ( $script, $wiki );
401
402     $script = Maasha::Common::get_scriptname();
403
404     if ( $script ne 'print_wiki' )
405     {
406         if ( exists $options->{ 'help' } or -t STDIN )
407         {
408             if ( not ( exists $options->{ 'stream_in' } or $options->{ 'data_in' } ) )
409             {
410                 if ( scalar keys %{ $options } <= 1 ) 
411                 {
412                     $wiki = $ENV{ 'BP_DIR' } . "/bp_usage/$script.wiki";
413                
414                     if ( exists $options->{ 'help' } ) {
415                         `print_wiki --data_in=$wiki --help`;
416                     } elsif ( $script =~ /^(list_biopieces|list_genomes|list_mysql_databases|biostat)$/ ) {
417                         return;
418                     } else {
419                         `print_wiki --data_in=$wiki`;
420                     }
421
422                     exit;
423                 }
424             }
425         }
426     }
427 }
428
429
430 sub check_mandatory
431 {
432     # Martin A. Hansen, May 2009.
433
434     # Check if mandatory arguments are set and raises an error if not.
435
436     my ( $arg,       # hashref
437          $options,   # options hash
438        ) = @_;
439
440     # Returns nothing.
441
442     if ( $arg->{ 'mandatory' } eq 'yes' and not defined $options->{ $arg->{ 'long' } } ) {
443         Maasha::Common::error( qq(Argument --$arg->{ 'long' } is mandatory) );
444     }
445 }
446
447
448 sub set_default
449 {
450     # Martin A. Hansen, May 2009.
451
452     # Set default values in option hash.
453
454     my ( $arg,      # hashref
455          $options,  # options hash
456        ) = @_;
457
458     # Returns nothing.
459
460     if ( not defined $options->{ $arg->{ 'long' } } ) {
461         $options->{ $arg->{ 'long' } } = $arg->{ 'default' }
462     }
463 }
464
465
466 sub check_uint
467 {
468     # Martin A. Hansen, May 2009.
469
470     # Check if value to argument is an unsigned integer and
471     # raises an error if not.
472
473     my ( $arg,      # hashref
474          $options,  # options hash
475        ) = @_;
476
477     # Returns nothing.
478
479     if ( $arg->{ 'type' } eq 'uint' and defined $options->{ $arg->{ 'long' } } )
480     {
481         if ( $options->{ $arg->{ 'long' } } !~ /^\d+$/ ) {
482             Maasha::Common::error( qq(Argument --$arg->{ 'long' } must be an unsigned integer - not $options->{ $arg->{ 'long' } }) );
483         }
484     }
485 }
486
487
488 sub check_int
489 {
490     # Martin A. Hansen, May 2009.
491
492     # Check if value to argument is an integer and
493     # raises an error if not.
494
495     my ( $arg,      # hashref
496          $options,  # options hash
497        ) = @_;
498
499     # Returns nothing.
500
501     if ( $arg->{ 'type' } eq 'int' and defined $options{ $arg->{ 'long' } } )
502     {
503         if ( $options->{ $arg->{ 'long' } } !~ /^-?\d+$/ ) {
504             Maasha::Common::error( qq(Argument --$arg->{ 'long' } must be an integer - not $options->{ $arg->{ 'long' } }) );
505         }
506     }
507 }
508
509
510 sub set_list
511 {
512     # Martin A. Hansen, May 2009.
513
514     # Splits an argument of type 'list' into a list that is put
515     # in the options hash.
516
517     my ( $arg,      # hashref
518          $options,  # options hash
519        ) = @_;
520
521     # Returns nothing.
522
523     if ( $arg->{ 'type' } eq 'list' and defined $options->{ $arg->{ 'long' } } ) {
524         $options->{ $arg->{ 'long' } } = [ split /,/, $options->{ $arg->{ 'long' } } ];
525     }
526 }
527
528
529 sub check_dir
530 {
531     # Martin A. Hansen, May 2009.
532
533     # Check if an argument of type 'dir!' truly is a directory and
534     # raises an error if not.
535
536     my ( $arg,      # hashref
537          $options,  # options hash
538        ) = @_;
539
540     # Returns nothing.
541
542     if ( $arg->{ 'type' } eq 'dir!' and defined $options->{ $arg->{ 'long' } } )
543     {
544         if ( not -d $options->{ $arg->{ 'long' } } ) {
545             Maasha::Common::error( qq(No such directory: "$options->{ $arg->{ 'long' } }") );
546         }
547     }
548 }
549
550
551 sub check_file
552 {
553     # Martin A. Hansen, May 2009.
554
555     # Check if an argument of type 'file!' truly is a file and
556     # raises an error if not.
557
558     my ( $arg,      # hashref
559          $options,  # options hash
560        ) = @_;
561
562     # Returns nothing.
563
564     if ( $arg->{ 'type' } eq 'file!' and defined $options->{ $arg->{ 'long' } } )
565     {
566         if ( not -f $options->{ $arg->{ 'long' } } ) {
567             Maasha::Common::error( qq(No such file: "$options->{ $arg->{ 'long' } }") );
568         }
569     }
570 }
571
572
573 sub set_files
574 {
575     # Martin A. Hansen, May 2009.
576
577     # Split the argument to 'files' into a list that is put on the options hash.
578
579     my ( $arg,      # hashref
580          $options,  # options hash
581        ) = @_;
582
583     # Returns nothing.
584
585     if ( $arg->{ 'type' } eq 'files' and defined $options->{ $arg->{ 'long' } } ) {
586         $options->{ $arg->{ 'long' } } = [ split /,/, $options->{ $arg->{ 'long' } } ];
587     }
588 }
589
590
591 sub check_files
592 {
593     # Martin A. Hansen, May 2009.
594
595     # Split the argument to 'files!' and check if each file do exists before adding
596     # the file list to the options hash.
597
598     my ( $arg,      # hashref
599          $options,  # options hash
600        ) = @_;
601
602     # Returns nothing.
603
604     my ( $elem, @files );
605
606     if ( $arg->{ 'type' } eq 'files!' and defined $options->{ $arg->{ 'long' } } )
607     {
608         foreach $elem ( split /,/, $options->{ $arg->{ 'long' } } )
609         {
610             if ( -f $elem ) {
611                 push @files, $elem;
612             } elsif ( $elem =~ /\*/ ) {
613                 push @files, glob( $elem );
614             }
615         }
616
617         if ( scalar @files == 0 ) {
618             Maasha::Common::error( qq(Argument to --$arg->{ 'long' } must be a valid file or fileglob expression - not $options->{ $arg->{ 'long' } }) );
619         }
620
621         $options->{ $arg->{ 'long' } } = [ @files ];
622     }
623 }
624
625
626 sub check_allowed
627 {
628     # Martin A. Hansen, May 2009.
629
630     # Check if all values to all arguement are allowed and raise an
631     # error if not.
632
633     my ( $arg,      # hashref
634          $options,  # options hash
635        ) = @_;
636
637     # Returns nothing.
638
639     my ( $elem );
640
641     if ( defined $arg->{ 'allowed' } and defined $options->{ $arg->{ 'long' } } )
642     {
643         map { $val_hash{ $_ } = 1 } split /,/, $arg->{ 'allowed' };
644
645         if ( $arg->{ 'type' } =~ /^(list|files|files!)$/ )
646         {
647             foreach $elem ( @{ $options->{ $arg->{ 'long' } } } )
648             {
649                 if ( not exists $val_hash{ $elem } ) {
650                     Maasha::Common::error( qq(Argument to --$arg->{ 'long' } $elem is not allowed) );
651                 }
652             }
653         }
654         else
655         {
656             if ( not exists $val_hash{ $options->{ $arg->{ 'long' } } } ) {
657                 Maasha::Common::error( qq(Argument to --$arg->{ 'long' } $options->{ $arg->{ 'long' } } is not allowed) );
658             }
659         }
660     }
661 }
662
663
664 sub check_disallowed
665 {
666     # Martin A. Hansen, May 2009.
667
668     # Check if any values to all arguemnts are disallowed and raise an error if so.
669
670     my ( $arg,      # hashref
671          $options,  # options hash
672        ) = @_;
673
674     # Returns nothing.
675
676     my ( $val, %val_hash );
677
678     if ( defined $arg->{ 'disallowed' } and defined $options->{ $arg->{ 'long' } } )
679     {
680         foreach $val ( split /,/, $arg->{ 'disallowed' } )
681         {
682             if ( $options->{ $arg->{ 'long' } } eq $val ) {
683                 Maasha::Common::error( qq(Argument to --$arg->{ 'long' } $val is disallowed) );
684             }
685         }
686     }
687 }
688
689
690 sub getopt_files
691 {
692     # Martin A. Hansen, November 2007.
693
694     # Extracts files from an explicit GetOpt::Long argument
695     # allowing for the use of glob. E.g.
696     # --data_in=test.fna
697     # --data_in=test.fna,test2.fna
698     # --data_in=*.fna
699     # --data_in=test.fna,/dir/*.fna
700
701     my ( $option,   # option from GetOpt::Long
702        ) = @_;
703
704     # Returns a list.
705
706     my ( $elem, @files );
707
708     foreach $elem ( split ",", $option )
709     {
710         if ( -f $elem ) {
711             push @files, $elem;
712         } elsif ( $elem =~ /\*/ ) {
713             push @files, glob( $elem );
714         }
715     }
716
717     return wantarray ? @files : \@files;
718 }
719
720
721 sub sig_handler
722 {
723     # Martin A. Hansen, April 2008.
724
725     # Removes temporary directory and exits gracefully.
726     # This subroutine is meant to be run always as the last
727     # thing even if a script is dies or is interrupted
728     # or killed. 
729
730     my ( $sig,   # signal from the %SIG
731        ) = @_;
732
733     # print STDERR "signal->$sig<-\n";
734
735     my $script = Maasha::Common::get_scriptname();
736
737     chomp $sig;
738
739     sleep 1;
740
741     if ( $sig =~ /MAASHA_ERROR/ )
742     {
743         print STDERR "\nProgram '$script' had an error"                     . "  -  Please wait for temporary data to be removed\n";
744         status_log( "ERROR" );
745     }
746     elsif ( $sig eq "INT" )
747     {
748         print STDERR "\nProgram '$script' interrupted (ctrl-c was pressed)" . "  -  Please wait for temporary data to be removed\n";
749         status_log( "INTERUPTED" );
750     }
751     elsif ( $sig eq "TERM" )
752     {
753         print STDERR "\nProgram '$script' terminated (someone used kill?)"  . "  -  Please wait for temporary data to be removed\n";
754         status_log( "TERMINATED" );
755     }
756     else
757     {
758         print STDERR "\nProgram '$script' died->$sig"                       . "  -  Please wait for temporary data to be removed\n";
759         status_log( "DIED" );
760     }
761
762     clean_tmp();
763
764     exit( 0 );
765 }
766
767
768 sub clean_tmp
769 {
770     # Martin A. Hansen, July 2008.
771
772     # Cleans out any unused temporary files and directories in BP_TMP.
773
774     # Returns nothing.
775
776     my ( $tmpdir, @dirs, $curr_pid, $dir, $user, $sid, $pid );
777
778     $tmpdir = $ENV{ 'BP_TMP' } || Maasha::Common::error( 'No BP_TMP variable in environment.' );
779
780     $curr_pid = Maasha::Common::get_processid();
781
782     @dirs = Maasha::Filesys::ls_dirs( $tmpdir );
783
784     foreach $dir ( @dirs )
785     {
786         if ( $dir =~ /^$tmpdir\/(.+)_(\d+)_(\d+)_bp_tmp$/ )
787         {
788             $user = $1;
789             $sid  = $2;
790             $pid  = $3;
791
792 #            next if $user eq "maasha"; # DEBUG
793
794             if ( $user eq Maasha::Common::get_user() )
795             {
796                 if ( not Maasha::Common::process_running( $pid ) )
797                 {
798                     # print STDERR "Removing stale dir: $dir\n";
799                     Maasha::Filesys::dir_remove( $dir );
800                 }
801                 elsif ( $pid == $curr_pid )
802                 {
803                     # print STDERR "Removing current dir: $dir\n";
804                     Maasha::Filesys::dir_remove( $dir );
805                 }
806             }
807         }
808     }
809 }
810
811
812 sub get_tmpdir
813 {
814     # Martin A. Hansen, April 2008.
815
816     # Create a temporary directory based on
817     # $ENV{ 'BP_TMP' } and sessionid. The directory
818     # name is written to the status file.
819
820     # Returns a path.
821
822     my ( $user, $sid, $pid, $script, $path, $file, $fh, $line );
823
824     Maasha::Common::error( qq(no BP_TMP set in %ENV) ) if not -d $ENV{ 'BP_TMP' };
825
826     $user   = Maasha::Common::get_user();
827     $sid    = Maasha::Common::get_sessionid();
828     $pid    = Maasha::Common::get_processid();
829     $script = Maasha::Common::get_scriptname();
830
831     $path = "$ENV{ 'BP_TMP' }/" . join( "_", $user, $sid, $pid, "bp_tmp" );
832     $file = "$ENV{ 'BP_TMP' }/" . join( ".", $user, $script, $pid ) . ".status";
833     
834     $fh   = Maasha::Filesys::file_read_open( $file );
835     $line = <$fh>;
836     chomp $line;
837     close $fh;
838
839     $fh   = Maasha::Filesys::file_write_open( $file );
840     print $fh "$line;$path\n";
841     close $fh;
842
843     Maasha::Filesys::dir_create( $path );
844
845     return $path;
846 }
847
848
849 sub biopiecesrc
850 {
851     # Martin A. Hansen, July 2009.
852
853     # Read Biopiece configuration info from .biopiecesrc.
854     # and returns the value of a given key.
855
856     my ( $key,   # configuration key
857        ) = @_;
858
859     # Returns a string.
860
861     my ( $file, $fh, $record );
862
863     $file = "$ENV{ 'HOME' }/.biopiecesrc";
864
865     return undef if not -f $file;
866
867     $fh     = Maasha::Filesys::file_read_open( $file );
868     $record = get_record( $fh );
869     close $fh;
870
871     if ( exists $record->{ $key } ) {
872         return $record->{ $key };
873     } else {
874         return undef;
875     }
876 }
877
878
879 END
880 {
881     clean_tmp();
882 }
883
884
885 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
886
887
888 1;
889
890 __END__