3
d5                 @   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 SLURM
    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   D/tmp/pip-build-7vycvbft/nipype/nipype/pipeline/plugins/slurmgraph.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 )SLURMGraphPlugina0  Execute using SLURM

    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/bashc                s   d| _ d|kr|d rd|d kr0|d d | _d|d krJ|d d | _d|d kr|d d | _tjj| jrt| jj | _d|d kr|d d | _ d|d kr|d d | _	nd| _	t
t| jf | d S )	N Zplugin_argsZretry_timeoutZ	max_triestemplatesbatch_argsZdont_resubmit_completed_jobsF)_sbatch_argsZ_retry_timeoutZ
_max_tries	_templateospathisfileopenread_dont_resubmit_completed_jobssuperr   __init__)selfkwargs)	__class__r   r   r   +   s"    
zSLURMGraphPlugin.__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 SLURM
            zj{0}_{1}-r
   .:)format_idreplace)Z	jobnumberZ	nodeslistZjob_namer   r   r   make_job_nameA   s    z5SLURMGraphPlugin._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}}}:r!   z--dependency=afterok:%sz-e z-e {errFile})ZerrFilez-o z-o {outFile})ZoutFilez{jobNm}=$(sbatch {outFileOption} {errFileOption} {extraSBatchArgs} {dependantIndex} -J {jobNm} {batchscript} | awk '/^Submitted/ {{print $4}}')
)ZjobNmZoutFileOptionZerrFileOptionZextraSBatchArgsZ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SLURMGraphPlugin._submit_graph)__name__
__module____qualname____doc__r   r   rC   __classcell__r   r   )r   r   r      s   
r   )
rG   r   r3   Zinterfaces.baser   baser   r   r   r   r   r   r   r   <module>   s   