3
[}                 @   s  d Z ddl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dlZddlZddlZddlmZ ddlmZ ddlmZ ddlmZ ddlmZ dd	lmZ dd
lmZ ddlmZmZ ddl m!Z!m"Z"m#Z# ddl$m%Z%m&Z& ddl'm(Z( ddl)m*Z*m+Z+m,Z,m-Z- ddl.m/Z/ ddl0m1Z1 ddl2m3Z3 ddl4m5Z5 e3r^ddl6m7Z7m8Z8m9Z9 dZ:d1Z;ej<e=Z>d3ddZ?dd Z@dd ZAe
jBde
jCZDdd  ZEd!d" ZFd#d$ ZGd4d'd(ZHd)d* ZId+d, ZJG d-d. d.eKZLG d/d0 d0eKZMdS )5zH
Support for installing and building the "wheel" binary package format.
    )absolute_importN)urlsafe_b64encode)Parser)pkg_resources)ScriptMaker)canonicalize_name)StringIO)
pep425tags)path_to_url
unpack_url)InstallationErrorInvalidWheelFilenameUnsupportedWheel)PIP_DELETE_MARKER_FILENAMEdistutils_scheme)
indent_log)call_subprocesscaptured_stdout
ensure_dirread_chunks)SETUPTOOLS_SHIM)TempDirectory)MYPY_CHECK_RUNNING)open_spinner)DictListOptionalz.whl      c             C   sr   t j }d}t| d2}x*t||dD ]}|t|7 }|j| q&W W dQ R X dt|j jdj	d }||fS )z5Return (hash, length) for path using hashlib.sha256()r   rb)sizeNzsha256=latin1=)
hashlibsha256openr   lenupdater   digestdecoderstrip)path	blocksizehlengthfblockr(    r1   4/tmp/pip-install-65viz3iy/pip/pip/_internal/wheel.pyrehash5   s    
r3   c             C   s6   t jd dk ri }d}nddi}d}t| || f|S )Nr      bnewline )sysversion_infor%   )namemodenlbinr1   r1   r2   open_for_csvC   s    r>   c             C   s   t jj| rt| dH}|j }|jds.dS tjjtj	 }d| t j
jd }|j }W dQ R X t| d}|j| |j| W dQ R X dS dS )	zLReplace #!python with #!/path/to/python
    Return True if file was changed.r   s   #!pythonFs   #!asciiNwbT)osr+   isfiler%   readline
startswithr8   
executableencodegetfilesystemencodinglinesepreadwrite)r+   script	firstlineexenamerestr1   r1   r2   
fix_scriptM   s    

rO   zX^(?P<namever>(?P<name>.+?)(-(?P<ver>.+?))?)
                                \.dist-info$c             C   s   | j dd}xttj|D ]f}tj|}|r|jd|krttjj||d,}x$|D ]}|j	 j
 }|dkrTdS qTW W dQ R X qW dS )	zP
    Return True if the extracted wheel in wheeldir should go into purelib.
    -_r:   WHEELzroot-is-purelib: trueTNF)replacerA   listdirdist_info_rematchgroupr%   r+   joinlowerr*   )r:   wheeldirname_foldeditemrV   wheelliner1   r1   r2   root_is_purelibc   s    

r_   c                s   t jj| si i fS t| <}t }x$|D ]}|j|j  |jd q*W |jd W d Q R X tj	j
|}|jdi }|jdi }dd  t fdd|j D }t fd	d|j D }||fS )
N
r   console_scriptsgui_scriptsc             S   s   t | jddjdS )zRget the string representation of EntryPoint, remove space and split
        on '=' r7   r"   )strrS   split)sr1   r1   r2   	_split_ep   s    z"get_entrypoints.<locals>._split_epc             3   s   | ]} |V  qd S )Nr1   ).0v)rg   r1   r2   	<genexpr>   s    z"get_entrypoints.<locals>.<genexpr>c             3   s   | ]} |V  qd S )Nr1   )rh   ri   )rg   r1   r2   rj      s    )rA   r+   existsr%   r   rJ   stripseekr   
EntryPoint	parse_mapgetdictvalues)filenamefpdatar^   entry_pointsconsoleguir1   )rg   r2   get_entrypointss   s    

ry   c       	         sR  | sdS t jt}x2| D ]*}tjj|}tjj|}|| j| qW dd tjj	ddj
tjD   jtjjtjjtj  fdd|j D }|sdS g }xn|j D ]b\}} t| } t| dkrd	j| d
 }n$djdj| dd d | d  }|jdj|| qW d}t|dkr8|j|jd n|j|jd dj|S )zDetermine if any scripts are not on PATH and format a warning.

    Returns a warning message if one or more scripts are not on PATH,
    otherwise None.
    Nc             S   s    g | ]}t jj|jt jqS r1   )rA   r+   normcaser*   sep)rh   ir1   r1   r2   
<listcomp>   s    z5message_about_scripts_not_on_PATH.<locals>.<listcomp>PATHr7   c                s&   i | ]\}}t jj| kr||qS r1   )rA   r+   rz   )rh   
parent_dirscripts)not_warn_dirsr1   r2   
<dictcomp>   s   z5message_about_scripts_not_on_PATH.<locals>.<dictcomp>r   zscript {} isr   zscripts {} arez, z and z.The {} installed in '{}' which is not on PATH.zeConsider adding {} to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.zthis directoryzthese directoriesr`   r   )collectionsdefaultdictsetrA   r+   dirnamebasenameaddenvironrp   re   pathsepappendrz   r8   rE   itemssortedr&   formatrX   )	r   grouped_by_dirdestfiler   script_namewarn_for	msg_lines
start_textlast_line_fmtr1   )r   r2   !message_about_scripts_not_on_PATH   s:    


 r   FTc       -   (      sD  |st | |||||	d}t| r,|d n|d g g jtjjtjj }i t  g }|rt 4}tj	   tj
d tj|ddd W dQ R X W dQ R X tj|j  dd	 d3 fdd		d4	
fdd	}||d std
 tjjd d}t|\fdd}xvD ]n}d}d}x^tjtjj|D ]F}d}|dkrpt}|}tjj||}|| }|||d
||d qVW q4W td|d d_dh_d_
fdd}|_d_jdd}|rdtjkrd| }|jj| tjjdddkrRdt j!dd |f }|jj| dt j!dd  |f }|jj| d!d" D }x|D ]}|= qW jd#d}|rdtjkrd$| }|jj| d%t j!dd  |f }|jj| d&d" D }x|D ]}|= qW t"dkrnj#d'd" j$ D }|j| |
rnt%|}|dk	rntj&| t"dkr|jj#d(d" j$ D d)di tjjd d*}tjjd d+} t'| d,}!|!j(d- W dQ R X t)j*| | |j+| tjjd d.}"tjjd d/}#t,|"d0}$t,|#d1}%t-j.|$}&t-j/|%}'g }(xZ|&D ]R})j|)d |)d |)d< |)d  krt0|)d \|)d< |)d2< |(j+t1|) qVW x0|D ](}*t0|*\}+},|(j+|*|+|,f qW x"D ]}*|(j+|* ddf qW xt2|(D ]})|'j3|) qW W dQ R X W dQ R X t)j*|#|" dS )5zInstall a wheel)userhomerootisolatedprefixpurelibplatlibignoreT)forcequietNc             S   s   t jj| |jt jjdS )N/)rA   r+   relpathrS   r{   )srcpr1   r1   r2   normpath   s    z"move_wheel_files.<locals>.normpathFc                s.   | }|}||< |r* j | dS )z6Map archive RECORD paths to installation RECORD paths.N)r   )srcfiler   modifiedoldpathnewpath)changed	installedlib_dirr   rZ   r1   r2   record_installed   s
    

z*move_wheel_files.<locals>.record_installedc                s  t | xtj| D ]\}}}|t| d  jtjj}tjj||}	|rj|jtjjdd j	drjqx|D ]}
tjj|||
}|r|dkr|j	dr j
|
 qpqp|rp|
j	drpt|
jtjrp std| d dj j
| qpW x|D ]}|r||rqtjj||}tjj|||}t |	 tjj|rRtj| tj|| tj|}ttdrtj||j|jf tj|tjrtj|}|jtjB tjB tjB }tj|| d	}|r||}||| qW qW d S )
Nr   r   z.datar7   z
.dist-infoz!Multiple .dist-info directories: z, utimeF)r   rA   walkr&   lstripr+   r{   rX   re   endswithr   r   rD   r:   AssertionErrorrk   unlinkshutilcopyfilestathasattrr   st_atimest_mtimeaccessX_OKst_modeS_IXUSRS_IXGRPS_IXOTHchmod)sourcedestis_basefixerfilterdirsubdirsfilesbasedirdestdirrf   
destsubdirr/   r   r   stpermissionsr   )	data_dirsinfo_dirr   reqr1   r2   clobber   sN    








z!move_wheel_files.<locals>.clobberz!%s .dist-info directory not foundr   zentry_points.txtc                sh   | j  jdr| d d }n<| j  jdr8| d d }n | j  jdrT| d d }n| }| kpf|kS )	Nz.exe   z
-script.py
   z.pyair   )rY   r   )r:   	matchname)rw   rx   r1   r2   is_entrypoint_wrapperL  s    z/move_wheel_files.<locals>.is_entrypoint_wrapperr   )r   r   r7   c                s<   | j d krtd| f  j| j| j jdd | j d S )NzInvalid script entry point: %s for req: %s - A callable suffix is required. Cf https://packaging.python.org/en/latest/distributing.html#console-scripts for more information..r   )moduleimport_namefunc)suffixr   script_templater   re   )entry)makerr   r1   r2   _get_script_textz  s    
z*move_wheel_files.<locals>._get_script_textz# -*- coding: utf-8 -*-
import re
import sys

from %(module)s import %(import_name)s

if __name__ == '__main__':
    sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0])
    sys.exit(%(func)s())
pipENSUREPIP_OPTIONSzpip = 
altinstallz
pip%s = %sr   r4   c             S   s   g | ]}t jd |r|qS )zpip(\d(\.\d)?)?$)rerV   )rh   kr1   r1   r2   r}     s    z$move_wheel_files.<locals>.<listcomp>easy_installzeasy_install = zeasy_install-%s = %sc             S   s   g | ]}t jd |r|qS )zeasy_install(-\d\.\d)?$)r   rV   )rh   r   r1   r1   r2   r}     s    c             S   s   g | ]}d | qS )z%s = %sr1   )rh   kvr1   r1   r2   r}     s    c             S   s   g | ]}d | qS )z%s = %sr1   )rh   r   r1   r1   r2   r}     s    rx   	INSTALLERzINSTALLER.pipr@   s   pip
RECORDz
RECORD.piprzw+   )F)NN)4r   r_   r*   rA   r+   r{   r   r   warningscatch_warningsfilterwarnings
compileallcompile_dirloggerdebuggetvaluer   rX   ry   rT   rO   r   r   variantsset_moder   r   popr   extendmakerp   r8   versionr&   make_multipler   r   warningr%   rJ   r   mover   r>   csvreaderwriterr3   tupler   writerow)-r:   r   rZ   r   r   r   	pycompileschemer   r   warn_script_locationr   	generatedstdoutr   ep_filer   datadirr   r   subdirr   r   
pip_scriptspecpip_epr   easy_install_scripteasy_install_epgenerated_console_scriptsmsg	installertemp_installerinstaller_filerecordtemp_record	record_in
record_outr   r   Zoutrowsrowr/   r(   r.   r1   )r   rw   r   rx   r   r   r   r   r   r   r   rZ   r2   move_wheel_files   s    




$F


#










$r  c             C   sn   yTdd t jd| D d }|jd}t j|}|d j }ttt|j	d}|S  t
k
rh   dS X dS )	z
    Return the Wheel-Version of an extracted wheel, if possible.

    Otherwise, return False if we couldn't parse / extract it.
    c             S   s   g | ]}|qS r1   r1   )rh   dr1   r1   r2   r}     s    z!wheel_version.<locals>.<listcomp>Nr   rR   zWheel-Versionr   F)r   find_on_pathget_metadatar   parsestrrl   r   mapintre   	Exception)
source_dirdist
wheel_datar   r1   r1   r2   wheel_version  s    
r$  c             C   sb   | st d| | d td kr>t d|djtt| f n | tkr^tjddjtt|  dS )a  
    Raises errors or warns if called with an incompatible Wheel-Version.

    Pip should refuse to install a Wheel-Version that's a major series
    ahead of what it's compatible with (e.g 2.0 > 1.1); and warn when
    installing a version only minor version ahead (e.g 1.2 > 1.1).

    version: a 2-tuple representing a Wheel-Version (Major, Minor)
    name: name of wheel or package to raise exception about

    :raises UnsupportedWheel: when an incompatible Wheel-Version is given
    z(%s is in an unsupported or invalid wheelr   zB%s's Wheel-Version (%s) is not compatible with this version of pipr   z*Installing from a newer Wheel-Version (%s)N)r   VERSION_COMPATIBLErX   r  rd   r   r   )r   r:   r1   r1   r2   check_compatibility  s    
r&  c               @   s:   e Zd ZdZejdejZdd Zd
ddZ	ddd	Z
dS )WheelzA wheel filez^(?P<namever>(?P<name>.+?)-(?P<ver>.*?))
        ((-(?P<build>\d[^-]*?))?-(?P<pyver>.+?)-(?P<abi>.+?)-(?P<plat>.+?)
        \.whl|\.dist-info)$c                s    j j|}|std| | _|jdjdd _|jdjdd _|jd _|jdj	d _
|jd	j	d _|jd
j	d _ fdd j
D  _dS )zX
        :raises InvalidWheelFilename: when the filename is invalid for a wheel
        z!%s is not a valid wheel filename.r:   rQ   rP   verbuildpyverr   abiplatc                s.   h | ]&} j D ]} jD ]}|||fqqqS r1   )abisplats)rh   xyz)selfr1   r2   	<setcomp>W  s   z!Wheel.__init__.<locals>.<setcomp>N)wheel_file_rerV   r   rs   rW   rS   r:   r   	build_tagre   
pyversionsr-  r.  	file_tags)r2  rs   
wheel_infor1   )r2  r2   __init__C  s    

zWheel.__init__Nc                s4    dkrt j   fdd| jD }|r0t|S dS )a"  
        Return the lowest index that one of the wheel's file_tag combinations
        achieves in the supported_tags list e.g. if there are 8 supported tags,
        and one of the file tags is first in the list, then return 0.  Returns
        None is the wheel is not supported.
        Nc                s   g | ]}| kr j |qS r1   )index)rh   c)tagsr1   r2   r}   e  s    z+Wheel.support_index_min.<locals>.<listcomp>)r	   get_supportedr7  min)r2  r<  indexesr1   )r<  r2   support_index_min\  s    zWheel.support_index_minc             C   s$   |dkrt j }tt|j| jS )z'Is this wheel supported on this system?N)r	   r=  boolr   intersectionr7  )r2  r<  r1   r1   r2   	supportedh  s    zWheel.supported)N)N)__name__
__module____qualname____doc__r   compileVERBOSEr4  r9  r@  rC  r1   r1   r1   r2   r'  7  s   
r'  c               @   sR   e Zd ZdZd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S )WheelBuilderz#Build wheels from a RequirementSet.NFc             C   s8   || _ || _|| _|j| _|p g | _|p*g | _|| _d S )N)finderpreparerwheel_cachewheel_download_dir
_wheel_dirbuild_optionsglobal_optionsno_clean)r2  rK  rL  rM  rP  rQ  rR  r1   r1   r2   r9  r  s    

zWheelBuilder.__init__c             C   s"   |j  | j|||dS Q R X dS )ziBuild one wheel.

        :return: The filename of the built wheel, or None if the build failed.
        )
python_tagN)	build_env_build_one_inside_env)r2  r   
output_dirrS  r1   r1   r2   
_build_one~  s    zWheelBuilder._build_onec             C   s   t dd~}| j||j|drzyFtj|jd }tjj||}tjtjj|j|| tj	d| |S  t
k
rx   Y nX | j| d S Q R X d S )Nr]   )kind)rS  r   zStored in directory: %s)r   _WheelBuilder__build_oner+   rA   rT   rX   r   r   r   infor   
_clean_one)r2  r   rV  rS  temp_dir
wheel_name
wheel_pathr1   r1   r2   rU    s    
z"WheelBuilder._build_one_inside_envc             C   s   t jddt|j gt| j S )Nz-uz-c)r8   rE   r   setup_pylistrQ  )r2  r   r1   r1   r2   _base_setup_args  s    
zWheelBuilder._base_setup_argsc             C   s   | j |}d|jf }t|~}tjd| |dd|g | j }|d k	rT|d|g7 }yt||jd|d dS  tk
r   |j	d	 tj
d
|j dS X W d Q R X d S )Nz#Running setup.py bdist_wheel for %szDestination directory: %sbdist_wheelz-dz--python-tagF)cwdshow_stdoutspinnerTerrorzFailed building wheel for %s)ra  r:   r   r   r   rP  r   setup_py_dirr   finishrf  )r2  r   tempdrS  	base_argsspin_messagere  
wheel_argsr1   r1   r2   __build_one  s    



zWheelBuilder.__build_onec             C   s`   | j |}tjd|j |ddg }yt||jdd dS  tk
rZ   tjd|j dS X d S )NzRunning setup.py clean for %scleanz--allF)rc  rd  Tz Failed cleaning build dir for %s)ra  r   rZ  r:   r   r!  r   rf  )r2  r   rj  
clean_argsr1   r1   r2   r[    s    
zWheelBuilder._clean_onec             C   s  ddl m} ddlm} | jp(|o(| jj}|s2tg }| jj	}x|D ]}	|	j
rPqD|	jrj|shtjd|	j qD|rv|	jrvqD|r|	j rqD|r|	jr|	jj r|j|	df qDd}
|r|	j}|j \}}|j|d|dkrd}
d|jt|	jkrtjd	|	j qD|j|	|
f qDW |sdS tjd
djdd |D  | j}t X g g  }}xD|D ]:\}	}d}|rtj}|r|j|	j}n|j|	j}yt| W nB tk
r } z$tj d|	j| |j|	 wVW Y dd}~X nX n| j}| j!|	||d}|r|j|	 |r|	jr:t"j#j$t"j#j|	jt% r:td|	j&  |	j'| j(j)|	_|t*||	_|	jjsntt+|	j|	jdd|d n
|j|	 qVW W dQ R X |rtjddjdd |D  |rtjddjdd |D  t,|dkS )zBuild wheels.

        :param unpack: If True, replace the sdist we built from with the
            newly built wheel, in preparation for installation.
        :return: True if all the wheels built correctly.
        r   )r:  )Linkz(Skipping %s, due to already being wheel.TFNbinaryzCSkipping bdist_wheel for %s, due to binaries being disabled for it.z*Building wheels for collected packages: %sz, c             S   s   g | ]\}}|j qS r1   )r:   )rh   r   rQ   r1   r1   r2   r}     s    z&WheelBuilder.build.<locals>.<listcomp>z Building wheel for %s failed: %s)rS  zbad source dir - missing marker)sessionzSuccessfully built %src   c             S   s   g | ]
}|j qS r1   )r:   )rh   r   r1   r1   r2   r}   7  s    zFailed to build %sc             S   s   g | ]
}|j qS r1   )r:   )rh   r   r1   r1   r2   r}   <  s    )-pip._internalr:  Zpip._internal.models.linkrp  rO  rM  	cache_dirr   rK  format_control
constraintis_wheelr   rZ  r:   editabler!  linkis_artifactr   splitextegg_info_matchesZget_allowed_formatsr   rX   r   r	   implementation_tagget_ephem_path_for_linkget_path_for_linkr   OSErrorr   rW  rA   r+   rk   r   remove_temporary_sourcebuild_locationrL  	build_dirr
   r   r&   )r2  requirementsrr  autobuildingr:  rp  building_is_possiblebuildsetru  r   ephem_cachery  baseext_cachebuild_successbuild_failureephemrS  rV  e
wheel_filer1   r1   r2   r)    s    







zWheelBuilder.build)NNF)N)N)N)F)rD  rE  rF  rG  r9  rW  rU  ra  rY  r[  r)  r1   r1   r1   r2   rJ  o  s   





rJ  )r   r      )r  )FNNTNFNT)NrG  
__future__r   r   r   r   r#   loggingos.pathrA   r   r   r   r8   r   base64r   email.parserr   pip._vendorr   pip._vendor.distlib.scriptsr   pip._vendor.packaging.utilsr   pip._vendor.sixr   rs  r	   pip._internal.downloadr
   r   pip._internal.exceptionsr   r   r   pip._internal.locationsr   r   pip._internal.utils.loggingr   pip._internal.utils.miscr   r   r   r   $pip._internal.utils.setuptools_buildr   pip._internal.utils.temp_dirr   pip._internal.utils.typingr   pip._internal.utils.uir   typingr   r   r   	wheel_extr%  	getLoggerrD  r   r3   r>   rO   rH  rI  rU   r_   ry   r   r  r$  r&  objectr'  rJ  r1   r1   r1   r2   <module>   sd   



=  
  88