3
d                 @   sf   d Z ddlZddlZddlmZ ddlmZ ddlmZ ddl	m
Z
mZ ejd	ZG d
d de
ZdS )z$Parallel workflow execution via LSF
    N)sleep   )logging)CommandLine   )SGELikeBatchManagerBaseloggerznipype.interfacec                   s0   e Zd ZdZ fddZdd Zdd Z  ZS )	LSFPluginaA  Execute using LSF Cluster Submission

    The plugin_args input to run can be used to control the LSF execution.
    Currently supported options are:

    - template : template to use for batch job submission
    - bsub_args : arguments to be prepended to the job execution script in the
                  bsub call

    c                s   d}d| _ d| _d| _d|krt|d rtd|d kr@|d d | _ d|d krZ|d d | _d|d krt|d d | _tt| j|f| d S )Nz
#$ -S /bin/sh
            plugin_argsZretry_timeoutZ	max_tries	bsub_args)_retry_timeout
_max_tries
_bsub_argssuperr	   __init__)selfkwargstemplate)	__class__ =/tmp/pip-build-7vycvbft/nipype/nipype/pipeline/plugins/lsf.pyr      s    zLSFPlugin.__init__c             C   sj   t dddd}d| |j_tj}tjtjd |jdd}tj| d	|j	j
ks^d
|j	j
krbdS dS dS )aE  LSF lists a status of 'PEND' when a job has been submitted but is
        waiting to be picked up, and 'RUN' when it is actively being processed.
        But _is_pending should return True until a job has finished and is
        ready to be checked for completeness. So return True if status is
        either 'PEND' or 'RUN'ZbjobsF	allatonce)resource_monitorterminal_outputz%dCRITICALT)Zignore_exceptionZDONEZEXITN)r   inputsargsifloggerlevelsetLevelr   getLevelNamerunruntimestdout)r   taskidcmdoldlevelresultr   r   r   _is_pending,   s    
zLSFPlugin._is_pendingc             C   s  t dttjddd}d}| jr&| j}d|jkrbd|jkrP|jd rP|jd }n|d|jd  7 }d	|krzd
||d f }d|krd||d f }|jrdjttjd |j|jf}ndjttjd |jf}|j	d}|j
  dj|}d|||f |j_tjd|jj  tj}tjtjd d}xy|j }	W nj tk
r }
 zL|| jk rn|d7 }t| j n&tj| tdjd|j t|
fW Y d d }
~
X nX P q.W tj| tjd|	jj}|rt|j  d }nt!d|	jj |j" | j#|< tjd||jf  |S )NZbsubFr   )environr   r   r   r   	overwrite z-oz%s -o %sz.logz-ez%s -e %s.LOGNAMEz%s -J %s sh %szbsub r   r   r   
z%Could not submit lsf task for node %sz<(\d*)>z(Can't parse submission job output id: %sz"submitted lsf task: %d for node %s)$r   dictosr+   r   r   Z
_hierarchyjoin_idsplitreverser   r   r   debugr   r    r!   r   r"   r#   	Exceptionr   r   r   RuntimeErrorstrresearchr$   r%   intgroupsIOError
output_dirZ_pending)r   Z
scriptfilenoder'   ZbsubargsZjobnameZjobnameitemsr(   triesr)   ematchr&   r   r   r   _submit_batchtask?   sh    
 


 
zLSFPlugin._submit_batchtask)__name__
__module____qualname____doc__r   r*   rE   __classcell__r   r   )r   r   r	      s   
r	   )rI   r2   r;   timer   r   r   Zinterfaces.baser   baser   r   	getLoggerr   r	   r   r   r   r   <module>   s   
