ó
ŕxTc           @   s´  d  Z  d d l m Z d d l Z d d l Z d d l Z d d l Z d d l Z d d l Z d d l	 Z	 d d l
 m Z d d l m Z i e d 6e d 6e d 6e d	 6e d
 6e d 6e d 6e d 6e d 6e d 6e d 6e d 6e d 6Z d Z d Z d Z d Z d Z d Z d   Z d   Z d   Z d   Z d d d d d d d d d d d d d e d   Z d! d d d" d# d$  Z e d% k r°e j  d Z! e e!  Z" e e"   n  d S(&   s,  
libtbx.qsub(
 where=path_to_where_you_want_to_run_your_jobs,
 source="/net/chevy/raid1/afonine/build/setpaths.csh",
 commands=[list of commands])

where [list of commands]) is

["python run.py model1.pdb data1.mtz",
 "python run.py model2.pdb data2.mtz",
...,
"python run.py modelN.pdb dataN.mtz" ]
i˙˙˙˙(   t   divisionN(   t   easy_run(   t   Sorryt   phenix_sourcet   wheret   commandst   number_of_chunkst   size_of_chunkst   codet   jst   qsub_cmdt   startt   endt   host_scratch_dirt   python_scriptt   parallel_nodess  
import os, sys
from libtbx import easy_run

cmds = [
%s
]

def run(only_i=None,
        chunk_n=1,
        chunk_size=len(cmds),
        ):
  try: only_i = int(only_i)
  except ValueError: only_i=None

  try: chunk_n = int(chunk_n)
  except ValueError: chunk_n=1
  try: chunk_size = int(chunk_size)
  except ValueError: chunk_size=len(cmds)

  #assert chunk_size==len(cmds) or chunk_n==1
  assert chunk_n>0
  assert chunk_size>0
  #if chunk_n!=1:
  #  chunk_size = (len(cmds)-1)//chunk_n+1
  #elif chunk_size!=1:
  #  chunk_n = len(cmds)%%chunk_size+1

  for i, cmd in enumerate(cmds):
    if only_i is None:
      print i, cmd
      continue
    else:
      if chunk_n!=1 or chunk_size!=len(cmds):
        if only_i!=i//chunk_size+1: continue
      else:
        if only_i!=i+1: continue
    print 'Running', i+1
    print 'Command', cmd
    easy_run.call(cmd)

if __name__=="__main__":
  run(*tuple(sys.argv[1:]))
s  
set work_dir=$PWD

set host_scratch_dir=/net/$host/scratch1/$user
if (! -d $host_scratch_dir) then
  echo "================================"
  echo " Creating host scratch directory $host_scratch_dir"
  echo "================================"
  mkdir $host_scratch_dir
endif

set host_scratch_dir=/net/$host/scratch1/$user/%s
if (! -d $host_scratch_dir) then
  echo "================================"
  echo " Creating host scratch directory $host_scratch_dir"
  echo "================================"
  mkdir $host_scratch_dir
endif

set host_scratch_dir=/net/$host/scratch1/$user/%s/$SGE_TASK_ID
if (! -d $host_scratch_dir) then
  echo "================================"
  echo " Creating host scratch directory $host_scratch_dir"
  echo "================================"
  mkdir $host_scratch_dir
endif

echo "=============="
echo " Changing into $host_scratch_dir"
echo "=============="
cd $host_scratch_dir
sk   
echo "==================="
echo " Move files back to $work_dir"
echo "==================="
mv * $work_dir
sĽ   #! /bin/csh -q
#$ -cwd
#$ -o %s_queue.output -j y -N %s
limit datasize 2000000

source %s

%s

libtbx.python %s $SGE_TASK_ID %s %s >& %s.$SGE_TASK_ID.out

%s

exit

sÍ   
import os, sys

def run(only_i=None):
  print "%s " % only_i
  f=file("test_%s.output" % only_i, "wb")
  f.write("Testing %s\n" % only_i)
  f.close()

if __name__=="__main__":
  run(*tuple(sys.argv[1:]))
s2   #! /bin/csh -q
#$ -j y
while (1)
  sleep 3600
end
c    
      C   sB  d   }  d   } d   } d   } d d GHd GHd d GHd } | } xă d	 d
 d	 d	 g d	 d d d	 g d d d d g d	 d d d	 g d d d d g d	 d d d g d	 d d d g d d d d g d d d d	 g g	 D]T \ } } } }	 | | } |    | |  t  j |  |   | d | d |	  qŐ W|    t j   d  S(   Nc          S   sL   xE t  j t  j    D]. }  |  j d  r d G|  GHt  j |   q q Wd  S(   NR   t   remove(   t   ost   listdirt   getcwdt
   startswithR   (   t   filename(    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/command_line/easy_qsub.pyt   _clean¤   s    	c         S   sg   d GHd } x& t  |   D] } | d | d 7} q W| GHt d d  } | j | d   | j   d  S(   Ns   
  test list of commandst    s   echo %d
ie   s   commands.txtt   wbi˙˙˙˙(   t   ranget   filet   writet   close(   t   number_of_cmdst   outlt   it   f(    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/command_line/easy_qsub.pyt   _write_cmdsŠ   s    c          S   sx   d d  l  }  |  j d  t } xR | rs t } x? t j d  D]. } | j d  d k r> d G| GHt } Pq> q> Wq" Wd  S(   Ni˙˙˙˙i   t   qstatR   t   running(   t   timet   sleept   Truet   FalseR   t   popent   find(   R$   R#   t   line(    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/command_line/easy_qsub.pyt   _wait˛   s    		c         S   sú   g  } xÓ t  j t  j    D]ź } | j d  r | j d  r |  d 8}  t | d  } | j   } | j   xf | D][ } | j d  d k rv | d 8} | j	   } | d | k s˝ t
  | j | d  qv qv Wq q W|  sé t
  | sö t
  d  S(   NR   s   .outi   t   rbt   Runningi˙˙˙˙(   R   R   R   R   t   endswithR   t	   readlinesR   R)   t   splitt   AssertionErrort   append(   t   number_of_output_filest   number_of_runst   runsR   R    t   linesR*   t   tmp(    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/command_line/easy_qsub.pyt   _check˝   s    


t   #iP   s   # testing easy_qsubsT   libtbx.easy_qsub phenix_source="/net/cci/xp/phenix/phenix_env" commands=commands.txti
   R   s    number_of_chunks=2i   i   s    size_of_chunks=2i   i   s    start=6s    end=5i   s    start=5 size_of_chunks=2i   s    end=5 size_of_chunks=2R3   R4   (   R   t   systemt   syst   exit(
   R   R!   R+   R8   t   cmdt   old_cmdt   cmdst   new_cmdR3   R4   (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/command_line/easy_qsub.pyt   test_easy_qsubŁ   s8    							%

c         C   sę   i  } xÝ |  D]Ő } | d k r1 d GHt  j   n0 | d k rH t   i  S| d k ra t | d <n  x~ t D]R } | j d |  d k rh | j d	  d
 | | <t | | |  | | <Pqh qh W| d k r d | GHd sâ t  q q W| S(   Ns   --helps   -hs   
  Program to sumbit jobs easily to a SGE queue
    e.g.

    libtbx.easy_qsub phenix_source="/net/cci/xp/phenix/phenix_env" commands=commands.txt

  s   --tests   --dryt   dry_runs   %s=i    t   =i   s   
  failed to process "%s"
(   s   --helps   -h(   s   --dry(   R;   R<   RA   R&   t	   key_wordsR)   R0   R1   (   t   argst   kwdst   tt   key(    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/command_line/easy_qsub.pyt   process_argsí   s&    	c          C   sî   d }  t  j d |   } t j   } | j d |  i  } x­ | j   j d  D] } | j d  d k rP | j   } | j | d g   | | d j t	 | d j d	  d   | | d j t	 | d j d	  d
   qP qP W| S(   Ns   qstat -ft   commandt   outs   
s   all.q@i˙˙˙˙i    i   t   /i   (
   R   t   fully_bufferedt   StringIOt   show_stdoutt   getvalueR0   R)   t
   setdefaultR2   t   int(   R=   t   eroRK   t   rcR*   R7   (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/command_line/easy_qsub.pyt   get_queue_machine_details	  s    ,3c         C   s   d d  l  } | j j j   S(   Ni˙˙˙˙(   t   libtbx.load_envt   envt
   python_exet   sh_value(   t   sourcet   libtbx(    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/command_line/easy_qsub.pyt   get_python_bin  s    i   i    t   qsubc   $         sŃ  d } |  sT d d GHt  j j d d  s3 | GHd  Sd GHd t  j j d d  }  n  | p] | sä d d GHd GHt d	 d
  } | j t  | j   g  } xG t d  D]6 } | j d t  j	 j
 t  j   d	  | d f  q§ Wn  d d GHd GHd G|  GH|  j d  d k r.|  j d  d k r.d GHt St  j	 j |   sSt d |    n  d G| GHt |  t g   k rź| d  k rd } n  d Gt |  Gt |  d k r<d | d GHq<n | d  k	 röt  j	 j |  rö| d  k r<| d  } q<nF | d  k	 r<t  j	 j |  r<| d  k r<| j d d  d  } q<n  | j d d  } d G| GHd G| GH| d k rud GHd  Sd d GHt  j   } | d  k r| } n  |  sŤt  t |  t g   k rĚ| } nŇ | d  k	 rt  j	 j |  rt | d   } | j   } | j   n | d  k	 rt  j	 j |  r|	 sEt d!   n  t |   } | s]t  g  } x8 t | |	 d  D]  } | j d" | | | f  qwWn  t |  } d# G| GHt | |  } | d  k r| d k rét |  } q=| | } | | r=| d 7} q=n0 | | } t d |  } | | r=| d 7} n  | } d$ G| GHd% G| GH| d k r  f d&   } | d k st d'   t       j   } | j |  xÜ | D]Á } |   | d   | d k rąt d( d
  } | j t  | j   t  j d( t j t j  Bt j! B | d) | 7} d* | | d f } d+ | d | f GHd, GHd- G| GHt" j# |  PqąqąWt d. |   n  t  j$ |  |
 rşd/ GHt% |
 |
 f } t& } n d } d } d0 } d1 } d } x9 | D]1 } | d d2 k r| d  } n  | d3 | 7} qßWt  j	 j
 t  j   |  } d4 | GHt | d
  } | j t' |  | j   t  j	 j
 t  j   |  } d5 | GHt | d
  } | j t( | | |  | | | | | | f	  | j   |	 d  k	 rét | |	  } n  d6 | | | | | f } d7 GHd8 | GH| rd9 GHn  t" j) d: |  } t* j*   }  | j+ d; |   t, j- d<  }! xb |  j.   j/ d2  D]@ } | sqqn  |! j0 |  }" |" rqt1 |" j2 d   }# PqqqqWd= GHd  }# t  j$ |  |# S(>   Ns  
  Submits a job to run commands on the queueing system using qsub. One master
  job is sent, which then deploys multiple sub jobs (Of the same job ID) to run
  each separate command.

  phenix_source: A path to the script used to initial the phenix environment
  where: A path to the directory in which to run the commands
  commands: A list of commands to run on the queue
  size_of_chunks: Number of commands to run within each queue job
  code: The task name used during submissions
  js: queue priority (default 0)
  qsub_cmd: The command to submit a job from a shell
  start: The start number for the task range
  end: The end number for the task range
  host_scratch_dir: If not None, will run the job within a local scratch
                    directory on each queue machine, then copy back all output
                    files to the directory set by where

  Returns the job id of the submission.

  Example of python script:
  run(
    where = path_to_where_you_want_to_run_your_jobs,
    phenix_source = "/net/chevy/raid1/nigel/build/setpaths.csh",
    commands = ["phenix.fetch_pdb 101m", "phenix.fetch_pdb 1s72"])

  For more help type:
    libtbx.easy_qsub --help
  t   -iP   t   PHENIXR   sE   
  Automatically setting phenix_source to current $PHENIX/phenix_env
s   %s/phenix_envs5   
  Generating a test run script and queuing 10 times
s   easy_qsub_test_script.pyR   i
   s   libtbx.python %s %sid   s     Inputss       phenix_sourcet
   phenix_envi˙˙˙˙t   setpaths0     Need to supply file to source. e.g. phenix_envs1   source file for PHENIX environment not found "%s"s	       wheret	   easy_qsubs       commandsi   s   similar to

> %s
i    s   ../RL   s       size_of_chunkss       number_of_chunkss$   
  Need to choose number_of_chunks>1R,   s(   Need to supply "end=n" for python_scripts   %s %s %ds"   
  Number of lines in command files   
  Number of queue jobss%     Number of command in each queue jobc            s<     |  d   |  d   | d   | d k r8 d Sd S(   Ni   i    i˙˙˙˙(    (   t   k1t   k2(   t   details(    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/command_line/easy_qsub.pyt   _cmp_gapĽ  s    4s#   Only one job can be run in parallels
   qblock.cshs    -q %ss   %s -t 1-%d qblock.cshs     Blocking %d slots on %ss        Need to remove them manuallys      s.   No queue machine found with %d available slotss0   
  Setting up scratch directories on queue hostss   easy_qsub_python_script.pys   easy_qsub_qsub_script.shs   
s     '''%s ''',
s%     Writing queue python script:
    %ss&     Writing queue command script:
    %ss   %s -t %d-%d -js %d %ss   
  Queue command
s   > %s
s     Skipping run...RJ   RK   s6   Your job-array (\d+)\..* \(\".*\"\) has been submitteds   Unable to determine job ID(3   R   t   environt   getR   R   t   test_run_scriptR   R   R2   t   patht   joinR   R)   R'   t   existsR   t   typet   Nonet   lent   replaceR1   R/   R\   t   mint   maxRU   t   keyst   sortt   qblockt   chmodt   statt   S_IREADt   S_IWRITEt   S_IXUSRR   t   callt   chdirt   host_scratch_dir_pret   host_scratch_dir_postt   script_filet   run_fileRM   RN   RO   t   ret   compileRP   R0   t   searchRR   t   group($   R   R   R   R   R   R   R	   R
   R   R   R   R   R   RB   t   helpR    R   t	   old_whereR6   t   phenix_python_bint   number_of_jobsRf   Rs   t
   queue_nameR=   t   pret   postt   python_run_filenamet   qsub_run_filenameR   R*   RS   RK   t   patternt   matcht   job_id(    (   Re   sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/command_line/easy_qsub.pyt   run  sF   +		
			*	 	  					 !	

 

 			 
!		 	
		

		 Rb   R"   i
   c   	      C   s  | rU t  | d  r= g  | D] } t |  j   ^ q } qU t |  j   g } n  |  rŞ t  |  d  r g  |  D] } t |  j   ^ qq }  qŞ t |   j   g }  n  xÖ t rt j | g d t j } | j   d } x | j d  d D]{ } | sqň n  | r$| d  j   | k r$Pn  | d d !j   | k rň |  d k	 ri| d	 d
 !j   |  k rjPqjqmPqň qň WPt
 j |  q­ Wd S(   s  
  Repeatedly checks the queue and waits until all jobs for a user have
  completed. The caller may specify further information, such as the job name,
  id, and user if jobs of another user are to be monitored.

  This function returns when there are no more jobs matching any of the
  parameters. i.e. No jobs matching <user>, no jobs matching <job_id>, and no
  jobs matching <task_name>.

  Parameters
  ----------
  task_name : int or str or list of int or list of str or None, optional
  job_id : int or str or list of int or list of str or None, optional
  user : str, optional
  qstat_cmd : str, optional
  sleep_time : int, optional
  t   __iter__t   stdouti    s   
i   i   i   i'   i   i   N(   t   hasattrt   strt   stripR&   t
   subprocesst   Popent   PIPEt   communicateR0   Rn   R$   R%   (	   t	   task_nameR   t   usert	   qstat_cmdt
   sleep_timeR   t   pR   R*   (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/command_line/easy_qsub.pyt   wait  s.    ((	 t   __main__(#   t   __doc__t
   __future__R    R   R   R   RN   R;   R$   Rw   R[   R   t   libtbx.utilsR   R   RR   RD   R   R}   R~   R   Ri   Ru   RA   RI   RU   R\   Rn   R'   R   R    t   __name__t   argvRE   RF   (    (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/command_line/easy_qsub.pyt   <module>   sr   
.!	J			ç<