
Zc           @   s  d  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 d l	 Z	 d d l
 m Z y d d l m Z Wn e k
 r d Z n Xd Z d Z d   Z d d  Z d	   Z d
   Z e e e j d d  Z d   Z d   Z d   Z e e _ d   Z d   Z e e _ d   Z d   Z e e _ d   Z  d   e  _ d   Z! e e e j d e! d  Z" d d d  Z# d   Z$ d   Z% e d  Z& e' d k re j( e&    n  d S(   s  Bootstrap setuptools installation

To use setuptools in your package's setup.py, include this
file in the same directory and add this to the top of your setup.py::

    from ez_setup import use_setuptools
    use_setuptools()

To require a specific version of setuptools, set a download
mirror, or use an alternate download directory, simply supply
the appropriate options to ``use_setuptools()``.

This file can also be run as a script to install or upgrade setuptools.
iN(   t   log(   t	   USER_SITEs   2.0.1s5   https://pypi.python.org/packages/source/s/setuptools/c          G   s#   t  j f |  }  t j |   d k S(   Ni    (   t   syst
   executablet
   subprocesst   call(   t   args(    (    sD   /oak/stanford/groups/akundaje/marinovg/programs/bwa-meth/ez_setup.pyt   _python_cmd$   s    c         C   s   t  j   } t j d |  t j   } z t j |  t j |   } t	 |  | j
   t j j | t j |  d  } t j |  t j d |  t j d  t d d |  s t j d  t j d  d	 SWd  t j |  t j |  Xd  S(
   Ns   Extracting in %si    s   Now working in %ss   Installing Setuptoolss   setup.pyt   installs-   Something went wrong during the installation.s   See the error message above.i   (   t   tempfilet   mkdtempR    t   warnt   ost   getcwdt   chdirt   tarfilet   opent   _extractallt   closet   patht   joint   listdirR   t   shutilt   rmtree(   t   tarballt   install_argst   tmpdirt   old_wdt   tart   subdir(    (    sD   /oak/stanford/groups/akundaje/marinovg/programs/bwa-meth/ez_setup.pyt   _install(   s$    

"c         C   s  t  j   } t j d |  t j   } z t j |  t j |  } t	 |  | j
   t j j | t j |  d  } t j |  t j d |  t j d |  t d d d d |  Wd  t j |  t j |  Xt j |   t j j |   st d	   n  d  S(
   Ns   Extracting in %si    s   Now working in %ss   Building a Setuptools egg in %ss   setup.pys   -qt	   bdist_eggs
   --dist-dirs   Could not build the egg.(   R	   R
   R    R   R   R   R   R   R   R   R   R   R   R   R   R   R   t   existst   IOError(   t   eggR   t   to_dirR   R   R   R   (    (    sD   /oak/stanford/groups/akundaje/marinovg/programs/bwa-meth/ez_setup.pyt
   _build_eggD   s$    

"c         C   s   t  j j | d |  t j d t j d f  } t  j j |  sj t |  | | |  } t | | |  n  t j j d |  d t j	 k r t j	 d =n  d d  l
 } | | _ d  S(   Ns   setuptools-%s-py%d.%d.eggi    i   t   pkg_resourcesi(   R   R   R   R   t   version_infoR    t   download_setuptoolsR$   t   insertt   modulest
   setuptoolst   bootstrap_install_from(   t   versiont   download_baseR#   t   download_delayR"   R   R*   (    (    sD   /oak/stanford/groups/akundaje/marinovg/programs/bwa-meth/ez_setup.pyt   _do_downloada   s    !	i   c   	      C   s!  t  j j |  } d	 } t t j  j |  } y d d  l } Wn! t k
 rc t	 |  | | |  SXy | j
 d |   d  SWn | j k
 r t	 |  | | |  S| j k
 r} | r t j d  j d | d |   } t j j |  t j d  n  ~ t j d =t	 |  | | |  SXd  S(
   NR%   R*   is   setuptools>=sO  
                The required version of setuptools (>={version}) is not available,
                and can't be installed while this script is running. Please
                install a more recent version first, using
                'easy_install -U setuptools'.

                (Currently using {VC_err.args[0]!r})
                t   VC_errR,   i   (   R%   R*   (   R   R   t   abspatht   setR   R)   t   intersectionR%   t   ImportErrorR/   t   requiret   DistributionNotFoundt   VersionConflictt   textwrapt   dedentt   formatt   stderrt   writet   exit(	   R,   R-   R#   R.   t   rep_modulest   importedR%   R0   t   msg(    (    sD   /oak/stanford/groups/akundaje/marinovg/programs/bwa-meth/ez_setup.pyt   use_setuptoolss   s(    c         C   sT   y t  j |   Wn< t  j k
 rO t j | t j  rI t j |  n    n Xd S(   sm   
    Run the command to download target. If the command fails, clean up before
    re-raising the error.
    N(   R   t
   check_callt   CalledProcessErrorR   t   accesst   F_OKt   unlink(   t   cmdt   target(    (    sD   /oak/stanford/groups/akundaje/marinovg/programs/bwa-meth/ez_setup.pyt   _clean_check   s    c         C   s9   t  j j |  } d d d t   g } t | |  d S(   s   
    Download the file at url to target using Powershell (which will validate
    trust). Raise an exception if the command cannot complete.
    t
   powershells   -CommandsC   (new-object System.Net.WebClient).DownloadFile(%(url)r, %(target)r)N(   R   R   R1   t   varsRI   (   t   urlRH   RG   (    (    sD   /oak/stanford/groups/akundaje/marinovg/programs/bwa-meth/ez_setup.pyt   download_file_powershell   s
    c          C   sx   t  j   d k r t Sd d d g }  t t j j d  } z, y t j |  d | d | Wn t SXWd  | j	   Xt
 S(   Nt   WindowsRJ   s   -Commands	   echo testt   wbt   stdoutR;   (   t   platformt   systemt   FalseR   R   R   t   devnullR   RB   R   t   True(   RG   RT   (    (    sD   /oak/stanford/groups/akundaje/marinovg/programs/bwa-meth/ez_setup.pyt   has_powershell   s    	c         C   s&   d |  d d | g } t  | |  d  S(   Nt   curls   --silents   --output(   RI   (   RL   RH   RG   (    (    sD   /oak/stanford/groups/akundaje/marinovg/programs/bwa-meth/ez_setup.pyt   download_file_curl   s    c          C   s_   d d g }  t  t j j d  } z, y t j |  d | d | Wn t SXWd  | j   Xt S(   NRW   s	   --versionRO   RP   R;   (	   R   R   R   RT   R   RB   RS   R   RU   (   RG   RT   (    (    sD   /oak/stanford/groups/akundaje/marinovg/programs/bwa-meth/ez_setup.pyt   has_curl   s    	c         C   s&   d |  d d | g } t  | |  d  S(   Nt   wgets   --quiets   --output-document(   RI   (   RL   RH   RG   (    (    sD   /oak/stanford/groups/akundaje/marinovg/programs/bwa-meth/ez_setup.pyt   download_file_wget   s    c          C   s_   d d g }  t  t j j d  } z, y t j |  d | d | Wn t SXWd  | j   Xt S(   NRZ   s	   --versionRO   RP   R;   (	   R   R   R   RT   R   RB   RS   R   RU   (   RG   RT   (    (    sD   /oak/stanford/groups/akundaje/marinovg/programs/bwa-meth/ez_setup.pyt   has_wget   s    	c         C   s   y d d l  m } Wn! t k
 r7 d d l m } n Xd } } z8 | |   } | j   } t | d  } | j |  Wd | r | j   n  | r | j   n  Xd S(   sa   
    Use Python to download the file, even though it cannot authenticate the
    connection.
    i(   t   urlopenRO   N(	   t   urllib.requestR]   R4   t   urllib2t   Nonet   readR   R<   R   (   RL   RH   R]   t   srct   dstt   data(    (    sD   /oak/stanford/groups/akundaje/marinovg/programs/bwa-meth/ez_setup.pyt   download_file_insecure   s    
c           C   s   t  S(   N(   RU   (    (    (    sD   /oak/stanford/groups/akundaje/marinovg/programs/bwa-meth/ez_setup.pyt   <lambda>   s    c          C   s7   t  t t t g }  x |  D] } | j   r | Sq Wd  S(   N(   RM   RX   R[   Re   t   viable(   t   downloaderst   dl(    (    sD   /oak/stanford/groups/akundaje/marinovg/programs/bwa-meth/ez_setup.pyt   get_best_downloader   s    	c   	      C   s   t  j j |  } d |  } | | } t  j j | |  } t  j j |  sv t j d |  |   } | | |  n  t  j j |  S(   s	  Download setuptools from a specified location and return its filename

    `version` should be a valid setuptools version number that is available
    as an egg for download under the `download_base` URL (which should end
    with a '/'). `to_dir` is the directory where the egg will be downloaded.
    `delay` is the number of seconds to pause before an actual download
    attempt.

    ``downloader_factory`` should be a function taking no arguments and
    returning a function for downloading a URL to a target.
    s   setuptools-%s.tar.gzs   Downloading %s(   R   R   R1   R   R    R    R   t   realpath(	   R,   R-   R#   t   delayt   downloader_factoryt   tgz_nameRL   t   savetot
   downloader(    (    sD   /oak/stanford/groups/akundaje/marinovg/programs/bwa-meth/ez_setup.pyR'     s    

	t   .c   
      C   sW  d d l  } d d l } d d l m } g  } | d k rC |  } n  xR | D]J } | j   r | j |  | j  |  } d | _ n  |  j | |  qJ W| j	 d | j
 d  d t  x | D] } t j j | | j  } y4 |  j | |  |  j | |  |  j | |  Wq | k
 rN}	 |  j d k r7  qO|  j d d	 |	  q Xq Wd S(
   s9  Extract all members from the archive to the current working
       directory and set owner, modification time and permissions on
       directories afterwards. `path' specifies a different directory
       to extract to. `members' is optional and must be a subset of the
       list returned by getmembers().
    iN(   t   ExtractErrori  t   keyt   namet   reversei   s   tarfile: %s(   t   copyt   operatorR   Rr   R`   t   isdirt   appendt   modet   extractt   sortt
   attrgetterRU   R   R   R   Rt   t   chownt   utimet   chmodt
   errorlevelt   _dbg(
   t   selfR   t   membersRv   Rw   Rr   t   directoriest   tarinfot   dirpatht   e(    (    sD   /oak/stanford/groups/akundaje/marinovg/programs/bwa-meth/ez_setup.pyR     s.    	c         C   s   |  j  r d g Sg  S(   sT   
    Build the arguments to 'python setup.py install' on the setuptools package
    s   --user(   t   user_install(   t   options(    (    sD   /oak/stanford/groups/akundaje/marinovg/programs/bwa-meth/ez_setup.pyt   _build_install_argsF  s    c          C   s   t  j   }  |  j d d d d d d t d d |  j d	 d d
 d d d t d d |  j d d d d d d d   d t d d |  j   \ } } | S(   s,   
    Parse the command line for options
    s   --usert   destR   t   actiont
   store_truet   defaultt   helps;   install in user site package (requires Python 2.6 or later)s   --download-baseR-   t   metavart   URLs=   alternative URL from where to download the setuptools packages
   --insecureRm   t   store_constt   constc           S   s   t  S(   N(   Re   (    (    (    sD   /oak/stanford/groups/akundaje/marinovg/programs/bwa-meth/ez_setup.pyRf   Z  s    s'   Use internal, non-validating downloader(   t   optparset   OptionParsert
   add_optionRS   t   DEFAULT_URLRj   t
   parse_args(   t   parserR   R   (    (    sD   /oak/stanford/groups/akundaje/marinovg/programs/bwa-meth/ez_setup.pyt   _parse_argsL  s    c         C   s7   t    } t d | j d | j  } t | t |   S(   s-   Install or upgrade setuptools and EasyInstallR-   Rm   (   R   R'   R-   Rm   R   R   (   R,   R   R   (    (    sD   /oak/stanford/groups/akundaje/marinovg/programs/bwa-meth/ez_setup.pyt   maina  s    	t   __main__(    ()   t   __doc__R   R   R   R	   R   R   R   RQ   R8   t	   distutilsR    t   siteR   R4   R`   t   DEFAULT_VERSIONR   R   R   R$   R/   t   curdirRA   RI   RM   RV   Rg   RX   RY   R[   R\   Re   Rj   R'   R   R   R   R   t   __name__R=   (    (    (    sD   /oak/stanford/groups/akundaje/marinovg/programs/bwa-meth/ez_setup.pyt   <module>   sX   
																(		