3
d                 @   sv   d Z ddlZddlZddlmZ ddlZddl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 )z7Parallel workflow execution via OAR http://oar.imag.fr
    N)sleep   )logging)CommandLine   )SGELikeBatchManagerBaseloggerznipype.interfacec                   s8   e Zd ZdZdZdZ fddZdd Zdd	 Z  Z	S )
	OARPluginax  Execute using OAR

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

    - template : template to use for batch job submission
    - oarsub_args : arguments to be prepended to the job execution
                    script in the oarsub call
    - max_jobname_len: maximum length of the job name.  Default 15.

        c                s   d}d| _ d| _d| _d|kr|d rd|d kr@|d d | _d|d krZ|d d | _ d|d krt|d d | _d|d kr|d d | _tt| j|f| d S )	Nz
# oarsub -J
           r
   plugin_argsoarsub_argsZretry_timeoutZ	max_triesZmax_jobname_len)_retry_timeout
_max_triesZ_max_jobname_length_oarsub_args_max_jobname_lensuperr	   __init__)selfkwargstemplate)	__class__ =/tmp/pip-build-7vycvbft/nipype/nipype/pipeline/plugins/oar.pyr   "   s    zOARPlugin.__init__c             C   sP   t jdddd|gt jt jd}|j \}}tj|| j }d|koJd|k}|S )NZoarstatz-Jz-sz-j)stdoutstderrerrorZ
terminated)
subprocessPopenPIPEcommunicatejsonloadslower)r   taskidprocoeparsed_resultZ
is_pendingr   r   r   _is_pending4   s    
zOARPlugin._is_pendingc             C   s  t dttjddd}tjj|}d}| jr2| j}d|jkrnd|jkr\|jd r\|jd }n|d|jd  7 }|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| j }d|krd|tjj	||d f }d|krd|tjj	||d f }d|kr0d| }tj|tjtjB tjB  d|||f |j_tj}tjtjd d}	xy|j }
W nj tk
r } zL|	| jk r|	d7 }	t| j n&tj| tdj	d|j
 t |fW Y d d }~X nX P qxW tj| d}d}xN|
j!j"j# D ]>}|j$ j%dr0d}|rB||d 7 }|j$ j%drP qW t&j'|d }|j( | j)|< t*j+d||j
f  |S )NZoarsubFZ	allatonce)environZresource_monitorZterminal_outputr   r   	overwrite .LOGNAMEr   z-Oz%s -O %sz.stdoutz-Ez%s -E %sz.stderrz-Jz%s -Jz%s -n %s -S %sCRITICALr   
z%Could not submit OAR task for node %s{T}Zjob_idz"submitted OAR task: %s for node %s),r   dictosr+   pathdirnamer   r   Z
_hierarchyjoin_idsplitreverser   chmodstatS_IEXECS_IREADS_IWRITEinputsargsifloggerlevelsetLevelr   getLevelNamerun	Exceptionr   r   r   RuntimeErrorstrZruntimer   
splitlinesstrip
startswithr"   r#   
output_dirZ_pendingr   debug)r   Z
scriptfilenodecmdr6   Z
oarsubargsZjobnameZjobnameitemsZoldleveltriesresultr(   r'   addliner%   r   r   r   _submit_batchtaskB   s|    
 




 
zOARPlugin._submit_batchtask)
__name__
__module____qualname____doc__r   r   r   r*   rV   __classcell__r   r   )r   r   r	      s   r	   )rZ   r5   r=   timer   r   Z
simplejsonr"   r   r   Zinterfaces.baser   baser   r   	getLoggerrC   r	   r   r   r   r   <module>   s   
