3
d                 @   s`   d 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m	Z	 ddl
mZ G dd	 d	eZdS )
z.Parallel workflow execution via Condor DAGMan
    N)warn   )GraphPluginBaselogger   )CommandLinec                   s4   e Zd ZdZdZdd Z fddZdd Z  ZS )	CondorDAGManPluginaz  Execute using Condor DAGMan

    The plugin_args input to run can be used to control the DAGMan execution.
    The value of most arguments can be a literal string or a filename, where in
    the latter case the content of the file will be used as the argument value.

    Currently supported options are:

    - submit_template : submit spec template for individual jobs in a DAG (see
                 CondorDAGManPlugin.default_submit_template for the default.
    - initial_specs : additional submit specs that are prepended to any job's
                 submit file
    - override_specs : additional submit specs that are appended to any job's
                 submit file
    - wrapper_cmd : path to an executable that will be started instead of a node
                 script. This is useful for wrapper script that execute certain
                 functionality prior or after a node runs. If this option is
                 given the wrapper command is called with the respective Python
                 executable and the path to the node script as final arguments
    - wrapper_args : optional additional arguments to a wrapper command
    - dagman_args : arguments to be prepended to the arguments of the
                    condor_submit_dag call
    - block : if True the plugin call will block until Condor has finished
                 processing the entire workflow (default: False)
    z
universe = vanilla
notification = Never
executable = %(executable)s
arguments = %(nodescript)s
output = %(basename)s.out
error = %(basename)s.err
log = %(basename)s.log
getenv = True
c          	   C   s2   t jj|r*t|}|j }W d Q R X n|}|S )N)ospathisfileopenread)selfargfcontent r   @/tmp/pip-build-7vycvbft/nipype/nipype/pipeline/plugins/dagman.py_get_str_or_file4   s
    
z#CondorDAGManPlugin._get_str_or_filec                s   xdd| j fddddddddf	D ]|\}}}d|kr|d d k	r||d kr|dkrhtjj|d | }n(|dkr~|d | }n| j|d | }t| || q W d|ko|d d k	 r|d }d|k rtd d|k rtd tt| j	f | d S )N	_templateZsubmit_template_initial_specstemplate initial_specs_override_specssubmit_specsoverride_specs_wrapper_cmdwrapper_cmd_wrapper_argswrapper_args_blockblockF_dagman_argsdagman_argsplugin_argszBthe 'template' argument is deprecated, use 'initial_specs' insteadzGthe 'submit_specs' argument is deprecated, use 'override_specs' instead)r   r   r   )r   r   r   )r   r   r   )r   r   r   )r   r   N)r   r    r   )r!   r"   F)r#   r$   r   )
default_submit_templater	   r
   abspathr   setattrr   superr   __init__)r   kwargsvarZid_valr%   )	__class__r   r   r*   C   s8    


zCondorDAGManPlugin.__init__c             C   s4  t jj|d \}}t jj|dtj  }t|d\}xt|D ] \}}	|| }
| j|
dddddg\}}}}}d	d
|df }t jj|	\}}dj|jdd d$ }t	|t
j|	t jj|||d}|d k	r||d< d|| t
j|	f |d< || }t jj|d| }t|d}|j| |j  W d Q R X |jd||f  qDW xB|D ]:}|| }t|rP|jddjdd |D |f  qPW W d Q R X tdt	t jddd}d| j|f |j_|j  tjd | jr0tjd t jjd| std tjd! d"| }xt jj|r.tjd# qW d S )%Nr   zworkflow-%s.dagwtr   r   r   r   r    z%s
%s
%s
queue
z%(initial_specs)sz%(override_specs)s.r   )r   
executable
nodescriptbasenamer   r1   z%s %s %sr2   z	%s.submitz
JOB %i %s
zPARENT %s CHILD %i
 c             S   s   g | ]}t |qS r   )str).0ir   r   r   
<listcomp>   s    z4CondorDAGManPlugin._submit_graph.<locals>.<listcomp>Zcondor_submit_dagFZ	allatonce)environZresource_monitorZterminal_outputz%s -update_submit %sz#submitted all jobs to Condor DAGMan
   z%s.condor.subz<DAGMan did not create its submit file, please check the logszwaiting for DAGMan to finishz%s.lock   )r	   r
   splitjoinuuiduuid4r   	enumerate	_get_argsdictsysr1   
writelinesclosewritelenr   r9   r#   inputsargsrunr   infor!   timesleepexistsEnvironmentError)r   ZpyfilesZdependenciesZnodesZ	batch_dir_ZdagfilenameZ
dagfileptridxZpyscriptnoder   r   r   r   r    namespecsZ
submitspecZ
submitfileZsubmitfileprtchildparentscmdZlockfilenamer   r   r   _submit_graphh   sv    	


,


z CondorDAGManPlugin._submit_graph)	__name__
__module____qualname____doc__r&   r   r*   rY   __classcell__r   r   )r.   r   r      s
   %r   )r]   r	   rD   r?   rM   warningsr   baser   r   Zinterfaces.baser   r   r   r   r   r   <module>   s   