B
    '\                 @   sD  d Z ddlmZmZmZmZ ddlmZmZm	Z	m
Z
mZ ddlZddlZddlZddlmZmZmZmZmZmZ ddlZddlmZ 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'm(Z( e'Z'e)e*Z+eZ,dd Z-erdd Z.dddZ/dd Z0d ddZ1d!ddZ2dd Z3e4dej5d dS )"ar   This module contains:
  * all low-level code for extracting, linking and unlinking packages
  * a very simple CLI

These API functions have argument names referring to:

    dist:        canonical package name (e.g. 'numpy-1.6.2-py26_0')

    pkgs_dir:    the "packages directory" (e.g. '/opt/anaconda/pkgs' or
                 '/home/joe/envs/.pkgs')

    prefix:      the prefix of a particular environment, which may also
                 be the "default" environment (i.e. sys.prefix),
                 but is otherwise something like '/opt/anaconda/envs/foo',
                 or even any prefix, e.g. '/home/joe/myenv'
    )absolute_importdivisionprint_functionunicode_literals)EACCESEEXISTENOENTEPERMEROFSN)dirnameisdirisfilejoinnormcasenormpath   )PREFIX_PLACEHOLDER)
itervalueson_winopen	iteritems)rm_rf)Dist)PackageType)	MatchSpec)
rm_fetchedPackageCacheDatac              C   s   G dd dt } |  S )Nc               @   s$   e Zd Zdd Zdd Zdd ZdS )z$package_cache.<locals>.package_cachec             S   s   t t t| d S )N)boolr   first_writablegetr   to_package_ref)selfdist r#   ,lib/python3.7/site-packages/conda/install.py__contains__1   s    z1package_cache.<locals>.package_cache.__contains__c             S   s   dd t t D S )Nc             s   s   | ]}t |V  qd S )N)r   ).0vr#   r#   r$   	<genexpr>5   s    z<package_cache.<locals>.package_cache.keys.<locals>.<genexpr>)r   r   r   )r!   r#   r#   r$   keys4   s    z)package_cache.<locals>.package_cache.keysc             S   s   t  t|  d S )N)r   r   remover   r    )r!   r"   r#   r#   r$   __delitem__7   s    z0package_cache.<locals>.package_cache.__delitem__N)__name__
__module____qualname__r%   r)   r+   r#   r#   r#   r$   package_cache/   s   r/   )object)r/   r#   r#   r$   r/   .   s    r/   c          
   C   s2  ddl m} ytt| W n< tk
rZ } z|jtkrHtt|rHn W dd}~X Y nX t	|d st
|d d}|d|   W dQ R X |sd}t	|s.t
|dT}|d | d	r|d
|| d | d   n|d|| d |   W dQ R X t|j}||d@ d? O }t|| dS )a-  Special function for Windows XP where the `CreateSymbolicLink`
        function is not available.

        Simply creates a `.bat` file at `dst` which calls `src` together with
        all command line arguments.

        Works of course only with callable files, e.g. `.bat` or `.exe` files.
        r   )shellsNz.batwz@echo off
call "%s" %%*
zbash.exez#!/usr/bin/env bash 
condaz%s "$@"Zpath_toz.exezsource %s "$@"i$     )Zutilsr1   osmakedirsr   OSErrorerrnor   r   r   r   writeendswithstatst_modechmod)srcdstshellr1   excfmoder#   r#   r$   win_conda_bat_redirect>   s*    	


 $rD   c             C   sn   t t| t t|krd S tr4d}tjt|d}n
d}tj}tt	| |s\t
t	| | t| ||| d S )NZScripts)r@   bin)r   r   r   	functoolspartialrD   r5   symlinkr   r   r6   symlink_conda_hlp)prefixroot_dirr@   where
symlink_fnr#   r#   r$   symlink_condal   s    rN   c       
      C   s  dddg}t | |}t|s&t| x|D ]}t |||}t ||}y.tj|r\t| tj|sr||| W q, ttfk
r }	 zttj|r|	j	t
tttfkrtd|| n>|	j	tkrtd|| n |	j	tkrtd|| n W d d }	~	X Y q,X q,W d S )Nr3   ZactivateZ
deactivatez>Cannot symlink {0} to {1}. Ignoring since link already exists.zbProblem with symlink management {0} {1}. File may have been removed by another concurrent process.zbProblem with symlink management {0} {1}. File may have been created by another concurrent process.)r   r   r5   r6   pathlexistsr   IOErrorr7   r8   r	   r   r
   r   logdebugformatr   )
rJ   rK   rL   rM   ZscriptsZprefix_whererB   Z	root_fileZprefix_fileer#   r#   r$   rI   |   s.    






rI   Fc                s8   ddl m} ddlm  || } fddt|jD S )z?
    Return a dictionary of the linked packages in prefix.
    r   )
PrefixData)r   c                s   i | ]}| |qS r#   r#   )r&   prefix_record)r   r#   r$   
<dictcomp>   s    zlinked_data.<locals>.<dictcomp>)core.prefix_datarV   models.distr   r   Z_prefix_records)rJ   ignore_channelsrV   pdr#   )r   r$   linked_data   s    r]   c                s.   t   tt| |d}t fdd|D S )z8
    Return the Dists of linked packages in prefix.
    )r[   c             3   s    | ]\}}|j  kr|V  qd S )N)Zpackage_type)r&   r"   Z
prefix_rec)conda_package_typesr#   r$   r(      s    zlinked.<locals>.<genexpr>)r   r^   r   r]   set)rJ   r[   Zldr#   )r^   r$   linked   s    r`   c             C   sH   ddl m} || }||jd}|dkr.dS t||r@|S dS dS )z
    Return the install metadata for a linked package in a prefix, or None
    if the package is not linked in the prefix.
    r   )rV   N)rY   rV   r   namer   match)rJ   r"   rV   r\   rW   r#   r#   r$   	is_linked   s    rc   zXWARNING: The conda.install module is deprecated and will be removed in a future release.)file)N)F)F)6__doc__Z
__future__r   r   r   r   r8   r   r   r   r	   r
   rF   Zloggingr5   os.pathr   r   r   r   r   r   sysZbase.constantsr   Zcommon.compatr   r   r   r   Zgateways.disk.deleter   rZ   r   Zmodels.enumsr   Zmodels.match_specr   Zcore.package_cache_datar   r   Z	getLoggerr,   rR   Zprefix_placeholderr/   rD   rN   rI   r]   r`   rc   printstderrr#   r#   r#   r$   <module>   s6    
.




