o
    :ήcY                     @   s  d Z ddlZddlZddlmZ ddlmZ ddlZddlm	Z	m
Z
mZmZmZ ddlmZmZmZ ddlmZmZ dd	lmZ dd
lmZ ddlmZmZmZmZmZmZmZmZ e ee!j"d Z#e Z$e Z%e Z&e Z'ddiZ(ej)dd Z*ej+j,dd Z-ej+j,dd Z.ej+j,dd Z/dd Z0ej+j,ej+j1de%e&e'gg dddd Z2ej+j,ej+j1de%e&e'gg dddd  Z3G d!d" d"Z4ej+j,d#d$ Z5G d%d& d&Z6ej+j,d'd( Z7ej+j,d)d* Z8ej+j,d+d, Z9ej+j,d-d. Z:d/d0 Z;d1d2 Z<ej+j,d3d4 Z=d5d6 Z>d7d8 Z?d9d: Z@d;d< ZAd=d> ZBd?d@ ZCej+j,dAdB ZDej+j,dCdD ZEej+j,dEdF ZFdGdH ZGdIdJ ZHdKdL ZIej+j,ej+1dMddNgdOdP ZJdS )Qz+
Test the core class and factory function.
    N)Path)TemporaryDirectory   )createPoochretrievedownload_actionstream_download)
get_loggertemporary_fileos_cache)	file_hashhash_matches)core)HTTPDownloader   )pooch_test_urlpooch_test_figshare_urlpooch_test_zenodo_urlpooch_test_registrycheck_tiny_datacheck_large_datacapture_logmirror_directorydatatiny-data.txt@098h0894dba14b12085eacb204284b97e362f4f3e5a5807693cc90ef415c1b2dc                 C   s
   t t| S )z
    Mirror the test data folder on a temporary directory. Needed to avoid
    permission errors when pooch is installed on a non-writable path.
    )r   DATA_DIR)tmp_path r   </tmp/pip-target-vg8gfxp4/lib/python/pooch/tests/test_core.pydata_dir_mirror0   s   
r!   c               	   C   sv  t  } d}t| }t +}t|d| d}| }| d dks#J d|v s)J t| |v s1J W d   n1 s;w   Y  ||t| d ksMJ t| t	|t| ks[J t }t|d| d}| dksnJ W d   n1 sxw   Y  t }t|t| | d}| dksJ W d   n1 sw   Y  W d   dS W d   dS 1 sw   Y  dS )z'Try downloading some data with retriever   N)
known_hashpathr   DownloadingSHA256 hash of downloaded file: )
r   BASEURLr   r   getvaluesplitREGISTRYlenr   r   local_store	data_fileurllog_filefnamelogsr   r   r    test_retrieve9   s0   "r3   c               	   C   s   t  `} d}t| }t ,}t|d| |d}| }| d dks$J d|v s*J t| |v s2J W d   n1 s<w   Y  |tj|d ksMJ t	| t
|t| ks[J W d   dS 1 sfw   Y  dS )zATry downloading some data with retrieve and setting the file namer   N)r"   r#   r1   r   r$   r%   r   )r   r'   r   r   r(   r)   r*   osr#   r   r   r,   r   r   r    test_retrieve_fnameS   s   "r5   c               
   C   s  d} t |  }td|  }znt 7}t|d| d}| }| d dks&J ttd |v s2J d|v s8J t|  |v s@J W d   n1 sJw   Y  |t| ksYJ t	| t
|t|  ksgJ W tjt|rytt| dS dS tjt|rtt| w w )zETry downloading some data with retrieve to the default cache locationr   poochN)r"   r1   r   r$   zSHA256 hash of downloaded file)r'   r   r   r   r(   r)   strresolver*   r   r   r4   r#   existsremove)r.   r/   expected_locationr0   r1   r2   r   r   r    test_retrieve_default_pathf   s(   r<   c                 C   s<   t | dtd}t| d }|d}||ksJ t| dS )zASetup a pooch that already has the local data and test the fetch.some bogus URLr#   base_urlregistryr   N)r   r*   r7   fetchr   )r!   puptruer1   r   r   r    test_pooch_local~   s
   
rD   r/   )httpsfigsharezenodo)idsc              	   C   s  t  {}t|}d| d i}t|dt|d}t )}|d}| }| d dks-J | d d| dks;J W d	   n1 sEw   Y  t| t }|d}| dks_J W d	   n1 siw   Y  W d	   d	S W d	   d	S 1 sw   Y  d	S )
z:Have pooch download the file from URL that is not base_urlr   r&   r#   r?   r@   urlsr   r$   ''.N)	r   r   r   r*   r   rA   r(   r)   r   )r/   r-   r#   rJ   rB   r0   r1   r2   r   r   r    test_pooch_custom_url   s$   

"rN   c              	   C   s.  t  }t|}t|d }t|| td}t )}|d}| }| d dks,J | d d| dks:J W d   n1 sDw   Y  ||ksOJ t	| t
|td ks]J t }|d}| d	ksnJ W d   n1 sxw   Y  W d   dS W d   dS 1 sw   Y  dS )
z:Setup a pooch that has no local data and needs to downloadr   r>   r   r$   rK   rL   rM   Nr&   )r   r   r7   r   r*   r   rA   r(   r)   r   r   )r/   r-   r#   	true_pathrB   r0   r1   r2   r   r   r    test_pooch_download   s(   

"rP   c                   @       e Zd ZdZdd Zdd ZdS )FakeHashMatchesz8Create a fake version of hash_matches that fails n timesc                 C   s   || _ d| _d S )Nr   )	nfailuresfailed)selfrS   r   r   r    __init__   s   
zFakeHashMatches.__init__c                 O   s>   | j | jk r|  j d7  _ t|d dfi |S t|i |S )z#Fail n times before finally passingr   r   bla)rT   rS   r   )rU   argskwargsr   r   r    r      s   zFakeHashMatches.hash_matchesN)__name__
__module____qualname____doc__rV   r   r   r   r   r    rR      s    rR   c              
   C   s  t  z}| tdtdj t|}t|ttd}t	
t }t }|d W d   n1 s2w   Y  W d   n1 sAw   Y  dt|v sNJ |  d}t|dks_J |d	 d
shJ |d	 d| dsuJ W d   dS 1 sw   Y  dS )z2Check that retrying the download is off by defaultr      r>   r   Ndoes not match the known hash
r   r   r$   rL   rM   )r   setattrr   rR   r   r   r   r'   r*   pytestraises
ValueErrorr   rA   r7   r(   stripr)   r+   
startswithendswithmonkeypatchr-   r#   rB   errorr0   r2   r   r   r    (test_pooch_download_retry_off_by_default   s    "rk   c                   @   rQ   )	FakeSleepz<Create a fake version of sleep that logs the specified timesc                 C   s
   g | _ d S )N)times)rU   r   r   r    rV      s   
zFakeSleep.__init__c                 C   s   | j | dS )z Store the time and doesn't sleepN)rm   append)rU   secsr   r   r    sleep   s   zFakeSleep.sleepN)rZ   r[   r\   r]   rV   rp   r   r   r   r    rl      s    rl   c              	   C   s  t  }| tdtdj t }| tjd|j t|}t	|d }d}t
|tt|d}t l}|d}|  d}	t|	d| ksJJ |	d d	sSJ |	d d
| ds`J tt|dd|	dd D ]\}
}d|v swJ |
dkr}dnd}d|
 d| d|v sJ qm|jg dksJ W d   n1 sw   Y  ||ksJ t| t|td ksJ W d   dS 1 sw   Y  dS )z?Check that retrying the download works if the hash is differentr      rp   r   r#   r?   r@   retry_if_failedr`   r   r   r$   rL   rM   rK   NFailed to downloadsr&   zdownload again z
 more time.)r   r   r^                  	   
   r}   )r   ra   r   rR   r   rl   timerp   r   r7   r   r'   r*   r   rA   r(   re   r)   r+   rf   rg   ziprangerm   r   r   )ri   r-   	fakesleepr#   rO   retriesrB   r0   r1   r2   ilinepluralr   r   r    test_pooch_download_retry   s4   
""r   c              
   C   s0  t  }| tdtdj t|}t|ttdd}t	
t }t }|d W d   n1 s3w   Y  W d   n1 sBw   Y  |  d}t|dksXJ |d	 d
saJ |d	 d| dsnJ d|d v svJ d|d v s~J dt|v sJ W d   dS 1 sw   Y  dS )zFCheck that retrying the download fails after the set amount of retriesr   r^   r   rr   r   Nr`   r   r   r$   rL   rM   rt   zdownload again 1 more time.r_   )r   ra   r   rR   r   r   r   r'   r*   rb   rc   rd   r   rA   r(   re   r)   r+   rf   rg   r7   rh   r   r   r    *test_pooch_download_retry_fails_eventually
  s$   "r   c               	   C   s   t  @} t| }dtd i}t|dt|d}td}|d}| dks(J W d   n1 s2w   Y  t| W d   dS 1 sFw   Y  dS )zHSetup a pooch and check that no logging happens when the level is raisedr   r&   rI   CRITICALN)	r   r   r'   r   r*   r   rA   r(   r   )r-   r#   rJ   rB   r0   r1   r   r   r    test_pooch_logging_level   s   


"r   c               	   C   sb  t  } t| }t|d }t|d}|d W d   n1 s#w   Y  t|ttd}t )}|	d}|
 }| d dksFJ | d d	| d
ksTJ W d   n1 s^w   Y  ||ksiJ t| t|td kswJ t }|	d}|
 dksJ W d   n1 sw   Y  W d   dS W d   dS 1 sw   Y  dS )zFSetup a pooch that already has the local data but the file is outdatedr   wdifferent dataNr>   r   UpdatingrK   rL   rM   r&   )r   r   r7   openwriter   r'   r*   r   rA   r(   r)   r   r   )r-   r#   rO   finrB   r0   r1   r2   r   r   r    test_pooch_update/  s.   

"r   c               	   C   s   t  W} t| }t|d }t|d}|d W d   n1 s#w   Y  t|ttdd}t	t
 |d W d   n1 sEw   Y  W d   dS W d   dS 1 s]w   Y  dS )z$Test that disallowing updates works.r   r   r   NFr#   r?   r@   allow_updates)r   r   r7   r   r   r   r'   r*   rb   rc   rd   rA   )r-   r#   rO   r   rB   r   r   r    test_pooch_update_disallowedK  s$   "r   c               	   C   s   d} zpdt j| < t N}t|}t|d }t|d}|d W d   n1 s+w   Y  t|tt	| d}t
t |d W d   n1 sMw   Y  W d   n1 s\w   Y  W t j|  dS W t j|  dS t j|  w )zDTest that disallowing updates works through an environment variable.MYPROJECT_DISALLOW_UPDATESFalser   r   r   Nr   )r4   environr   r   r7   r   r   r   r'   r*   rb   rc   rd   rA   pop)variable_namer-   r#   rO   r   rB   r   r   r    (test_pooch_update_disallowed_environment_  s.   
r   c              
   C   s  t  g}tj|}t|ttd}t G}t	t
}|d W d   n1 s*w   Y  dt|jv s8J | }| d dksFJ | d d| d	ksTJ W d   n1 s^w   Y  W d   n1 smw   Y  t| ttd}t H}t	t
}|d W d   n1 sw   Y  dt|jv sJ | }| d d
ksJ | d d|  d	ksJ W d   dS 1 sw   Y  dS )z>Raise an exception if the file hash doesn't match the registryr>   r   Nz(tiny-data.txt)r   r$   rK   rL   rM   r   )r   r4   r#   abspathr   r'   REGISTRY_CORRUPTEDr   rb   rc   rd   rA   r7   valuer(   r)   )r!   r-   r#   rB   r0   rj   r2   r   r   r    test_pooch_corruptedx  s0   "r   c                  C   sH   t ddtd} tt | d W d   dS 1 sw   Y  dS )z=Should raise an exception if the file is not in the registry.zit shouldn't matterzthis shouldn't eitherr>   zthis-file-does-not-exit.csvN)r   r*   rb   rc   rd   rA   rB   r   r   r    test_pooch_file_not_in_registry  s   "r   c                  C   L   t ddd} | tjtd | jtksJ | j	 t
t	 ks$J dS )z,Loading the registry from a file should workr&   r#   r?   registry.txtNr   load_registryr4   r#   joinr   r@   r*   registry_filessortlistr   r   r   r    test_pooch_load_registry     r   c                  C   r   )z8Loading the registry from a file and strip line commentsr&   r   zregistry_comments.txtNr   r   r   r   r    !test_pooch_load_registry_comments  r   r   c                  C   s   t jtd} tddd}t| d}|| W d   n1 s"w   Y  |jtks.J |j	
 tt
 ks;J tddd}t| }|| W d   n1 sUw   Y  |jtksaJ |j	
 tt
 ksnJ dS )z'Loading the registry from a file objectr   r&   r   rbN)r4   r#   r   r   r   r   r   r@   r*   r   r   r   )r#   rB   r   r   r   r     test_pooch_load_registry_fileobj  s   
r   c                  C   sD   t ddd} | tjtd | jtksJ | jddiks J dS )z8Load the registry from a file with a custom URL insertedr&   r   zregistry-custom-url.txtr   zhttps://some-site/tiny-data.txtN)	r   r   r4   r#   r   r   r@   r*   rJ   r   r   r   r    #test_pooch_load_registry_custom_url  s   r   c                  C   sR   t ddi d} tt | tjtd W d   dS 1 s"w   Y  dS )z?Should raise an exception when a line doesn't have two elementsr&   r>   zregistry-invalid.txtN)	r   rb   rc   IOErrorr   r4   r#   r   r   r   r   r   r    %test_pooch_load_registry_invalid_line  s   "r   c                  C   sn   t tttd} | dsJ t ttd td} | drJ ddi}|t t tt|d} | dr5J dS )FShould correctly check availability of existing and non existing filesr>   r   z
wrong-url/znot-a-real-data-file.txtnotarealhashN)r   r   r'   r*   is_availableupdate)rB   r@   r   r   r    test_check_availability  s   
r   c                  C   s4   t tddddd} | dsJ | drJ dS )	r   z;ftp://data-out.unavco.org/pub/products/velocity/rel_201712/z$md5:0b75d4049dedd0e179615f4b5e956156jdjdjdjdflld)pbo.final_igs08.20171202.veldoesnot_exist.zipr>   r   r   N)r   r   r   r   r   r   r    test_check_availability_on_ftp  s   r   c           	   	   C   s,  dd }t  }t|}t|ttd}t 3}|jd|d}| }| }t	|dks.J |d 
 d dks:J |d	 d
ksBJ W d   n1 sLw   Y  |  jrXJ t| t }|d}| dksmJ W d   n1 sww   Y  W d   dS W d   dS 1 sw   Y  dS )z%Setup a downloader function for fetchc                 S   s   t  d t | || dS )z2Download through HTTP and warn that we're doing itdownloader executedN)r
   infor   )r/   output_filerB   r   r   r    download  s   z,test_fetch_with_downloader.<locals>.downloadr>   zlarge-data.txt)
downloaderr   r   r$   r   r   Nr&   )r   r   r   r'   r*   r   rA   r(   
splitlinesr+   r)   
readouterrerrr   )	capsysr   r-   r#   rB   r0   r1   r2   linesr   r   r    test_fetch_with_downloader  s*   
"r   c                 C   s\   t | tddid}tt}|d W d   n1 sw   Y  dt|jv s,J dS )z!Test an invalid hashing algorithmr   z	blah:1234r>   Nz'blah')r   r'   rb   rc   rd   rA   r7   r   )r!   rB   excr   r   r    test_invalid_hash_alg  s   
r   c                 C   s   t | d }t| t|d}| }W d   n1 sw   Y  dD ]+}t|}|| d| d|  i}t| d|d}||	dksLJ t| q%dS )z2Test different hashing algorithms using local datar   r   N)sha512md5:r=   r>   )
r7   r   r   readhashlibnewr   	hexdigestr   rA   )r!   r1   r   r   alghasherr@   rB   r   r   r    #test_alternative_hashing_algorithms  s   



r   c               	   C   s  t tddd\} }| dksJ |dksJ t }t t|dd\} }W d   n1 s.w   Y  | dks9J |dks?J t .}t|d	}|d
 W d   n1 sXw   Y  t t|t|d\} }W d   n1 ssw   Y  | dks~J |dksJ dS )z>Test that the right action is performed based on file existingzthis_file_does_not_exist.txtN)r"   r   r$   znot the correct hashr   r   r   z	some datarA   Fetching)r   r   r   r   r   r   )actionverbtmpoutputr   r   r    test_download_action&  s$   
r   r1   zsubdir/tiny-data.txtc                 C   s   t d |  }t|  }t }t *}t||  }| rJ t||||dd | s,J tt| W d   dS 1 s=w   Y  dS )z9Check that downloading a file over HTTP works as expectedzstore/N)r6   )	r'   r*   r   r   r   r9   r	   r   r7   )r1   r/   r"   r   r-   destinationr   r   r    test_stream_download;  s   "r   )Kr]   r   r4   pathlibr   tempfiler   rb   r   r   r   r   r   r	   utilsr
   r   r   hashesr   r   r&   downloadersr   r   r   r   r   r   r   r   r   r7   __file__parentr   r*   r'   FIGSHAREURL	ZENODOURLr   fixturer!   marknetworkr3   r5   r<   rD   parametrizerN   rP   rR   rk   rl   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    <module>   s   (



	

 



	


