B
    XM\                 @   s   d dl 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mZ d dlmZ d dlmZmZmZ d dlmZ d	d
 Zdd Zdd Zdd Zdd Zdd ZejddddfddZdS )    )absolute_importdivisionprint_function)joinisdirabspath
expanduserexistsN   )string_types)mk_relative_osx)check_call_envrec_globget_site_packages)find_executablec             C   s<   t | dg}x*|D ]"}tjdkr,t|| qtd qW dS )a  
    invokes functions in post module to relink to libraries in conda env

    :param pkg_path: look for shared objects to relink in pkg_path
    :param build_prefix: path to conda environment which contains lib/. to find
        runtime libraries.

    .. note:: develop mode builds the extensions in place and makes a link to
        package in site-packages/. The build_prefix points to conda environment
        since runtime libraries should be loaded from environment's lib/. first
    z.sodarwinz"Nothing to do on Linux or Windows.N)r   sysplatformr   print)pkg_pathZbuild_prefixZ	bin_filesZb_file r   2lib/python3.7/site-packages/conda_build/develop.pyrelink_sharedobjects   s
    

r   c          
   C   sv   t | d}t|dX}t|d}| }W dQ R X |d |krNtd|  n||d  td|  W dQ R X dS )a  
    Append pkg_path to conda.pth in site-packages directory for current
    environment. Only add path if it doens't already exist.

    :param sp_dir: path to site-packages/. directory
    :param pkg_path: the package path to append to site-packes/. dir.
    z	conda.ptharN
zpath exists, skipping zadded )r   open	readlinesr   write)sp_dirr   Zc_filefZcfZpkgs_in_dev_moder   r   r   write_to_conda_pth)   s    
r!   c             C   s&   t | d}t|s"td|  |S )z3 Return full path to setup.py or exit if not found zsetup.pyz"No setup.py found in {0}. Exiting.)r   r	   r   exitformat)Zpath_setup_pyr   r   r   get_setup_pyA   s    
r%   c             C   s0   d| dg}t | tdd|  td dS )zZ
    This invokes:
    $ python setup.py clean

    :param setup_py: path to setup.py
    pythoncleanzCompleted:  z/===============================================N)r   r   r   )r$   cmdr   r   r   _cleanL   s    
r*   c             C   s2   d| ddg}t | tdd|  td dS )z
    Define a develop function - similar to build function
    todo: need to test on win32 and linux

    It invokes:
    $ python setup.py build_ext --inplace

    :param setup_py: path to setup.py
    r&   	build_extz	--inplacezCompleted: r(   z/===============================================N)r   r   r   )r$   r)   r   r   r   
_build_extZ   s    r,   c          
   C   s   t | d}t | d}d}t|dR}t|d<}x4|D ],}||d krR|| q6td|  d}q6W W d	Q R X W d	Q R X |std
|  td t|| d	S )a@  
    Look for pkg_path in conda.pth file in site-packages directory and remove
    it. If pkg_path is not found in conda.pth, it means package is not
    installed in 'development mode' via conda develop.

    :param sp_dir: path to site-packages/. directory
    :param pkg_path: the package path to be uninstalled.
    z	conda.pthzconda.pth.tempFwr   r   zuninstalled: TNz!conda.pth does not contain path: z'package not installed via conda develop)r   r   r   r   shutilZmove)r   r   Zo_c_pthZn_c_pthfoundZnew_cZorig_cliner   r   r   
_uninstalll   s    	


r1   Fc       
      C   s   t |std|  td|ds&tt|dttjj	ttjj
f}t| tkr\| g} x~| D ]v}tt|}|rt|| d S |s|rt|}	|rt|	 |sd S |rt|	 |st|| t|| td|  qbW d S )Nz_Error: environment does not exist: %s
#
# Use 'conda create' to create the environment first.
#r&   )prefix.zcompleted operation for: )r   r   r"   r   AssertionErrorr   r   strversion_infomajorminortyper   r   r   r1   r%   r*   r,   r!   r   r   )
Zrecipe_dirsr2   Zno_pth_filer+   r'   Z	uninstallr   pathr   r$   r   r   r   execute   s2    




r;   )Z
__future__r   r   r   os.pathr   r   r   r   r	   r.   r   Zconda_interfacer   Zconda_build.postr   Zconda_build.utilsr   r   r   Zconda_build.os_utils.externalr   r   r!   r%   r*   r,   r1   r2   r;   r   r   r   r   <module>   s   