B
    -]\(                 @   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 W n ek
r   dZY nX dZdZdd Zd4d
dZdd Zdd Ze
jdd Zdd ZeeejdfddZdd Zdd Zdd Zee_dd Zdd  Z e e_d!d" Z!d#d$ Z"e"e!_d%d& Z#d'd( e#_d)d* Z$eeejde$fd+d,Z%d-d. Z&d/d0 Z'd1d2 Z(e)d3kre*e(  dS )5a  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.
    N)log)	USER_SITEz3.5.1z5https://pypi.python.org/packages/source/s/setuptools/c              G   s   t jf|  } t| dkS )z/
    Return True if the command succeeded.
    r   )sys
executable
subprocessZcall)args r   ;/home/pi/CrowPi/Drivers/Adafruit_Python_CharLCD/ez_setup.py_python_cmd%   s    r
   r   c          	   C   sF   t | 4 td td| s8td td dS W d Q R X d S )NzInstalling Setuptoolssetup.pyinstallz-Something went wrong during the installation.zSee the error message above.   )r   r   )archive_contextr   warnr
   )archive_filenameZinstall_argsr   r   r	   _install-   s    



r   c          	   C   sR   t |" td| tdddd| W d Q R X t|  tj| sNtdd S )NzBuilding a Setuptools egg in %szsetup.pyz-qZ	bdist_eggz
--dist-dirzCould not build the egg.)r   r   r   r
   ospathexistsIOError)eggr   to_dirr   r   r	   
_build_egg8   s    

r   c              C   s(   G dd dt j} tt jdr$t jS | S )zL
    Supplement ZipFile class to support context manager for Python 2.6
    c               @   s   e Zd Zdd Zdd ZdS )z(get_zip_class.<locals>.ContextualZipFilec             S   s   | S )Nr   )selfr   r   r	   	__enter__H   s    z2get_zip_class.<locals>.ContextualZipFile.__enter__c             S   s
   | j  d S )N)close)r   typevalue	tracebackr   r   r	   __exit__J   s    z1get_zip_class.<locals>.ContextualZipFile.__exit__N)__name__
__module____qualname__r   r   r   r   r   r	   ContextualZipFileG   s   r#   r   )zipfileZZipFilehasattr)r#   r   r   r	   get_zip_classC   s    r&   c          	   c   s   t  }td| t }z`t| t | }|  W d Q R X tj	
|t|d }t| td| d V  W d t| t| X d S )NzExtracting in %sr   zNow working in %s)tempfileZmkdtempr   r   r   getcwdchdirr&   Z
extractallr   joinlistdirshutilZrmtree)filenameZtmpdirZold_wdarchiveZsubdirr   r   r	   r   P   s    



r   c             C   s|   t j|d| tjd tjd f }t j|sJt| |||}t||| tjd| dtj	krjtj	d= dd l
}||_d S )Nzsetuptools-%s-py%d.%d.eggr      pkg_resources)r   r   r*   r   version_infor   download_setuptoolsr   insertmodules
setuptoolsZbootstrap_install_from)versiondownload_baser   download_delayr   r.   r5   r   r   r	   _do_downloadf   s    

r9      c       	   
   C   s   t j|}d}ttj|}ydd l}W n tk
rJ   t	| |||S X y|
d|   d S  |jk
r~   t	| |||S  |jk
r } zF|rtdj|| d}tj| td ~tjd= t	| |||S d }~X Y nX d S )N)r0   r5   r   zsetuptools>=aO  
                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})
                )VC_errr6   r   r0   )r   r   abspathsetr   r4   intersectionr0   ImportErrorr9   ZrequireZDistributionNotFoundZVersionConflicttextwrapdedentformatstderrwriteexit)	r6   r7   r   r8   Zrep_modulesZimportedr0   r;   msgr   r   r	   use_setuptoolsx   s(    

rG   c             C   sD   yt |  W n0 t jk
r>   t|tjr8t|  Y nX dS )zm
    Run the command to download target. If the command fails, clean up before
    re-raising the error.
    N)r   
check_callZCalledProcessErrorr   accessF_OKunlink)cmdtargetr   r   r	   _clean_check   s    
rN   c             C   s*   t j|}dddt  g}t|| dS )z
    Download the file at url to target using Powershell (which will validate
    trust). Raise an exception if the command cannot complete.
    
powershellz-CommandzC(new-object System.Net.WebClient).DownloadFile(%(url)r, %(target)r)N)r   r   r<   varsrN   )urlrM   rL   r   r   r	   download_file_powershell   s
    rR   c              C   sf   t  dkrdS dddg} ttjjd}z.ytj| ||d W n tk
rR   dS X W d |	  X dS )	NZWindowsFrO   z-Commandz	echo testwb)stdoutrC   T)
platformsystemopenr   r   devnullr   rH   	Exceptionr   )rL   rX   r   r   r	   has_powershell   s    


rZ   c             C   s   d| dd|g}t || d S )Ncurlz--silentz--output)rN   )rQ   rM   rL   r   r   r	   download_file_curl   s    r\   c              C   sT   ddg} t tjjd}z.ytj| ||d W n tk
r@   dS X W d |  X dS )Nr[   z	--versionrS   )rT   rC   FT)rW   r   r   rX   r   rH   rY   r   )rL   rX   r   r   r	   has_curl   s    

r]   c             C   s   d| dd|g}t || d S )Nwgetz--quietz--output-document)rN   )rQ   rM   rL   r   r   r	   download_file_wget   s    r_   c              C   sT   ddg} t tjjd}z.ytj| ||d W n tk
r@   dS X W d |  X dS )Nr^   z	--versionrS   )rT   rC   FT)rW   r   r   rX   r   rH   rY   r   )rL   rX   r   r   r	   has_wget   s    

r`   c             C   s   yddl m} W n  tk
r0   ddlm} Y nX d }}z(|| }| }t|d}|| W d|rp|  |r||  X dS )za
    Use Python to download the file, even though it cannot authenticate the
    connection.
    r   )urlopenNrS   )Zurllib.requestra   r?   Zurllib2readrW   rD   r   )rQ   rM   ra   ZsrcZdstdatar   r   r	   download_file_insecure   s    
rd   c               C   s   dS )NTr   r   r   r   r	   <lambda>       re   c              C   s*   t tttg} x| D ]}| r|S qW d S )N)rR   r\   r_   rd   viable)ZdownloadersZdlr   r   r	   get_best_downloader   s    
rh   c       	      C   s^   t j|}d|  }|| }t j||}t j|sRtd| | }||| t j|S )a  
    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.
    zsetuptools-%s.zipzDownloading %s)r   r   r<   r*   r   r   r   realpath)	r6   r7   r   Zdelaydownloader_factoryZzip_namerQ   ZsavetoZ
downloaderr   r   r	   r2   	  s    
r2   c             C   s   | j rdgS g S )zT
    Build the arguments to 'python setup.py install' on the setuptools package
    z--user)user_install)optionsr   r   r	   _build_install_args"  s    rm   c              C   sj   t  } | jdddddd | jddd	td
d | jddddd tdd | jddtd |  \}}|S )z,
    Parse the command line for options
    z--userrk   Z
store_trueFz;install in user site package (requires Python 2.6 or later))destactiondefaulthelpz--download-baser7   ZURLz=alternative URL from where to download the setuptools package)rn   Zmetavarrp   rq   z
--insecurerj   Zstore_constc               S   s   t S )N)rd   r   r   r   r	   re   6  rf   z_parse_args.<locals>.<lambda>z'Use internal, non-validating downloader)rn   ro   Zconstrp   rq   z	--versionz!Specify which version to download)rq   rp   )optparseZOptionParserZ
add_optionDEFAULT_URLrh   DEFAULT_VERSIONZ
parse_args)Zparserrl   r   r   r   r	   _parse_args(  s"    ru   c              C   s(   t  } t| j| j| jd}t|t| S )z-Install or upgrade setuptools and EasyInstall)r6   r7   rj   )ru   r2   r6   r7   rj   r   rm   )rl   r.   r   r   r	   mainA  s    
rv   __main__)r   )+__doc__r   r,   r   r'   r$   rr   r   rU   r@   
contextlibZ	distutilsr   Zsiter   r?   rt   rs   r
   r   r   r&   contextmanagerr   r9   curdirrG   rN   rR   rZ   rg   r\   r]   r_   r`   rd   rh   r2   rm   ru   rv   r    rE   r   r   r   r	   <module>   sZ   




