B
    l‚z]«  ã               @   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 |s˜d}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r¾t d ||¡¡ n>|	j	tkrÜt d ||¡¡ n |	j	tkrút 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    
.




