3
d>                 @   sP  d dl mZ d dlmZ d dlmZ d dl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jZddlmZmZmZ dd	lmZmZmZ ejd
ZejddZejddZejddZejddZ ejddZ!dd Z"dddddddddd d!d"d#d$d%d&gZ#d:d'd(Z$d)Z%d;d*d+Z&d<d-d.Z'd=d0d1Z(d>d2d3Z)d?d6d7Z*G d8d9 d9e+Z,dS )@    )OrderedDict)deepcopy)dumpsN)uuid1   )get_infologging__version__   )md5hashlibhash_infileznipype.utilsfoafzhttp://xmlns.com/foaf/0.1/dctermszhttp://purl.org/dc/terms/Znipypezhttp://nipy.org/nipype/terms/niirizhttp://iri.nidash.org/cryptozEhttp://id.loc.gov/vocabulary/preservation/cryptographicHashFunctions/c               C   s   t t j S )N)r   r   hex r   r   9/tmp/pip-build-7vycvbft/nipype/nipype/utils/provenance.py<lambda>   s    r   PATHZFSLDIRZFREESURFER_HOMEZANTSPATHZ
CAMINOPATHZ	CLASSPATHLD_LIBRARY_PATHZDYLD_LIBRARY_PATHZFIX_VERTEX_AREAZFSF_OUTPUT_FORMATZ
FSLCONFDIRZFSLOUTPUTTYPELOGNAMEUSERZMKL_NUM_THREADSZOMP_NUM_THREADSc             C   s   t | |d\}}t| S )N)skip)get_hashvalr   )attrr   dictwithhashZhashvalr   r   r   get_attr_id1   s    r   i  c       	      C   s   i }t  }i }x(| D ] }|dk	r*||kr*q|||j< qW xt|D ]}| ||  }|}yt|tjrl|j }W n tk
r   Y nX t|tjr|j}t|tj	r|j
}t|||< t|d||< tt|j }qBW |t|j j fS )ao  Return a dictionary of our items with hashes for each file.

    Searches through dictionary items and if an item is a file, it
    calculates the md5 hash of the file contents and stores the
    file name and hash value as the new key value.

    However, the overall bunch hash is calculated only on the hash
    value of a file. The path and name of the file are not used in
    the overall hash calculation.

    Returns
    -------
    dict_withhash : dict
        Copy of our dictionary with the new file hashes included
        with each file.
    hashvalue : str
        The md5 hash value of the traited spec

    NT)r   urisorted
isinstancepmURIRefdecodeAttributeErrorZQualifiedNameLiteralvalue_get_sorteddictstritemsr   encode	hexdigest)	Z	inputdictr   Zdict_withhashZdict_nofilenamekeyskeyvaloutnameZsorted_dictr   r   r   r   9   s.    
r   Fc             C   s   t | tr>t }xt| j D ]\}}|rt||||< qW nt | ttfrg }x | D ]}|rV|jt|| qVW t | trt|}nLt | t	rt
jj| rt| }|r| |f}q|}nt | trdj| }n| }|S )Nz%.10f)r!   dictr   r    r*   r(   listtupleappendr)   ospathisfiler   floatformat)objectr   outr.   r/   hashr   r   r   r(   g   s*    





r(   Tc          E   C   s  | dkr&d}|r"t j|t jd S |S t| ttfrt| trJt| d} tjj| r| d tj	krptjj
| } djtj j | }|s|S y
t j|S  tk
r   t j|t jd S X nF| }t| tkrd}| dtt|  | }|s|S t j|t jd S t| tr.|s| S t jt| t jd	 S t| trV|sD| S t j| t jd
 S t| tri }xJt| j D ]:\}}t|dd}t|t jr|j ||< n|||< qtW ytj|}W n: tk
r } zdj|}tjd| W Y dd}~X nX |s|S t j|t jd S t| ttfr&t| } d}	yt j!| j"}
|
t j"t#k}	W n t$k
rl   d}	Y nX |	rg }xB| D ]:}t|dd}t|t jr|j%|j  n
|j%| q~W |} ytj| }W n: tk
r } zdj|}tjd| W Y dd}~X nX |s|S t j|t jd S t| t jrJ|r>| S t| j S d}t jd }ytj| j&}W n tk
r~   Y nX |dkryt| }t'd }W n. t(k
r } zdj|}W Y dd}~X nX |s|S t j||S )z)
    Encodes a python value for prov
    NUnknownstringzutf-8r   zfile://{}{}anyURIz...Clipped...integerr8   F)
as_literalzCould not encode dictionary. {}zProv: %sTzCould not encode list/tuple. {}picklezCould not encode object. {}))r"   r&   XSDr!   r)   bytesr5   r6   existspathsepabspathr9   platformnodelowerr#   r%   lenmax_text_lenintr8   r1   r2   r*   safe_encodeZjson_representationjsonr   UnicodeDecodeErrorloggerwarningr3   nparrayZdtyper:   
ValueErrorr4   __dict__	nipype_ns	TypeError)xrA   r'   ZcliptxtZoutdictr.   Zencoded_valueZjsonstrZexcpZ	is_objectZnptypeZoutlistZltyper   r   r   rN      s    








rN   c             C   s  t |ttfo|r
t|}t|dkrRt|}tjd |i}t|}| j||S t|dkrlt	| |d S g }xh|D ]`}t	| |}|j
| t |ttfrqvt|jd }	t |	t}
|
 s|
rvd|	krvt	| |ddS qvW t }| j|d}x|D ]}| j|| qW |S t|}tjd |i}t |trtjj|r|jtjd |i tjj|st|tjd	}|jtd
 tj|tjd i t|tjd tjd gd}nt|tjd gd}nt|}| j||}|S )Nr   r'   r
   zfile://F)create_container)
identifierlocation)r   sha512r>   )r   )r!   r2   r3   rK   rN   r"   PROVr   entityprov_encoder4   r'   r)   get_id
collection	hadMemberr5   r6   rE   updateisdirr   r   r]   r   r&   rC   )graphr'   rZ   Zencoded_literalr   ZeidentitiesitemZitem_entityZitem_entity_valZis_strr_   r]   r   r   r   r`      sH    




 r`   
provenanceallc             C   s   d }y"t  }|j|  |j||d}W nf tk
r } zJdd l}|j }t|drp|ddjdd |jD  7 }t	j
d| W Y d d }~X nX |S )	N)filenamer9   r   argsz

Exception arguments:
z, c             S   s   g | ]}d | qS )z"%s"r   ).0argr   r   r   
<listcomp>2  s    z$write_provenance.<locals>.<listcomp>z1Writing provenance failed - Exception details:
%s)	ProvStoreadd_resultswrite_provenance	Exception	traceback
format_excgetattrjoinrl   rQ   rR   )resultsrk   r9   ZprovZpsert   err_msgr   r   r   rr   &  s    

rr   c               @   s(   e Zd Zdd ZdddZddd	Zd
S )rp   c             C   s>   t j | _| jjt | jjt | jjt | jjt d S )N)r"   ZProvDocumentgZadd_namespacer   r   rW   r   )selfr   r   r   __init__:  s
    
zProvStore.__init__Fc             C   s:  |r|j rt|j | _| jS |j}|j}|j}|j}|j}dj|j	|j}dj
dd |jdD }	td |j	td |tjd t|	 tjd	 |td
 t|jtd t|jtd t|jtd t|jtd t|ji	}
tj|jtjd |
td < yL|
jtd t|ji |
jtd t|ji |
jtd t|ji W n tk
rV   Y nX | jjt |j |j!|
}t }| jj"|}|j#tjd td tjd	 di | jj$|| xzt%t&|j'j( D ]d\}\}}|t)krqtjd	 |td |tjd t|i}t*|}| jj+|| | jj,|| qW |rt }| jj"|}|j#tjd td tjd	 di xft%t&|j( D ]R\}\}}t-| j|j.}| jj,|| tjd	 |td |i}| jj$|||d qzW |rt }| jj"|}t/|t0s|j1 }|j#tjd td tjd	 di | jj2|| xft%t&|j( D ]R\}\}}t-| j|j.}| jj,|| tjd	 |td |i}| jj3|||d q>W t }| jj"|}|j#tjd td tjd	 di | jj2|| xpt&|j( D ]`\}}|sq|d(krqtjd	 |t| t|i}t }| jj+t | | jj,|| qW tjd tjd" tjd	 t4j5 td# tt4j5 i}| jj6t*||}tjd tjd$ tjd	 d%td# td%td t7i}x2t8t9 j( D ] \}}|jt| t|i qW | jj6t*||}| jj:||d d tjd& td' i | jj:|| | jS ))Nz{0}.{1} c             S   s   g | ]}|j  qS r   )
capitalize)rm   ir   r   r   ro   K  s    z)ProvStore.add_results.<locals>.<listcomp>.module	interfacetypelabeldurationZworkingDirectoryZ
returnCoderH   versionr?   hostcommandZcommandPathdependenciesEnvironmentZenvironmentVariabler'   ZInputsZinPort)activityr_   other_attributesZOutputsZoutPort)r   r   ZRuntimeZRuntimeInfostdoutstderrmergedZPersonnameZSoftwareAgentZNipypeZhadRoleZLoggedInUser)r   r   r   );ri   r   r{   runtimer   inputsoutputs__name__r9   
__module__rw   splitrW   r"   r^   rN   r   cwd
returncoderH   r   r&   hostnamerC   r   rd   ZcmdlineZcommand_pathr   r%   r   ra   Z	startTimeZendTimerb   Zadd_attributesused	enumerater    environr*   PROV_ENVVARSr   r_   rc   r`   r[   r!   r1   Zget_traitsfreeZwasGeneratedByZ
generationgetpassgetuserZagentr	   r2   r   ZwasAssociatedWith)r|   rx   Zkeep_provenancer   r   r   r   	classnameZ
modulepathZactivitytypeZa0_attrsZa0idZenv_collectionidxr.   r/   Zin_attrZinput_collectionZ	in_entityZ	used_attrZoutput_collectionZ
out_entityZgen_attrZruntime_collectionr'   r   Z	user_attr
user_agentZ
agent_attrZsoftware_agentr   r   r   rq   A  s    


 



zProvStore.add_resultsri   rj   c             C   s   |dkr2t |d d}|j| jj  W d Q R X yd|dkrtt| jjdkrVd}d}nd	}d
}| jj|| d|d |dkr| jj|d dddd W n tjj	k
r   Y nX | jS )Nprovnrj   z.provnwtrdfr   Zturtlez.ttlZtrigz.trig)r9   
rdf_formatjsonldz.jsonldzjson-ld   )r9   r   indent)r   rj   )r   rj   )r   )
open
writelinesr{   Z	get_provnrK   Zbundles	serializer"   ZserializersZ
DoNotExist)r|   rk   r9   fpr   extr   r   r   rr     s"    zProvStore.write_provenanceN)F)ri   rj   )r   r   __qualname__r}   rq   rr   r   r   r   r   rp   9  s   
{rp   )N)N)F)T)T)ri   rj   )-collectionsr   copyr   rB   r   r5   r   rH   uuidr   Z
simplejsonrO   numpyrS   Z
prov.modelmodelr"   r~   r   r   r	   Z	filemanipr   r   r   	getLoggerrQ   Z	Namespacer   r   rW   r   r   ra   r   r   rL   r   r(   rN   r`   rr   r:   rp   r   r   r   r   <module>   sX   



.

u
/
