ó
Ř-]c           @   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
 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
 j 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   Z' d   Z( 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.
i˙˙˙˙N(   t   log(   t	   USER_SITEs   3.5.1s5   https://pypi.python.org/packages/source/s/setuptools/c          G   s#   t  j f |  }  t j |   d k S(   s/   
    Return True if the command succeeded.
    i    (   t   syst
   executablet
   subprocesst   call(   t   args(    (    s@   /home/pi/CrowPi/Drivers/Adafruit_Python_LED_Backpack/ez_setup.pyt   _python_cmd%   s    c         C   sT   t  |   B t j d  t d d |  sJ t j d  t j d  d SWd  QXd  S(   Ns   Installing Setuptoolss   setup.pyt   installs-   Something went wrong during the installation.s   See the error message above.i   (   t   archive_contextR    t   warnR   (   t   archive_filenamet   install_args(    (    s@   /home/pi/CrowPi/Drivers/Adafruit_Python_LED_Backpack/ez_setup.pyt   _install-   s    c      
   C   sk   t  |  + t j d |  t d d d d |  Wd  QXt j |   t j j |   sg t d   n  d  S(   Ns   Building a Setuptools egg in %ss   setup.pys   -qt	   bdist_eggs
   --dist-dirs   Could not build the egg.(   R	   R    R
   R   t   ost   patht   existst   IOError(   t   eggR   t   to_dir(    (    s@   /home/pi/CrowPi/Drivers/Adafruit_Python_LED_Backpack/ez_setup.pyt
   _build_egg8   s    c          C   s6   d t  j f d     Y}  t t  j d  r2 t  j S|  S(   sL   
    Supplement ZipFile class to support context manager for Python 2.6
    t   ContextualZipFilec           B   s   e  Z d    Z d   Z RS(   c         S   s   |  S(   N(    (   t   self(    (    s@   /home/pi/CrowPi/Drivers/Adafruit_Python_LED_Backpack/ez_setup.pyt	   __enter__H   s    c         S   s   |  j  d  S(   N(   t   close(   R   t   typet   valuet	   traceback(    (    s@   /home/pi/CrowPi/Drivers/Adafruit_Python_LED_Backpack/ez_setup.pyt   __exit__J   s    (   t   __name__t
   __module__R   R   (    (    (    s@   /home/pi/CrowPi/Drivers/Adafruit_Python_LED_Backpack/ez_setup.pyR   G   s   	R   (   t   zipfilet   ZipFilet   hasattr(   R   (    (    s@   /home/pi/CrowPi/Drivers/Adafruit_Python_LED_Backpack/ez_setup.pyt   get_zip_classC   s    c         c   sÁ   t  j   } t j d |  t j   } zw t j |  t   |    } | j   Wd  QXt j	 j
 | t j |  d  } t j |  t j d |  d  VWd  t j |  t j |  Xd  S(   Ns   Extracting in %si    s   Now working in %s(   t   tempfilet   mkdtempR    R
   R   t   getcwdt   chdirR#   t
   extractallR   t   joint   listdirt   shutilt   rmtree(   t   filenamet   tmpdirt   old_wdt   archivet   subdir(    (    s@   /home/pi/CrowPi/Drivers/Adafruit_Python_LED_Backpack/ez_setup.pyR	   P   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   R0   R7   (    (    s@   /home/pi/CrowPi/Drivers/Adafruit_Python_LED_Backpack/ez_setup.pyt   _do_downloadf   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(
   NR2   R7   i˙˙˙˙s   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_errR9   i   (   R2   R7   (   R   R   t   abspatht   setR   R6   t   intersectionR2   t   ImportErrorR<   t   requiret   DistributionNotFoundt   VersionConflictt   textwrapt   dedentt   formatt   stderrt   writet   exit(	   R9   R:   R   R;   t   rep_modulest   importedR2   R=   t   msg(    (    s@   /home/pi/CrowPi/Drivers/Adafruit_Python_LED_Backpack/ez_setup.pyt   use_setuptoolsx   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(    (    s@   /home/pi/CrowPi/Drivers/Adafruit_Python_LED_Backpack/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   R>   t   varsRV   (   t   urlRU   RT   (    (    s@   /home/pi/CrowPi/Drivers/Adafruit_Python_LED_Backpack/ez_setup.pyt   download_file_powershellŁ   s
    c          C   s   t  j   d k r t Sd d d g }  t t j j d  } z6 y t j |  d | d | Wn t	 k
 rn t SXWd  | j
   Xt S(   Nt   WindowsRW   s   -Commands	   echo testt   wbt   stdoutRH   (   t   platformt   systemt   Falset   openR   R   t   devnullR   RO   t	   ExceptionR   t   True(   RT   Rb   (    (    s@   /home/pi/CrowPi/Drivers/Adafruit_Python_LED_Backpack/ez_setup.pyt   has_powershell°   s    	c         C   s&   d |  d d | g } t  | |  d  S(   Nt   curls   --silents   --output(   RV   (   RY   RU   RT   (    (    s@   /home/pi/CrowPi/Drivers/Adafruit_Python_LED_Backpack/ez_setup.pyt   download_file_curlŔ   s    c          C   si   d d g }  t  t j j d  } z6 y t j |  d | d | Wn t k
 rU t SXWd  | j   Xt	 S(   NRf   s	   --versionR\   R]   RH   (
   Ra   R   R   Rb   R   RO   Rc   R`   R   Rd   (   RT   Rb   (    (    s@   /home/pi/CrowPi/Drivers/Adafruit_Python_LED_Backpack/ez_setup.pyt   has_curlÄ   s    	c         C   s&   d |  d d | g } t  | |  d  S(   Nt   wgets   --quiets   --output-document(   RV   (   RY   RU   RT   (    (    s@   /home/pi/CrowPi/Drivers/Adafruit_Python_LED_Backpack/ez_setup.pyt   download_file_wgetŇ   s    c          C   si   d d g }  t  t j j d  } z6 y t j |  d | d | Wn t k
 rU t SXWd  | j   Xt	 S(   NRi   s	   --versionR\   R]   RH   (
   Ra   R   R   Rb   R   RO   Rc   R`   R   Rd   (   RT   Rb   (    (    s@   /home/pi/CrowPi/Drivers/Adafruit_Python_LED_Backpack/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   urlopenR\   N(	   t   urllib.requestRl   RA   t   urllib2t   Nonet   readRa   RI   R   (   RY   RU   Rl   t   srct   dstt   data(    (    s@   /home/pi/CrowPi/Drivers/Adafruit_Python_LED_Backpack/ez_setup.pyt   download_file_insecureä   s    
c           C   s   t  S(   N(   Rd   (    (    (    s@   /home/pi/CrowPi/Drivers/Adafruit_Python_LED_Backpack/ez_setup.pyt   <lambda>ű   t    c          C   s7   t  t t t g }  x |  D] } | j   r | Sq Wd  S(   N(   RZ   Rg   Rj   Rt   t   viable(   t   downloaderst   dl(    (    s@   /home/pi/CrowPi/Drivers/Adafruit_Python_LED_Backpack/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.zips   Downloading %s(   R   R   R>   R)   R   R    R
   t   realpath(	   R9   R:   R   t   delayt   downloader_factoryt   zip_nameRY   t   savetot
   downloader(    (    s@   /home/pi/CrowPi/Drivers/Adafruit_Python_LED_Backpack/ez_setup.pyR4   	  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(    (    s@   /home/pi/CrowPi/Drivers/Adafruit_Python_LED_Backpack/ez_setup.pyt   _build_install_args"  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 d d d d t |  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
   --insecureR}   t   store_constt   constc           S   s   t  S(   N(   Rt   (    (    (    s@   /home/pi/CrowPi/Drivers/Adafruit_Python_LED_Backpack/ez_setup.pyRu   6  Rv   s'   Use internal, non-validating downloaders	   --versions!   Specify which version to download(   t   optparset   OptionParsert
   add_optionR`   t   DEFAULT_URLRz   t   DEFAULT_VERSIONt
   parse_args(   t   parserR   R   (    (    s@   /home/pi/CrowPi/Drivers/Adafruit_Python_LED_Backpack/ez_setup.pyt   _parse_args(  s"    c          C   s@   t    }  t d |  j d |  j d |  j  } t | t |    S(   s-   Install or upgrade setuptools and EasyInstallR9   R:   R}   (   R   R4   R9   R:   R}   R   R   (   R   R0   (    (    s@   /home/pi/CrowPi/Drivers/Adafruit_Python_LED_Backpack/ez_setup.pyt   mainA  s    			t   __main__(    (,   t   __doc__R   R+   R   R$   R    R   R   R^   RE   t
   contextlibt	   distutilsR    t   siteR   RA   Ro   R   R   R   R   R   R#   t   contextmanagerR	   R<   t   curdirRN   RV   RZ   Re   Rw   Rg   Rh   Rj   Rk   Rt   Rz   R4   R   R   R   R   RJ   (    (    (    s@   /home/pi/CrowPi/Drivers/Adafruit_Python_LED_Backpack/ez_setup.pyt   <module>   sZ   
																			
