B
    §dÈŸ  ã            
   @   sÚ  d Z ddlmZmZmZ ddlZddlZddlZddlZddl	Z	ddl
Z
ddlZddlZdZdZyddlmZ ddlmZ W n4 ek
r°   ddlmZ ddlmZ dZdZY nX edkrèe
j ej ej e¡¡¡ ddlmZmZ nd	dlmZmZ e ¡ d
krddl Z ddl!Z!dd„ ZddddœZ"e ¡ dkrNde ¡ krNde"d< dZ#dZ$e$j%de#e"e ¡  dZ&ej ej 'eddddd¡¡Z(ej 'dddde&¡Z)dd„ Z*G dd „ d e+ƒZ,d!d"„ Z-e.d#krÖe
 /e-ƒ ¡ dS )$a™  
Manage conda environment for CCTBX programs

This is a minimal wrapper for conda functionality. It is meant to create
the minimum environment for building CCTBX programs and is aimed towards
developers that do not want to manage their own conda environments.
Advanced users should use conda natively so that they have full control.

This is called by bootstrap.py to set up a developer's conda
environment. If no conda installation is found, one will be downloaded.
It will be installed in the "mc3" directory at the same level as
"modules."

The minimum version for conda is 4.4. This is when conda moved towards
building packages with a set of common compilers.
é    )Úabsolute_importÚdivisionÚprint_functionNFT)Úurljoin)Úurlopen)Úcheck_outputÚcallé   ÚWindowsc          	   O   s„   d| d ksd| d krlddg|  } d}t  ¡ 2}tj| f|žd|i|—Ž}| d¡ | ¡ }W d Q R X |S t| f|ž|Ž}|S d S )Nz	conda.exer   ÚactivateÚcmdz/cÚ Ústdout)ÚtempfileZTemporaryFileÚ
subprocessZ
check_callÚseekÚreadr   )Úcommand_listÚargsÚkwargsÚoutputÚfÚ
returncode© r   ú~/mnt/filia/a/genomebrowser/www/genomebrowser/fleming/tools/molprobity/modules/cctbx_project/libtbx/auto_build/install_conda.pyr   3   s    

r   zosx-64zlinux-64zwin-64)ÚDarwinÚLinuxr
   r   Zarm64z	osx-arm64ÚPYTHON_VERSIONz${builder}_py{version}_{platform}.txtÚcctbx)ÚbuilderÚversionÚplatformz..Zcctbx_projectÚlibtbxÚ
auto_buildÚ
conda_envsc          
   C   sp   t r<t| ƒ(}t|dƒ}t ||¡ W dQ R X W dQ R X n0trlt|dƒ}t| ƒ ¡ }| |¡ W dQ R X dS )a"  
  Simple function for downloading a file from a URL
  No error checking is done since anything downloaded is necessary.

  Parameters
  ----------
  url: str
    The url pointing to the location of the file
  filename: str
    The name of the local file

  Returns
  -------
    Nothing
  ÚwbN)Úpy3r   ÚopenÚshutilZcopyfileobjÚpy2r   Úwrite)ÚurlÚfilenameZresponseZ
local_filer   r   r   Údownload_file\   s    
"r-   c               @   s>  e Zd ZdZej ddejde	e
e ¡  d¡Zeeeeeeej ddejde	e
e ¡  d¡ej ddddd	d
œe ¡  ¡eeeeej ddejde	e
e ¡  d¡dœZed  ed< ed< dddgZedddddejfdd„Zd)dd„Zd*dd„Zdd„ Zd+dd„Zdd„ Zd,dd „Zd-d"d#„Zd.d$d%„Zd/d'd(„ZdS )0Úconda_managerau  
  Class for managing conda environments

  Attributes
  ----------
  env_locations: dict
    Dictionary for determining which environment to use for each builder.
    The keys are the builder names and must match those in bootstrap.py
    The values are the relative path from "modules" to the file. Builders
    for programs outside of cctbx_project should place their environment
    files inside their repository. For example, the Phenix environment
    file should be in the phenix repository.

  Methods
  -------
  __init__(root_dir, conda_base, conda_env, verbose, log)
    Constructor that does the basic check for a conda installation
  get_conda_exe(prefix, check_file)
    Returns the platform-dependent conda executable
  get_conda_python(conda_env, check_file)
    Returns the platform-dependent conda python
  update_environments()
    Returns a list of paths that are conda environments based on the
    environments.txt file in ${HOME}/.conda
  install_miniconda(prefix)
    Downloads and installs the latest version of miniconda3
  update_conda()
    Updates the current version of conda
  create_environment(builder, filename, copy)
    Uses the known conda installtion to create an environment
  write_conda_setpaths(prefix, build_dir, conda_env, check_file)
    Writes an additional script that activates the conda environment before
    calling the normal setpaths script.
  Úphenixr$   )r   r    r!   Údialsz.conda-envsz	macos.txtz	linux.txtzwindows.txt)r   r   r
   Z	phasertng)Z	cctbxliter   r/   Zphenix_voyagerZphenix_releaseZ
xfellegacyz	dials-oldr0   ZexternalZ
molprobityZqrefineZphaserZvoyagerÚxfelÚlabelitNTé   Fc                sö  t  ¡ ˆ _|ˆ _dˆ _|dk	r,tj |¡ˆ _dˆ _|dk	rHtj |¡ˆ _|ˆ _|ˆ _	|ˆ _
|ˆ _dˆ _ˆ jdk	r‚ˆ jˆ jddˆ _tj tj d¡dd¡ˆ _ˆ  ¡ ˆ _tj ¡ ˆ _dˆ jd< d	ˆ _ˆ jdkrTtj d
¡}|dk	rTˆ jdkrú| d¡}n
| d¡}xN|D ]F}	tj tj |	d¡¡}ˆ j|dd}
tj |
¡r
|ˆ _|
ˆ _P q
W ˆ jdkrœx:ˆ jD ]0}ˆ j|dd}
tj |
¡rh|ˆ _|
ˆ _P qhW ‡ fdd„}ˆ jdkrìˆ jdk	rì|ˆ jˆ jƒ\}}
tj |
¡rì|ˆ _|
ˆ _ˆ jdkrvˆ jdkrvtj ˆ jd¡}tj |¡r8tdˆ jd |ˆ _n,tdˆ jd tdˆ jd ˆ jˆ jdˆ _ˆ jˆ jddˆ _ˆ  ¡ ˆ _ˆ jdk	rÎtj ˆ j¡s¼|ˆ jˆ jƒ\ˆ _ˆ _ˆ  ¡ ˆ _tj ˆ j¡sÖtˆ jƒ‚t  t!ˆ jddgˆ jd¡}ˆ j|d kg}x"ˆ jD ]}| "||d k¡ q
W d|kr@d}t# $|t%¡ |d  d¡}t&|d ƒ}t&|d  ƒ}|d!k s„|d!krŒ|d!k rŒtd"ƒ‚td#j'ˆ jd$ˆ jd ˆ j
rÎt!ˆ jdgˆ jd}t|ˆ jd ˆ jdk	ròtd%j'ˆ jd&ˆ jd dS )'a‘  
    Constructor that performs a basic check for the conda installation.
    If an installation is not found, the latest version can be downloaded
    and installed.

    Parameters
    ----------
    root_dir: str
      Required argument for specifying the root level directory for
      CCTBX builds. This is where the "modules" and "build" directories
      reside. If a new conda installation is required, it will be placed
      here under the "mc3" directory. The conda environment for building
      will also be placed in this directory
    conda_base: str
      Required argument for specifying location of a conda installation.
      If this is None, miniconda will be installed unless conda_env is
      set to a valid path.
    conda_env: str
      Optional argument for specifying location of a conda environment.
      Since this is assumed to be a working conda environment, a new
      installation of conda will not be created. This is for developers
      that want to manage their own environments.
    check_file: bool
      Flag for checking if a file exists. A RuntimeError is raised if a
      this flag is set and a file does not exist. Used in get_conda_exe
      and get_conda_python.
    max_retries: int
      When downloading conda packages, there may be network issues that
      prevent the environment from being constructed. This parameter
      controls the number of retry attempts for constructing the conda
      environment. The first retry is attempted 1 minute after the
      initial failure. The second retry is attempted 2 minutes, etc.
    verbose: bool
      Flag for showing conda output
    log: file
      For storing log output
    NF)Ú
check_fileú~z.condazenvironments.txtr   Z
PYTHONPATHzØ
The conda executable cannot be found. Please make sure the correct
directory for the base conda installation was provided. The directory
can be found by running "conda info" and looking the "base environment"
value.ÚPATHr
   ú;ú:z..c                s„   | d krd} |d krd}xft j |¡s~t j t j | d¡¡} ˆ j| dd}t j |¡r^| |fS | t j t j | d¡¡krdS qW d S )Nr   z..F)r4   )r   r   )ÚosÚpathÚisfileÚabspathÚjoinÚget_conda_exe)Znew_conda_baseZnew_conda_exe)Úselfr   r   Úwalk_up_check%  s    z-conda_manager.__init__.<locals>.walk_up_checkÚmc3z Using default conda installation)Úfilez+Location of conda installation not providedz$Proceeding with a fresh installation)ÚprefixÚinfoz--json)ÚenvZroot_prefixÚenvszø
There is a mismatch between the conda settings in your home directory
and what "conda info" is reporting. This is not a fatal error, but if
an error is encountered, please check that your conda installation and
environments exist and are working.
Zconda_versionÚ.r   r	   é   z£
CCTBX programs require conda version 4.4 and greater to make use of the
common compilers provided by conda. Please update your version with
"conda update conda".
z!Base conda installation:
  {base})Úbasez Build environment:
  {conda_env})Ú	conda_env)(r!   ÚsystemÚroot_dirÚ
conda_baser9   r:   ÚnormpathrJ   r4   Úmax_retriesÚverboseÚlogÚ	conda_exer>   r=   Ú
expanduserÚenvironment_fileÚupdate_environmentsÚenvironmentsÚenvironÚcopyrE   Úconda_exe_not_foundÚgetÚsplitr<   r;   ÚisdirÚprintÚinstall_minicondaÚRuntimeErrorÚjsonÚloadsr   ÚappendÚwarningsÚwarnÚRuntimeWarningÚintÚformat)r?   rL   rM   rJ   r4   rO   rP   rQ   Úpathsr:   rR   Zenvironmentr@   Úinstall_dirZ
conda_infoZconsistency_checkrE   ÚmessageZsplit_versionZmajor_versionZminor_versionr   r   )r?   r   Ú__init__¾   sª    '












zconda_manager.__init__c             C   sf   |dkr| j }|dkr| j}| jdkr8tj |dd¡}ntj |dd¡}|rbtj |¡sbt| jƒ‚|S )a3  
    Find the conda executable. This is platform-dependent

    Parameters
    ----------
    prefix: str
      The path to the base conda environment
    check_file: bool
      Used to override the check_file attribute

    Returns
    -------
    conda_exe: str
      The path to the conda executable
    Nr
   ÚScriptsz	conda.exeÚbinÚconda)	rM   r4   rK   r9   r:   r=   r;   r_   rY   )r?   rC   r4   rR   r   r   r   r>   z  s    

zconda_manager.get_conda_exec             C   s˜   |dkr| j }|dkr| j}|dkr*| j}| jdkrDtj |d¡}n0| jdkrdtj |dddd¡}ntj |d	d¡}|r”tj |¡s”td
j	|dƒ‚|S )a,  
    Find the conda python. This is platform-dependent

    Parameters
    ----------
    conda_env: str
      The path to the conda environment
    check_file: bool
      Used to override the check_file attribute

    Returns
    -------
    conda_python: str
      The path to the conda python
    Nr
   z
python.exer   z
python.appZContentsZMacOSÚpythonrm   z‹
  Python could not be located in {conda_env}. Please make sure {conda_env}
  is a valid conda environment and that Python is installed.
  )rJ   )
rJ   rM   r4   rK   r9   r:   r=   r;   r_   rg   )r?   rJ   r4   Zconda_pythonr   r   r   Úget_conda_pythonœ  s"    

zconda_manager.get_conda_pythonc       	   	   C   sþ   t ƒ }yVt| jƒ}| ¡ }W dQ R X x2|D ]*}| ¡ }tj |¡r,| tj 	|¡¡ q,W W n t
k
rp   Y nX | jdk	rútj | jd¡tj tj d¡dd¡g}xT|D ]L}tj |¡rªt |¡}x0|D ](}tj ||¡}tj |¡rÊ| |¡ qÊW qªW |S )z¢
    Read and check for existence of environment directories

    Returns
    -------
    environments: list
      List of paths that exist on the filesystem
    NrF   r5   z.conda)Úsetr'   rT   Ú	readlinesÚstripr9   r:   r\   ÚaddrN   ÚIOErrorrM   r=   rS   Úlistdir)	r?   rV   r   rh   rE   Zenv_dirsZenv_dirÚdirsZ_dirr   r   r   rU   Æ  s*    	




z!conda_manager.update_environmentsc       
      C   sr  |dkr| j }ddddœ}dj|| j d}t| j dkrLd	j|| j d}| jdkr`|d
7 }n|d7 }d}t||ƒ}tj ||¡}tj |¡sÈt	dj|d| j
d t||ƒ t	dj|d| j
d nt	dj|d| j
d tj |d¡}| jdkrdj|d}d| d |g}ndddj|dg}d|g| }t	dj|d| j
d t|| jd}	| jrnt	|	| j
d |S )aT  
    Download a miniconda installer and install. The default installation
    location is at the same directory level as the "modules" directory.

    Parameters
    ----------
    prefix: str
      The installation directory for miniconda

    Returns
    -------
    conda_base: str
      The location of the "base" conda environment
    NZMacOSXr   r
   )r   r   r
   z#Miniconda3-latest-{platform}-x86_64)r!   z	osx-arm64z"Miniconda3-latest-{platform}-arm64z.exez.shz$https://repo.anaconda.com/miniconda/zDownloading {url})r+   )rB   zDownloaded file to {filename})r,   zUsing local copy at {filename}rA   zK/InstallationType=JustMe /RegisterPython=0 /AddToPath=0 /S /D={install_dir})ri   ú"z-bz-pz"{install_dir}"z/bin/shz'Installing miniconda to "{install_dir}")rE   )rL   rg   rK   Úconda_platformr   r9   r:   r=   r;   r]   rQ   r-   r   rE   rP   )
r?   rC   Zos_namesr,   Zurl_baser+   ri   Úflagsr   r   r   r   r   r^   è  sH    




zconda_manager.install_minicondac             C   sR   | j dddddddg}yt|| jd}W n tk
rD   td	ƒ Y n
X t|ƒ d
S )z¿
    Update the version of conda, if possible. The defaults channel is
    used because that is the default for a normal miniconda installation.

    Parameters
    ----------
      None
    Úupdatez-nrI   z-cÚdefaultsz-yrn   )rE   a  
*******************************************************************************
There was a failure in updating your base conda installaion. To update
manually, try running

  conda update -n base -c defaults conda

If you are using conda from a different channel, replace "defaults" with that
channel
*******************************************************************************
N)rR   r   rE   Ú	Exceptionr]   )r?   r   r   r   r   r   Úupdate_conda*  s    	
zconda_manager.update_condac          
   C   sÊ   t }| js|rt}xlt| jƒD ]^}|d7 }y||| jd}W n< tk
rx   tdj|| jt	 
¡ dƒ t	 |d ¡ Y qX P qW || jkr’tdƒ‚tdj||d| jd	 |  ¡ | _|| jkrÆtd
ƒ‚dS )a­  
    Internal convenience function for retrying creation/update of a
    conda environment.

    Parameters
    ----------
      command_list: list
        Command list for check_output
      text: str
        Text to be printed. Usually "installion into" or "update of"
      prefix: str
        Directory of conda environment
      verbose: bool
        Used to override self.verbose

    Returns
    -------
      Nothing
    r	   )rE   a$  
*******************************************************************************
There was a failure in constructing the conda environment.
Attempt {retry} of {max_retries} will start {retry} minute(s) from {t}.
*******************************************************************************
)ÚretryrO   Úté<   z‰
The conda environment could not be constructed. Please check that there is a
working network connection for downloading conda packages.
zCompleted {text}:
  {prefix})ÚtextrC   )rB   zU
The newly installed environment cannot be found in
${HOME}/.conda/environments.txt.
N)r   rP   r   ÚrangerO   rE   r}   r]   rg   ÚtimeZasctimeZsleepr_   rQ   rU   rV   )r?   r   r‚   rC   rP   Zrun_commandr   r   r   r   r   Ú_retry_commandG  s*    




zconda_manager._retry_commandr   c             C   sò  || j kr,tdj|d t| j  ¡ ƒ¡dƒ‚| jdkr>tdƒ‚|dkrR|dkrRd}|dkr˜tj | j	d	| j | ¡}|dk	r¤|d
krŠtdƒ‚| 
d|¡}ntj |¡}tj |¡sÂtdj||dƒ‚d}| d¡sÚ| d¡rÞd}| jdkrþd}tj | j	|¡}ntj | j¡}d}	d}
tj |¡r,t |¡}	tj |¡rDt |¡}
|
dk	rx|	dk	rx|
j|	jkrxtd| jd dS || jkrœd}|r’d}ddg}nd}ddg}| j|d|d|g}|rÌ| dd ¡ | jd!krôtj | jd"d#¡d$d%g| }|r
|s
| d&¡ |r |s | d'¡ |d(kr:|s:| d)¡ || jkrŒtd*d+„ |pVd,D ƒƒ}d-|d. |d |d. |d d f }| |¡ td/j|d. ||d0| jd | j||d |dd1 | jd!krî| d¡rîtd2tj |d3¡d4 dS )5a;  
    Create the environment based on the builder and file. The
    environment name is "conda_base".

    Parameters
    ----------
    builder: str
      The builder from bootstrap.py. The default environment is defined
      by the env_locations class variable
    filename: str
      If filename is not None, the argument overrides the file defined
      in the env_locations dictionary. The filename should be a
      relative path to the "modules" directory.
    python: str
      If set, the specific Python version of the environment for the
      builder is used instead of the default. Current options are
      '27' and '36' for Python 2.7 and 3.6, respectively.
    copy: bool
      If set to True, the --copy flag is passed to conda
    offline: bool
      If set to True, the --offline flag is passed to conda
    zS
The builder, {builder}, is not recognized. The available builders are,
{builders}
z, )r   ZbuildersNz&A conda installation is not available.r0   )Ú27Ú36z	dials-oldÚmodules)r†   Ú37Ú38Ú39Ú310zAOnly Python 2.7, 3.7, 3.8, 3.9, and 3.10 are currently supported.r   zŸThe file, {filename}, is not available. Please contact the developers to make sure that the requested version of Python is supported for the {builder} builder.)r,   r   FZymlZyamlTrM   zDThe environment is newer than the environment file. Skipping update.)rB   Zinstallr{   ÚUpdatingz	update ofÚcreateÚ
Installingzinstallation intoz--prefixz--filer	   rE   r
   rl   r   rI   z&&z--copyz	--offline)r0   z	dials-oldr1   r2   z-yc             s   s   | ]}t |ƒV  qd S )N)rf   )Ú.0Úir   r   r   ú	<genexpr>é  s    z3conda_manager.create_environment.<locals>.<genexpr>r‡   z#"conda-forge::python>=%s.%s,<%s.%s"r   z/{text} {builder} environment with:
  {filename})r‚   r   r,   )rP   zchttps://download.microsoft.com/download/5/D/8/5D8C65CB-C849-4025-8E95-C3966CAFD8AE/vcredist_x64.exezvcredist_x64.exe)r+   r,   )Úenv_locationsr_   rg   r=   ÚsortedÚkeysrM   r9   r:   rL   Úreplacer<   r;   ÚendswithrJ   ÚexistsÚstatÚst_mtimer]   rQ   rV   rR   ÚinsertrK   rb   Úenv_without_pythonÚtupler…   r-   )r?   r   r,   ro   rX   ÚofflineZyaml_formatÚnamerC   Z
file_statsZ	env_statsÚcommandÚtext_messagesr   Zpython_versionZpython_requirementr   r   r   Úcreate_environment  s”    












z conda_manager.create_environmentc       	      C   sâ   g }|r|  d¡ |r*|  d¡ |  d¡ tj | jd¡}d}ddg}|| jkr\d}d	d
g}| j|dddd|g| }td| jd tdj	|d d| jd x|D ]}td|| jd q¢W | j
||d |dd td| jd |S )a*  
    Create a separate environment for development tools. Packages from
    the conda-forge channel are used.

    Package list:
      svn
      git
      git-lfs

    Parameters
    ----------
      svn: bool
        If set to true, svn is installed. This is useful for Xcode 11.4
        and later where svn is no longer available.
      git: bool
        If set to true, git is installed with git-lfs. This is needed
        for repositories that use git-lfs.

    Returns
    -------
      dev_dir: str
        The directory with the environment
    ÚsvnÚgitzgit-lfsZdev_envrŽ   r   zinstallation intor{   r   z	update ofz-yz-czconda-forgez--prefixzO-------------------------------------------------------------------------------)rB   z0{text} extra development environment containing:r   )r‚   z  -r	   T)rP   )rb   r9   r:   r=   rL   rV   rR   r]   rQ   rg   r…   )	r?   r£   r¤   Zpackage_listrC   r    r¡   r   Úpackager   r   r   Úcreate_dev_environment  s,    





z$conda_manager.create_dev_environmentrn   c             C   s>  |dkr| j }|dkr t d¡}|dkr0tdƒ‚d}|dksHtj |¡sPt|ƒ‚tjdkrztj tj 	|d¡¡s®t|ƒ‚n4tj tj 	|d¡¡r¦tj tj 	|d¡¡s®t|ƒ‚|dkr¼| j
}|dkrÖd	d
lm} |ƒ }tj |¡}dd„ }tjdkrôd}tj tj 	| jdd¡¡}	tj tj 	|d¡¡}
tj tj 	||d ¡¡}t|dƒ}| |j|	||
d¡ W dQ R X |r~||ƒ d}	d}tj tj 	|d¡¡}
tj tj 	||d ¡¡}t|dƒ}| |j|	||
d¡ W dQ R X |r:||ƒ nFxBdD ]8}d}tj tj 	| jddd| ¡¡}	|dkr>d}d}d}nd }d!}d"}tj tj 	|d#| ¡¡}
tj tj 	||d$ | ¡¡}t|dƒ"}| |j|	|||||
d%¡ W dQ R X |rÂ||ƒ d&}tj tj 	|d'| ¡¡}
tj tj 	||d( | ¡¡}t|dƒ}| |j|
d)¡ W dQ R X |rü||ƒ qüW dS )*aI  
    Write a script similar to setpaths.sh/csh/bat that activates the environment
    first. This is useful for developers that want to use other software
    installed in the conda environment without having to manually activate it.

    Parameters
    ----------
      prefix: str
        The prefix for the output file. The output file will be constructed
        as <prefix>_<script name>.<extension>. The <script name> is the
        standard setpaths or unsetpaths file, and the <extension> will be
        the existing extensions for the script (sh, csh, and bat)
      build_dir: str
        The build directory where the standard setpaths script resides.
      conda_env: str
        The conda environment directory
      check_file: bool
        Used to override the check_file attribute

    Returns
    -------
      Nothing
    NÚLIBTBX_BUILDz‹Please run the dispatcher version of libtbx.install_conda or provide a valid
directory as an argument to the write_conda_setpaths function.z5Please provide the directory to the setpaths script.
Zwin32zsetpaths.batzsetpaths.shzsetpaths.cshr   )Úget_conda_prefixc             S   s:   t j | ¡r&tdjt j | ¡dƒ ntdj| dƒ‚dS )zJ
      Convenience function for checking if the script was written.
      z){filename} has been written successfully.)r,   z.
{filename} has not been written successfully.N)r9   r:   r;   r]   rg   Úbasenamer_   )r,   r   r   r   Údo_check_filer  s
    z9conda_manager.write_conda_setpaths.<locals>.do_check_filez5@ECHO OFF
CALL {mc3_dir} {conda_env}
CALL {setpaths}
rl   zactivate.batz_setpaths.batÚw)Úmc3_dirrJ   Úsetpathsrn   Z
deactivatezunsetpaths.batz_unsetpaths.bat)ÚshZcshzssource {mc3_dir}
{get_old_prompt}
conda activate {conda_env}
{set_old_prompt}
{unset_old_prompt}
source {setpaths}
Zetcz	profile.dzconda.r®   zLIBTBX_OLD_PS1=$PS1zPS1=$LIBTBX_OLD_PS1zunset LIBTBX_OLD_PS1zset libtbx_old_prompt="$prompt"zset prompt="$libtbx_old_prompt"zunset libtbx_old_promptz	setpaths.z
_setpaths.)r¬   Úget_old_promptrJ   Úset_old_promptÚunset_old_promptr­   z#conda deactivate
source {setpaths}
zunsetpaths.z_unsetpaths.)r­   )r4   r9   Úgetenvr_   r:   r\   Úsysr!   r;   r=   rJ   Zlibtbx.env_configr¨   r<   rM   r'   r*   rg   )r?   rC   Z	build_dirrJ   r4   Zbuild_dir_errorr¨   rª   Zscript_templater¬   r­   r,   r   Zextr¯   r°   r±   r   r   r   Úwrite_conda_setpaths7  s’    


	
z"conda_manager.write_conda_setpaths)NN)NN)N)N)r   NNFF)FT)rn   NNT)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r9   r:   r=   Údefault_formatrg   r    ry   r!   rK   Z
phenix_envÚdefault_filer“   rœ   rL   r³   r   rk   r>   rp   rU   r^   r~   r…   r¢   r¦   r´   r   r   r   r   r.   v   sT   "





 <
"
*"
B
8 
 
6 r.   c        	      C   sT  t j d¡} | d ks(|  d¡s(|  d¡r:t j tjd ¡} dj	| d}t
j| t|t
jd}|jdd tdd	ttj ¡ ƒd
d |jddtdddddddgdd |jdddd |jdddd |jdd tddddd |jdddd |jd d td!d"d# |jd$d td%d&d# |jd'd tdd(d)d*d+ |jd,dd-d.d/ |jd0dd-d1d/ |jd2dd-d3d/ ttjƒd4kr|| ¡  | ¡  | ¡ }|j}|jr–d }d }|j}|jd k	rÚt j |j¡}t j |¡rÌ|}|d krÚd	}tt||jd5|j d6}|j!r | !¡  |j"r| #¡  |d k	r4|j$|||j%|j&|j'd7 |j(d k	rP|j)|j(d5d8 dS )9NZLIBTBX_DISPATCHER_NAMEro   r   a„  
Example usage:

  {prog}
    Shows this help screen

  {prog} --verbose
    Shows if a base conda installation can be found. If no installation
    is found, the latest miniconda3 will be installed.

  {prog} --install_conda --builder=<builder>
    Install conda and default environment for <builder>

  {prog} --install_conda --builder=<builder> --python=36
    Install conda and default Python 3.6 environment for <builder>

  {prog} --conda_base=<path> --builder=<builder>
    Install default environment for <builder> with known conda installation

  {prog} --conda_env=<path> --builder=<builder>
    Update conda environment for builder

)Úprog)r»   ZdescriptionÚepilogZformatter_classz	--builderú?r   zŒInstall the default environment for a builder. The choices are the
      same as the ones for bootstrap.py. The default builder is "cctbx." )ÚdefaultÚtypeÚnargsÚconstZchoicesÚhelpz--pythonr†   r‰   rŠ   r‹   rŒ   z‰When set, a specific Python version of the environment will be used.
    This only affects environments selected with the --builder flag.z--install_condaZ
store_truezlWhen set, conda will be automatically downloaded and installed
      regardless of an existing installation.)ÚactionrÂ   z--update_condazáWhen set, conda will try to update itself to the latest version.
      This should only be used if your conda installation was installed by
      this script or if your conda is writeable and uses the "defaults"
      channelz--install_envr   ZENV_FILEzŸWhen set, the environment for the builder will be installed. The
      default environment can be overridden by providing a path to the
      environment file.)r¾   r¿   rÀ   rÁ   ÚmetavarrÂ   z--install_dev_envz€When set, an additional environment named dev_env will be
    created that contains extra development tools (svn, git, git-lfs).z--conda_baseZBASE_DIRECTORYas  The location of the base conda environment. This is useful for
      systems where there is a multiuser installation of conda. Providing the
      path will ensure the the correct conda is used. To determine the base
      location, run "conda info" and look for the "base environment" setting.
      This is required, otherwise, a new conda installation will be created.)r¾   r¿   rÄ   rÂ   z--conda_envZENV_DIRECTORYa  The location of the conda environment for building. This is useful
      for when the exact conda environment is known. Providing the path will
      ensure that that environment is used. Using $CONDA_PREFIX as the
      argument will use the currently active environment for building.z--write_setpathsZPREFIXrn   as  When set, another script is added that activates the conda
      environment before sourcing the setpaths script. Optionally, the prefix
      of the script can be provided. The output files will be named
      <prefix>_setpaths.<extension> and <prefix>_unsetpaths.<extension>
      where the extension will be "sh"/"csh" for linux and macOS, and
      "bat" for Windows.)r¾   r¿   rÀ   rÄ   rÁ   rÂ   z--copyFzvWhen set, the new environment has copies, not links to files. This
      should only be used when building installers.)rÃ   r¾   rÂ   z	--offlinez†When set, the network will not be accessed for installing packages.
      This should only be used if the packages are already cached.z	--verbosez*When set, output from conda is displayed. r	   T)rL   rM   rJ   r4   rP   )r   r,   ro   rX   rž   )rC   r4   )*r9   rW   rZ   Ú
startswithr—   r:   r©   r³   Úargvrg   ÚargparseZArgumentParserr¸   ZRawDescriptionHelpFormatterZadd_argumentÚstrr”   r.   r“   r•   ÚlenZ
print_helpÚexitZ
parse_argsrM   Úinstall_condar   Zinstall_envr<   r;   rL   rJ   rP   r~   Zinstall_dev_envr¦   r¢   ro   rX   rž   Zwrite_setpathsr´   )	r»   r¼   ZparserÚ	namespacerM   r,   r   Ztmp_filenameÚmr   r   r   ÚrunÈ  s˜    







rÎ   Ú__main__)0r¸   Ú
__future__r   r   r   rÇ   r`   r9   r!   r(   r³   r„   rc   r)   r&   Zurllib.parser   Zurllib.requestr   ÚImportErrorZurlparseZurllib2Ú__package__r:   rb   Údirnamer<   Ú__file__Zinstaller_utilsr   r   rK   r   r   ry   r    r¹   rg   Zdefault_filenamer=   rL   rº   r-   Úobjectr.   rÎ   rµ   rÊ   r   r   r   r   Ú<module>   sh   
      X 
