B
    d
1                 @   sZ  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
yXejddgZee  Zd dlZedd dD reeedk rd	ZdZW n ek
r   d	ZdZY nX yd dlZd d
lmZ W n ek
r   dZdZY nX yd dlZW n ek
r   dZY nX dd Ze ZdddZejdd Zdd Z dd Z!dd Z"dS )    )absolute_importdivisionprint_functionNz-cz"import pip; print(pip.__version__)c             c   s   | ]}|t tkV  qd S )N)dirpkg_resources).0symbol r	   o/mnt/filia/a/genomebrowser/www/genomebrowser/fleming/tools/molprobity/modules/cctbx_project/libtbx/pkg_utils.py	<genexpr>   s    r   )parse_versionrequireDistributionNotFoundVersionConflictz9.0.0F)Requirementc                 sN   t tjd tj tj tj fdd| D  tj tj d tj  d S )NzP================================================================================c             3   s   | ]}|j f  V  qd S )N)format)r   l)contextr	   r
   r   1   s    z_notice.<locals>.<genexpr>)printoslinesepjoin)linesr   r	   )r   r
   _notice/   s    r   c             C   s  t stdddddd dS |s"d}t| | }|jrB|j sBdS d|_|j}t|}|jt|j }yXy td	|t	
|d
 jf  dS  t	jk
r   td|t	
|d
 jf  dS X W nx t	jk
r   d}| }d}td|  Y nL t	jk
r6   t	
|d
 j}t	
|d
 j}d}td||f  Y nX xttjD ]j}	tj|	|d }
tj|
r@t|
d}|  }W dQ R X tddddddd| |||d dS q@W tjtjdstdddddd| |||d
 dS tdrtd d krtddd!| |||d dS tjjjrDtddd"| |||d dS td#j|| d$ tj !d%}tj"d&d'd|g}t#$|}|j%}|stj &d%d |d
krtd(j|d) dS td*j|d) dS dS )+a|  Ensure a package requirement is met. Install or update package as required
     and print a warning message if this can't be done due to the local
     environment, or when automatic package management is disabled by setting
     the environment variable 'LIBTBX_DISABLE_UPDATES'.
     :param pkgname: A string describing the package requirement. This will
                     generally just be a package name, but package features
                     can be specified in square brackets. Features are not
                     enforced, but will be requested during installation and
                     update.
     :param version: An optional string describing version constraints. This
                     can be a minimum version, eg. '>=1.0', a maximum version,
                     eg. '<2', or both, eg. '>=4.5,<4.6'.
     :return: True when the requirement is met, False otherwise.zR  WARNING: Can not verify python package requirements - pip/setuptools out of datez.  Please update pip and setuptools by running: z9    libtbx.python -m pip install pip setuptools --upgradezL  or following the instructions at https://pip.pypa.io/en/stable/installing/FTNzrequires %s, has %sr   z)requires %s, has %s, but without featuresz(not determined)installz:requirement %s is not currently met, package not installedupdatez7requirement %s is not currently met, current version %sz	.egg-linkrz<    WARNING: Can not update package {package} automatically.zKIt is installed as editable package for development purposes. The currentlyzWinstalled version, {currentversion}, is too old. The required version is {requirement}.z=Please update the package manually in its installed location:z    {location})packagecurrentversionrequirementlocation.z>    WARNING: Can not {action} package {package} automatically.zMYou are running in a base-less installation, which disables automatic packagezOmanagement by convention, see https://github.com/cctbx/cctbx_project/issues/151z%Please {action} the package manually.)r   r   r    actionZLIBTBX_DISABLE_UPDATES)0r   zMEnvironment variable LIBTBX_DISABLE_UPDATES is set. Please {action} manually.z9You are in a conda environment. Please {action} manually.z#attempting {action} of {package}...)r#   r   ZPIP_REQ_TRACKERz-mpipz{action} successful)r#   z&{action} failed. please check manually)'use_pipr   r   markerZevaluatenamestr	specifierr   r   r   versionZUnknownExtrar   r   project_namesyspathr   r   isfileopenreadlinestripisdirlibtbxenv
under_basegetenvbuild_options	use_condar   environget
executable
subprocessrun
returncodepop)Zpkgnamer+   r    ZbasepkgnameZrequirestringZbaserequirestringr   r,   r#   	path_itemZegg_linkfhr!   Zhas_req_trackerZpip_install_cmdZpip_install_resultZ	exit_coder	   r	   r
   r   6   s    

 

r   c              c   s   t jdkrdV  dS tj  z<t tj } tt jd}t 	| tj  dV  W d| dk	rvt 	| tj  |dk	r|
  X dS )z%Helper context which shuts up stdout.ntNw)r   r(   r-   stdoutflushdupfilenor0   devnulldup2close)Z	oldstdoutZ	dest_filer	   r	   r
   _silence   s    


rL   c       	   	   K   s  y
t  W n$ t k
r.   t d jj}Y nX |jd }|dkrtj|j	j
}tj|\}}|dsttdtjtj|}n|dstd|dd }tstd	||tkrtd
| t| tdj|d x&| D ]}tdj|t| | d qW z|t }ttjttjjd zJtj}ddgt_t * tj f d|d|| d| W dQ R X W d|t_X W dt| X dS )zA function to allow non-setuptools packages (ie. libtbx modules) to use
     the setuptools entry_points mechanism. Call this function from
     libtbx_refresh.py and pass a dictionary of entry points.   __name____main__Zlibtbx_refreshz>Entry points can only be defined from within libtbx_refresh.pyz.libtbx_refreshNizXYou must install setuptools to configure package {}. Run
  libtbx.pip install setuptoolszEntry points have already been defined for package %s. There must only be a single call to define_entry_points() per calling packagez"Updating entry points for {caller})callerz"  {n} entries for entry point {ep})epnlibzsetup.pyZegg_infoz	libtbx.{}z!libtbx entry point manager for {})r(   descriptionentry_points)!	Exceptionr-   exc_infotb_framef_back	f_globalsr   r.   abspathf_codeco_filenamesplitextendswithRuntimeErrorbasenamedirname
setuptoolsImportErrorr   _defined_entrypointsaddr   lengetcwdchdirr   absr4   r5   
build_pathargvrL   setup)	ZepdictkwargsframerP   Zrefresh_file_rQ   curdirZ	argv_origr	   r	   r
   define_entry_points   sJ    






rr   c                s    j dkstd fdd| D }t|dkrTt|dkrJtd d| |d }nd}|r|j j@ |_|j jB |_|j jB |_n
|   dS )	a{  Merge a new requirement with a list.

  If it exists in an identical form (name, marker) then the
  specifiers and extras will be merged, otherwise it will be added.

  If the environment markers are different it will assume that they
  are mutually exclusive - entries will only be merged if identical, which
  could cause problems with duplicate requirement entries if not filtered
  by pass status later.

  URL field is also not handled, as unsure how to merge these if they differ.

  Args:
    requirements (List[packaging.requirements.Requirement]): Existing.
    new_req (packaging.requirements.Requirement): New requirement
  Nz)URL requirement fields not handled/testedc                s(   g | ] }|j  j kr|j jkr|qS r	   )r(   r'   )r   x)new_reqr	   r
   
<listcomp>	  s   z'_merge_requirements.<locals>.<listcomp>r      z,Warning: More than one match for requirementz: )urlAssertionErrorrg   r   r*   modulesextrasappend)requirementsrt   matchesmatchr	   )rt   r
   _merge_requirements   s    

r   c             C   sD   g }x:t jdd | D  D ]"\}}t|}|h|_t|| qW |S )a  Combine python requirements from a module list.

  An attempt will be made to merge any joint requirements. The requirement
  objects will have an added property 'modules', which is a set of module
  names that formed the requirement.

  Attr:
      modules (Iterable[libtbx.env_config.module]): The module list

  Returns:
      List[packaging.requirements.Requirement]: The merged requirements
  c                s*   g | ]" t  d r fdd jD qS )python_requiredc                s   g | ]} j |fqS r	   )r(   )r   y)rs   r	   r
   ru   ,  s    z:collate_python_requirements.<locals>.<listcomp>.<listcomp>)hasattrr   )r   r	   )rs   r
   ru   ,  s    z/collate_python_requirements.<locals>.<listcomp>)	itertoolschainr   ry   r   )ry   r|   modulespecr    r	   r	   r
   collate_python_requirements  s    r   )N)#
__future__r   r   r   
contextlibr   r   r-   r=   libtbx.load_envr4   r<   Zpip_version_cmdcheck_outputdecoder2   Zpip_version_strr   allr   r&   rd   	packagingZpackaging.requirementsr   rc   r   setre   r   contextmanagerrL   rr   r   r   r	   r	   r	   r
   <module>   sF   




l>(