B
    '\A'                 @   s  d dl mZmZmZmZ d dlmZ d dlmZ d dl	m
Z
 d dlmZ d dlmZ d dlZd dlmZ d dlZd d	lmZ d d
lmZ d dlmZmZmZ d dlmZ d dlZddlmZmZ ddl m!Z! ddl"m#Z# ddl$m%Z% ddl&m'Z'm(Z( ddl)m*Z*m+Z+m,Z,m-Z- ddl.m/Z/m0Z0m1Z1 ddl2m3Z3 ddl4m5Z5m6Z6m7Z7 ddl8m9Z9m:Z: ddl;m<Z<m=Z=m>Z>m?Z? ee@ZAeZeee  ZZZdd ZBdd ZCdd ZDd d! ZEd"d# ZFd$d% ZGd&d' ZHd(d) ZId*d+ ZJd,d- ZKd.d/ ZLd0d1 ZMd2d3 ZNd4d5 ZOd6d7 ZPd8d9 ZQdS ):    )absolute_importdivisionprint_functionunicode_literals)	b64encode)
namedtuple)ENOENT)partial)globN)chain)	getLogger)listdir)isdirisfilejoin)shlex_split_unicode   )islinklexists   )first)dals)PREFIX_PLACEHOLDER)ensure_text_typeopen)PythonDistributionPythonEggInfoDistributionPythonEggLinkDistributionPythonInstalledDistribution)CondaUpgradeErrorCondaVerificationErrorPathNotFoundError)Channel)FileModePackageTypePathType)PackageInfoPackageMetadata)PathData
PathDataV1	PathsDataPrefixRecordc          
   c   s~   yDt | 2}x*|D ]"}| }|r|dr.q|V  qW W dQ R X W n4 ttfk
rx } z|jtkrfn W dd}~X Y nX dS )zGenerator function for lines in file.  Empty generator if path does not exist.

    Args:
        path (str): path to file

    Returns:
        iterator: each line in file, not starting with '#'

    #N)r   strip
startswithIOErrorOSErrorerrnor   )pathfhlinee r6   7lib/python3.7/site-packages/conda/gateways/disk/read.pyyield_lines)   s    



r8   c          	   C   s^   t |st|t| }t|d,}x$tt|jddD ]}|| q:W W d Q R X |	 S )Nrbi        )
r   r!   hashlibnewr   iterr	   readupdateZ	hexdigest)Zalgor2   Zhasherr3   chunkr6   r6   r7   _digest_pathA   s    
rA   c             C   s
   t d| S )NZmd5)rA   )file_full_pathr6   r6   r7   compute_md5sumL   s    rC   c             C   s
   t d| S )Nsha256)rA   )rB   r6   r6   r7   compute_sha256sumP   s    rE   c              G   s0   x*| D ]"}xt |D ]}t|r|S qW qW d S )N)r
   r   )Zglobsgr2   r6   r6   r7   find_first_existingT   s
    
rG   c          	   C   sB   |j }t|}t|}t|}t|t| jp.| j| |j|||dS )N)extracted_package_dirchannelZrepodata_recordurlicondatapackage_metadata
paths_data)	rH   read_icondataread_package_metadataread_paths_jsonr&   r"   ZschannelrI   rJ   )recordZpackage_cache_recordZepdrK   rL   rM   r6   r6   r7   read_package_info`   s    rR   c          	   C   s&   t t| dd}t|S Q R X d S )Ninfoz
index.json)r   r   jsonload)extracted_package_directoryfir6   r6   r7   read_index_jsonr   s    rX   c          	   C   s2   t | }|d }tt|S Q R X d S )Nzinfo/index.json)tarfiler   Zextractfiler>   rT   loadsr   )Zpackage_tarball_full_pathZtfcontentsr6   r6   r7   read_index_json_from_tarballw   s    r\   c          	   C   s&   t t| dd}t|S Q R X d S )NrS   zrepodata_record.json)r   r   rT   rU   )rV   rW   r6   r6   r7   read_repodata_json}   s    r]   c          	   C   sH   t | dd}t|r@t|d}| }W d Q R X t|dS d S d S )NrS   zicon.pngr9   zutf-8)r   r   r   r>   r   decode)rV   Zicon_file_pathfdatar6   r6   r7   rN      s    rN   c          	      sr    fdd}t | td}|s"d S t|d.}t| }|ddkrVttdW d Q R X t	f |}|S d S )Nc               3   s    t  ddV  t  ddV  d S )NrS   z	link.jsonzpackage_metadata.json)r   r6   )rV   r6   r7   _paths   s    z%read_package_metadata.<locals>._paths)keyrZpackage_metadata_versionr   z
                The current version of conda is too old to install this package. (This version
                only supports link.json schema version 1.)  Please update conda to install
                this package.
                )
r   r   r   rT   rZ   r>   getr   r   r'   )rV   ra   r2   r_   r`   rL   r6   )rV   r7   rO      s    
rO   c          	      s   t  dt d}t|rpt|}t|}W d Q R X |ddkrTttdtddd |d D d	}n>t	t d
t
 fdd}t| }td|d	}|S )NrS   z
paths.jsonpaths_versionr   z
            The current version of conda is too old to install this package. (This version
            only supports paths.json schema version 1.)  Please update conda to install
            this package.c             s   s   | ]}t f |V  qd S )N)r)   ).0r_   r6   r6   r7   	<genexpr>   s    z"read_paths_json.<locals>.<genexpr>paths)re   rh   Z
has_prefixc              3   s   t d} xdd dd t| D D D ]x}d|i}| kr`| d |d< | d |d	< |krpd
|d< tt  |rtj|d< n
tj|d< tf |V  q(W d S )Nfilesc             s   s   | ]}|r|V  qd S )Nr6   )rf   Zlnr6   r6   r7   rg      s    z;read_paths_json.<locals>.read_files_file.<locals>.<genexpr>c             s   s   | ]}|  V  qd S )N)r-   )rf   r4   r6   r6   r7   rg      s    _pathr   Zprefix_placeholderr   Z	file_modeTno_link	path_type)r   r8   keysr   r%   Zsoftlinkhardlinkr(   )Z
files_pathr_   Z	path_info)rV   has_prefix_filesinfo_dirrk   r6   r7   read_files_file   s    
"
z(read_paths_json.<locals>.read_files_filer   )r   r   r   rT   rU   rd   r   r   r*   read_has_prefixread_no_linktuple)rV   Zpaths_json_pathZ
paths_jsonr`   rM   rq   rh   r6   )rV   ro   rp   rk   r7   rP      s&    



rP   c                s<   t dd  fddfddtD }dd |D S )	z
    reads `has_prefix` file and return dict mapping filepaths to tuples(placeholder, FileMode)

    A line in `has_prefix` contains one of
      * filepath
      * placeholder mode filepath

    mode values are one of
      * text
      * binary
    ParseResult)placeholderfilemodefilepathc                sp   t dd t| ddD }t|dkr8 ttj|d S t|dkr` |d t|d |d S td	 d S )
Nc             s   s   | ]}| d V  qdS )z"'N)r-   )rf   xr6   r6   r7   rg      s    z6read_has_prefix.<locals>.parse_line.<locals>.<genexpr>F)posixr   r   r      z#Invalid has_prefix file at path: %s)rt   r   lenr   r#   textr    )r4   parts)ru   r2   r6   r7   
parse_line   s    z#read_has_prefix.<locals>.parse_linec             3   s   | ]} |V  qd S )Nr6   )rf   r4   )r   r6   r7   rg      s    z"read_has_prefix.<locals>.<genexpr>c             S   s   i | ]}|j |jf|jqS r6   )rv   rw   rx   )rf   Zprr6   r6   r7   
<dictcomp>   s    z#read_has_prefix.<locals>.<dictcomp>)r   r8   )r2   Zparsed_linesr6   )ru   r   r2   r7   rr      s    

rr   c             C   s"   t ttt| dtt| dS )Nrk   Zno_softlink)setr   r8   r   )rp   r6   r6   r7   rs      s    rs   c                s   t  fdd|D S )Nc             3   s    | ]}t t |r|V  qd S )N)r   r   )rf   r_   )rV   r6   r7   rg      s    z"read_soft_links.<locals>.<genexpr>)rt   )rV   ri   r6   )rV   r7   read_soft_links   s    r   c             C   s4  t | ||}| \}}t|trhtd}d}tj}| }	t	ddd |	D d}
t
dd |	D }nt|trtd}d	}tj}t	dd
dd
 }
}nvt|trtd}d}|jrtj}| }	t
dd |	D }t	ddd |D d}
ntj}t	dd
dd
 }
}nt t||j|j|d|j|d|
|||dS )zz
    Convert a python package defined by an anchor file (Metadata information)
    into a conda prefix record object.
    ZpypiZpypi_0r   c             s   s&   | ]\}}}t |tj||d V  qdS ))rj   rl   rD   Zsize_in_bytesN)r)   r%   rn   )rf   r2   Zchecksumsizer6   r6   r7   rg      s   z%read_python_record.<locals>.<genexpr>)re   rh   c             s   s   | ]}|d  V  qdS )r   Nr6   )rf   pr6   r6   r7   rg     s    z	<develop>Zdev_0r6   c             s   s   | ]}|d  V  qdS )r   Nr6   )rf   r   r6   r6   r7   rg     s    c             s   s   | ]}t |tjd V  qdS ))rj   rl   N)r(   r%   rn   )rf   r2   r6   r6   r7   rg     s    r   )package_typenameversionrI   ZsubdirfnbuildZbuild_numberrM   ri   depends
constrains)r   ZinitZget_conda_dependencies
isinstancer   r"   r$   ZVIRTUAL_PYTHON_WHEELZ	get_pathsr*   rt   r   ZVIRTUAL_PYTHON_EGG_LINKr   Zis_manageableZVIRTUAL_PYTHON_EGG_MANAGEABLEZVIRTUAL_PYTHON_EGG_UNMANAGEABLENotImplementedErrorr+   Z
conda_namer   Zsp_reference)Zprefix_pathZanchor_fileZpython_versionZpydistr   r   rI   r   r   Z
paths_tupsrM   ri   r6   r6   r7   read_python_record   sR    

r   )RZ
__future__r   r   r   r   base64r   collectionsr   r1   r   	functoolsr	   r
   r;   	itertoolsr   rT   Zloggingr   osr   os.pathr   r   r   Zconda._vendor.auxlib.compatr   rY   linkr   r   Z_vendor.auxlib.collectionr   Z_vendor.auxlib.ishr   Zbase.constantsr   Zcommon.compatr   r   Zcommon.pkg_formats.pythonr   r   r   r   
exceptionsr   r    r!   Zmodels.channelr"   Zmodels.enumsr#   r$   r%   Zmodels.package_infor&   r'   Zmodels.recordsr(   r)   r*   r+   __name__logr8   rA   rC   rE   rG   rR   rX   r\   r]   rN   rO   rP   rr   rs   r   r   r6   r6   r6   r7   <module>   sV   
*