3
d                 @   sL   d Z ddlZddlZddlmZ ddlmZmZ dd ZG d	d
 d
eZ	dS )z$Parallel workflow execution via SGE
    N   )CommandLine   )GraphPluginBaseloggerc             C   sX   | j dkp| j dko| jj }d}y| j \}}}}W n tk
rN   d}Y nX |oV|S )z
    A function to determine if a node has previously completed it's work
    :param checknode: The node to check the run status
    :return: boolean value True indicates that the node does not need to be run.
    FN)	overwriteZ
_interfaceZ
always_runhash_exists	Exception)Z	checknodeZ%node_state_does_not_require_overwriter   _ r   B/tmp/pip-build-7vycvbft/nipype/nipype/pipeline/plugins/sgegraph.pynode_completed_status   s    

r   c                   s,   e Zd ZdZdZ fddZdd Z  ZS )SGEGraphPlugina.  Execute using SGE

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

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

    z#
#!/bin/bash
#$ -V
#$ -S /bin/bash
c                s   d| _ d| _d|krx|d rx|d }d|krT|d | _tjj| jrTt| jj | _d|krf|d | _ d|krx|d | _tt	| j
f | d S )N Fplugin_argstemplate	qsub_argsZdont_resubmit_completed_jobs)
_qsub_args_dont_resubmit_completed_jobs	_templateospathisfileopenreadsuperr   __init__)selfkwargsr   )	__class__r   r   r   /   s    

zSGEGraphPlugin.__init__c              C   s  dd }t jj|d \}}t jj|d}t }| jrxpt|D ]d\}	}
||	 }t|}|r|	|krx6||	 D ]*}||kr|| }nt|| }|o|}qnW |||	< q@W t|d}|j	d |j	d xt|D ]\}	}
||	 }|j
|	drqq| j|d	d
g\}}t jj|
\}}dj|jdd d$ }dj|dtj|
f f}t jj|d| }|d }|d }t|d}|j	| |j  W d Q R X d}|	|krd}x<||	 D ]0}| j s||  r|dj|||7 }qW |dkr|jd}d| }||	|}d}| jjddkr.dj|d}d}| jjddkrPdj|d}dj||||||d}|j	| qW W d Q R X tdtt jdd d!}d"| |j_|j  tjd# d S )%Nc             S   s2   dj | ||  j}|jddjddjdd}|S )z
            - jobnumber: The index number of the job to create
            - nodeslist: The name of the node being processed
            - return: A string representing this job to be displayed by SGE
            zj{0}_{1}-r
   .:)format_idreplace)Z	jobnumberZ	nodeslistZjob_namer   r   r   make_job_nameA   s    z3SGEGraphPlugin._submit_graph.<locals>.make_job_namer   zsubmit_jobs.shwtz#!/usr/bin/env bash
z# Condense format attempted
Fr   r   r!   r   
z%s %szbatchscript_%s.shz.oz.er    z	${{{0}}},,z-hold_jid%sz-e z-e {errFile})ZerrFilez-o z-o {outFile})ZoutFilez{jobNm}=$(qsub {outFileOption} {errFileOption} {extraQSubArgs} {dependantIndex} -N {jobNm} {batchscript} | awk '/^Your job/{{print $3}}')
)ZjobNmZoutFileOptionZerrFileOptionZextraQSubArgsZdependantIndexbatchscriptbashZ	allatonce)environZresource_monitorZterminal_outputz%szsubmitted all jobs to queue)r   r   splitjoindictr   	enumerater   r   
writelinesget	_get_argssys
executablecloser#   rstripr   countr   r-   inputsargsrunr   info) r   ZpyfilesZdependenciesZnodesr&   Z	batch_dirr
   ZsubmitjobsfileZcache_doneness_per_nodeidxZpyscriptnodeZnode_status_doneZ	child_idxZchild_status_donefpr   r   namer+   ZbatchscriptfileZbatchscriptoutfileZbatchscripterrfileZbatchfpdepsvaluesZjobidZjobnameZ
stderrFileZ
stdoutFileZ	full_linecmdr   r   r   _submit_graph@   s    








zSGEGraphPlugin._submit_graph)__name__
__module____qualname____doc__r   r   rF   __classcell__r   r   )r   r   r      s   
r   )
rJ   r   r6   Zinterfaces.baser   baser   r   r   r   r   r   r   r   <module>   s   