inprogress
[jalview.git] / forester / ruby / scripts / rb_qsub.rb
1 #!/usr/local/bin/ruby -w
2 #
3 # = rb_qsub 
4 #
5 # Copyright::  Copyright (C) 2006-2008 Christian M. Zmasek
6 # License::    GNU Lesser General Public License (LGPL)
7 #
8 # $Id: rb_qsub.rb,v 1.6 2008/08/30 19:57:59 cmzmasek Exp $
9 #
10 # last modified: 11/13/2007
11 #
12 #
13 # To execute qsub commands.
14 # Each line l (unless precded by a # or space) in file
15 # 'commands.qsub' is executed as 'qsub l'
16
17
18 module ForesterScripts
19
20     if RUBY_VERSION !~ /1.9/
21         puts( "Your ruby version is #{RUBY_VERSION}, expected 1.9.x " )
22         exit( -1 )
23     end     
24     
25     CMDS_FILE    = 'commands.qsub'
26     TMP_CMD_FILE = '__QSUB_RB_CMD__'
27     PRG_NAME     = 'rb_qsub'
28
29     if ( !File.exists?( CMDS_FILE ) ) 
30         puts( '[' +PRG_NAME + '] > commands file "' + CMDS_FILE + '" not found' )
31         Process.exit!          
32     end    
33     
34     puts( '[' +PRG_NAME + '] > reading ' + CMDS_FILE )
35
36     open( CMDS_FILE ).each { |line| 
37         if ( line.length > 1 && line =~ /^[^#]\S+/ )
38             if ( File.exists?( TMP_CMD_FILE ) ) 
39                 File.delete( TMP_CMD_FILE ) 
40             end
41             open( TMP_CMD_FILE, 'w' ) do |f|
42                 f.write( line )
43             end 
44             puts( '[' +PRG_NAME + '] > excuting ' + line )
45             IO.popen( 'qsub ' + TMP_CMD_FILE , 'r+' ) do |pipe|
46                 pipe.close_write
47                 puts pipe.read
48             end
49             if ( File.exists?( TMP_CMD_FILE ) ) 
50                 File.delete( TMP_CMD_FILE ) 
51             end
52             sleep( 10.0 )
53         end
54     }
55     puts( '[' +PRG_NAME + '] > OK.' )
56     puts
57
58 end
59