3
dx                 @   s^   d Z ddlZddlZddljZddlmZ ddlmZ ddl	m
Z
 dd Zdd	 Zd
d ZdS )z=Utilities to manipulate and search through .pklz crash files.    N)glob)
TraitError)	loadcrashc             C   sP   yt | }W n0 tk
r0 } zt|S d}~X n    Y nX dj|d S dS )z5Return the traceback message in the given crash file.N
	traceback)r   r   strjoin)Zcrash_filepathdatate r   </tmp/pip-build-7vycvbft/nipype/nipype/scripts/crash_files.pyload_pklz_traceback   s    r   c             c   s4   t ttj| d}x|D ]}|t|fV  qW dS )a#  Return an iterator over each file path and
    traceback field inside `logdir`.
    Parameters
    ----------
    logdir: str
        Path to the log folder.

    field: str
        Field name to be read from the crash file.

    Yields
    ------
    path_file: str

    traceback: str
    z*.pkl*N)sortedr   opr   r   )ZlogdirZcrash_filesZcfr   r   r   iter_tracebacks   s    
r   c       	   	   C   s4  ddl m} || }d}d|kr(|d }|d }td td|   |rtd|  |jrltd	|j   ntd
 td td t|j td td tdj| td |r0|dkrtd dS td ||_dddii|_y|j  W n0   |r |dkr ddl	}|j
  n Y nX td dS )z0display crash file content and rerun if requiredr   )r   Nnoder   r   zFile: %szNode: %szWorking directory: %szNode crashed before executionzNode inputs:zTraceback:  z"No node in crashfile. Cannot rerunzRerunning nodeZ	executionZcrashdump_dirz/tmpZipython)nipype.utils.filemanipr   printbase_dir
output_dirinputsr   configrunpdbZpost_mortem)	Z	crashfileZrerundebug	directoryr   Z
crash_datar   tbr   r   r   r   display_crash_file/   sF    

r   )__doc__resysos.pathpathr   r   Ztraits.trait_errorsr   r   r   r   r   r   r   r   r   r   <module>   s   
