U
    @vgj                     @   s  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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 d dlm Z  d dl!m"Z"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.m/Z/m0Z0 d dl1m2Z2 d dl3m4Z4 d dl5m6Z6 d dl7m8Z8m9Z9 e4rd dlm:Z: d dl;m<Z<m=Z=m>Z>m?Z? d dl@mAZA d dlBmCZC d dl7mDZD d dlmEZE eFeGZHdd ZIG d d! d!eZJd"d# ZKd$d% ZLd-d'd(ZMd)d* ZNd+d, ZOdS ).    )absolute_importN)SUPPRESS_HELP)pkg_resources)canonicalize_name)
WheelCache)
cmdoptions)make_target_python)RequirementCommand)ERRORSUCCESS)CommandErrorInstallationErrorPreviousBuildDirErrordistutils_scheme)check_install_conflicts)RequirementSetinstall_given_reqs)get_requirement_tracker)
deprecated)parse_distutils_argstest_writable_dir)
ensure_dirget_installed_version(protect_pip_from_modification_on_windowswrite_output)TempDirectory)MYPY_CHECK_RUNNING)virtualenv_no_global)build should_build_for_install_command)Values)AnyIterableListOptional)FormatControl)InstallRequirement)BinaryAllowedPredicate)running_under_virtualenvc                    s    fdd}|S )Nc                    s&   | j r
dS t| j} |}d|kS )NTbinary)
use_pep517r   nameget_allowed_formats)reqcanonical_nameallowed_formatsformat_control B/tmp/pip-unpacked-wheel-gw11q0wt/pip/_internal/commands/install.pycheck_binary_allowed@   s
    

z6get_check_binary_allowed.<locals>.check_binary_allowedr4   )r3   r6   r4   r2   r5   get_check_binary_allowed>   s    r7   c                       s<   e Zd ZdZdZ fddZdd Zdd Zd	d
 Z  Z	S )InstallCommandaI  
    Install packages from:

    - PyPI (and other indexes) using requirement specifiers.
    - VCS project urls.
    - Local project directories.
    - Local or remote source archives.

    pip also supports installing from "requirements files", which provide
    an easy way to specify a whole environment to be installed.
    a%  
      %prog [options] <requirement specifier> [package-index-options] ...
      %prog [options] -r <requirements file> [package-index-options] ...
      %prog [options] [-e] <vcs project url> ...
      %prog [options] [-e] <local project path> ...
      %prog [options] <archive url/path> ...c                    sp  t t| j|| | j}|t  |t  |t  |t	  |t
  |jddddd dd t| |jddd	d
d |jddd	td |jdddd dd |jdddd dd |jddd	dd |t  |t  |jdddd	dd |jdddddgdd |jd d!d	d"d |jd#d$d%d	d&d |t  |t  |t  |t  |t  |t  |jd'd	d(d)d*d+ |jd,d-d(d.d/ |jd0d-d1d)d2d+ |jd3d-d4d)d5d+ |t  |t  |t  |t  |t  |t  ttj| j}| jd6| | jd6| d S )7Nz-tz--target
target_dirdirzInstall packages into <dir>. By default this will not replace existing files/folders in <dir>. Use --upgrade to replace existing packages in <dir> with new versions.)destmetavardefaulthelpz--useruse_user_site
store_truea  Install to the Python user install directory for your platform. Typically ~/.local/, or %APPDATA%\Python on Windows. (See the Python documentation for site.USER_BASE for full details.)  On Debian systems, this is the default when running outside of a virtual environment and not as root.)r;   actionr>   z	--no-useruse_system_locationz--root	root_pathz=Install everything relative to this alternate root directory.z--prefixprefix_pathzIInstallation prefix where lib, bin and other top-level folders are placedz--systemzDInstall using the system scheme (overrides --user on Debian systems)z-Uz	--upgradeupgradezUpgrade all specified packages to the newest available version. The handling of dependencies depends on the upgrade-strategy used.z--upgrade-strategyupgrade_strategyzonly-if-neededeageraG  Determines how dependency upgrading should be handled [default: %default]. "eager" - dependencies are upgraded regardless of whether the currently installed version satisfies the requirements of the upgraded package(s). "only-if-needed" -  are upgraded only when they do not satisfy the requirements of the upgraded package(s).)r;   r=   choicesr>   z--force-reinstallforce_reinstallz;Reinstall all packages even if they are already up-to-date.z-Iz--ignore-installedignore_installedzIgnore the installed packages, overwriting them. This can break your system if the existing package is of a different version or was installed with a different package manager!z	--compilecompileTz'Compile Python source files to bytecode)rA   r;   r=   r>   z--no-compilestore_falsez.Do not compile Python source files to bytecode)rA   r;   r>   z--no-warn-script-locationwarn_script_locationz0Do not warn when installing scripts outside PATHz--no-warn-conflictswarn_about_conflictsz%Do not warn about broken dependenciesr   )superr8   __init__cmd_opts
add_optionr   requirementsconstraintsno_depspreeditableadd_target_python_optionsr   	build_dirsrcignore_requires_pythonno_build_isolationr,   no_use_pep517install_optionsglobal_options	no_binaryonly_binaryprefer_binaryno_cleanrequire_hashesprogress_barmake_option_groupindex_groupparserinsert_option_group)selfargskwrQ   
index_opts	__class__r4   r5   rP   _   s     

 	 
zInstallCommand.__init__c           '         s  t | d}|jr|j}t j|dd |jr8|jg}nd }|jsl|jsl|jsl|j	slt
 slt dkrld|_|j	rxd|_tj|j|_|jpg }t|j|j|j|j|jd|_d }d }|jr
d|_tj|j|_tj|jrtj|jstdtdd	}|j}|jpg }| |}	t|}
| j||	|
|jd
}|jpH|j }t |j!|j"}t# 
}t|j|dd}t$|j d}zz(| '|||||	| t(||j | j)||||	||jd}| j*|||||j|j|j|j+||j,d
}| -| |.| z|/d}W n t0k
r*   d }Y nX |j1d k}t2|d t3|j"  fdd|j45 D }t6||g g d\}}dd |D }|rt7d8d9dd |D |:|}|j; o|j<}|r| =| |j>}|jrd}t?||||j||j|j@||jd	}tA|j||j|j|jd}tBC|}|jDtEFdd g }|D ]R} | jG}!z$tH| jG|d}"|"rp|!d|" 7 }!W n tIk
r   Y nX |J|! qDd 9|}#|#rtKd!|# W n tLk
r" }$ zN| jMd"k}%tN|$|%|j}&tOjP|&|%d# tQ W Y W HW  5 Q R  W  5 Q R  S d }$~$X Y n tRk
r>   d|_ Y nX W 5 |js\|%  |&  X W 5 Q R X W 5 Q R X |jr| S|j||j tTS )$Nzto-satisfy-onlyT)check_targetr   F)rD   r9   rC   isolated_modez=Target path exists but is not a directory, will not continue.target)kind)optionssessiontarget_pythonr[   install)deleters   )check_supported_wheels)temp_build_dirrt   req_trackerru   finderr?   )
preparerr|   rt   wheel_cacher?   rJ   r[   rI   rF   r,   pip)modifying_pipc                    s   g | ]}t | r|qS r4   )r!   .0rr6   r4   r5   
<listcomp>w  s    z&InstallCommand.run.<locals>.<listcomp>)r~   build_optionsr_   c                 S   s   g | ]}|j r|qS r4   )r,   r   r4   r4   r5   r     s     zPCould not build wheels for {} which use PEP 517 and cannot be installed directlyz, c                 s   s   | ]}|j V  qd S N)r-   r   r4   r4   r5   	<genexpr>  s     z%InstallCommand.run.<locals>.<genexpr>)roothomeprefix	pycompilerM   r?   )userr   r   r   isolatedr-   )key)working_set- zSuccessfully installed %s   exc_info)Ur   check_install_build_globalrE   rF   check_dist_restrictionpython_versionr?   rD   r9   rB   r*   osgeteuidpathabspathsrc_dirr^   decide_user_installrC   rq   rJ   existsisdirr   r   r_   get_default_sessionr   _build_package_finderr[   rc   rY   r   	cache_dirr3   r   r   cleanup_filescleanuppopulate_requirement_setwarn_deprecated_install_optionsmake_requirement_preparermake_resolverrI   r,   trace_basic_inforesolveget_requirementKeyErrorsatisfied_byr   r7   rS   valuesr    r   formatjoinget_installation_orderignore_dependenciesrN   _warn_about_conflictsrM   r   rK   get_lib_location_guessesr   
WorkingSetsortoperator
attrgetterr-   r   	Exceptionappendr   EnvironmentError	verbositycreate_env_error_messageloggererrorr
   r   _handle_target_dirr   )'rj   rt   rk   rF   python_versionsr^   target_temp_dirtarget_temp_dir_pathr_   ru   rv   r|   build_deleter~   r{   	directoryrequirement_setr}   resolverpip_reqr   reqs_to_build_build_failurespep517_build_failures
to_installshould_warn_about_conflictsrM   	installedlib_locationsr   itemsresultiteminstalled_versioninstalled_descr   show_tracebackmessager4   r   r5   run   s   




       








 
 
  4  zInstallCommand.runc              	      sf  t | g }|J td|jd}|d }|d }|d }tj|rP|| tj|rn||krn|| tj|r|| |D ]}	t|	D ]}
|	|krtj||
 t fdd|d d D rqtj||
}tj|r>|st	
d	| qtj|rt	
d
| qtj|r4t| n
t| ttj|	|
| qqW 5 Q R X d S )N )r   purelibplatlibdatac                 3   s   | ]}|  V  qd S r   )
startswith)r   sddirr4   r5   r     s     z4InstallCommand._handle_target_dir.<locals>.<genexpr>zKTarget directory %s already exists. Specify --upgrade to force replacement.zTarget directory %s already exists and is a link. Pip will not automatically replace links, please remove if replacement is desired.)r   r   r   r   r   r   listdirr   anyr   warningislinkr   shutilrmtreeremovemove)rj   r9   r   rE   lib_dir_listschemepurelib_dirplatlib_dirdata_dirlib_dirr   target_item_dirr4   r   r5   r     sP    



z!InstallCommand._handle_target_dirc              
   C   s   zt |\}}W n$ tk
r4   tjddd Y d S X |\}}|D ]2}|| d }|| D ]}td|||d  qZqB|D ]8}|| d }|| D ]\}	}
}td||||	|
 qqzd S )NzError checking for conflicts.Tr   r   z*%s %s requires %s, which is not installed.r   zF%s %s has requirement %s, but you'll have %s %s which is incompatible.)r   r   r   r   critical)rj   r   package_set	_dep_infomissingconflictingproject_nameversion
dependencydep_namedep_versionr/   r4   r4   r5   r     s4          z$InstallCommand._warn_about_conflicts)
__name__
__module____qualname____doc__usagerP   r   r   r   __classcell__r4   r4   rn   r5   r8   K   s     i8r8   c                  O   s   t d| |}|d |d gS )Nr   r   r   )r   r   )rk   kwargsr   r4   r4   r5   r   8  s    r   c                  K   s   t dd ttf | D S )Nc                 s   s   | ]}t |V  qd S r   r   )r   dr4   r4   r5   r   >  s    z)site_packages_writable.<locals>.<genexpr>)allsetr   )r  r4   r4   r5   site_packages_writable=  s    r  Fc                 C   s   | dk	r| st d dS | rF|r*tdt r8tdt d dS | dksRt|sZ|rht d dS tjs|t d	 dS t||d
rt d dS t 	d dS )aZ  Determine whether to do a user install based on the input options.

    If use_user_site is False, no additional checks are done.
    If use_user_site is True, it is checked for compatibility with other
    options.
    If use_user_site is None, the default behaviour depends on the environment,
    which is provided by the other arguments.
    Nz$Non-user install by explicit requestFzVCan not combine '--user' and '--prefix' as they imply different installation locationszZCan not perform a '--user' install. User site-packages are not visible in this virtualenv.z User install by explicit requestTz3Non-user install due to --prefix or --target optionz4Non-user install because user site-packages disabled)r   r   z0Non-user install because site-packages writeablezMDefaulting to user installation because normal site-packages is not writeable)
r   debugr   r   r   AssertionErrorsiteENABLE_USER_SITEr  info)r?   rD   r9   rC   rq   r4   r4   r5   r   C  s4    





r   c                 C   s   dd }| j t| j  }g }|D ]8}|jdg }t|}|r$|d||	 | q$|rt|}|r|d||	  |sdS t
dd|d	d
dd dS )zIf any location-changing --install-option arguments were passed for
    requirements or on the command-line, then show a deprecation warning.
    c                 S   s   dd | D S )Nc                 S   s   g | ]}d  |ddqS )z--{}r   r   )r   replace)r   r-   r4   r4   r5   r     s     zKwarn_deprecated_install_options.<locals>.format_options.<locals>.<listcomp>r4   )option_namesr4   r4   r5   format_options  s    z7warn_deprecated_install_options.<locals>.format_optionsr^   z{!r} from {}z{!r} from command lineNz}Location-changing options found in --install-option: {}. This configuration may cause unexpected behavior and is unsupported.z; zCusing pip-level options like --user, --prefix, --root, and --targetz20.2i  )reasonreplacementgone_inissue)unnamed_requirementslistrS   r   rt   getr   r   r   keysr   r   )r   rt   r  rS   	offendersrequirementr^   location_optionsr4   r4   r5   r     sD    
 
r   c                 C   s   g }| d |s,| d | t|  n
| d |d  d7  < | jtjkrd}d}|st||d| g n
| | | d	 d
| d S )z{Format an error message for an EnvironmentError

    It may occur anytime during the execution of the install command.
    z5Could not install packages due to an EnvironmentErrorz: .r   
z"Consider using the `--user` optionzCheck the permissionsz or z.
r   )r   strerrnoEACCESextendlowerr   strip)r   r   using_user_sitepartsuser_option_partpermissions_partr4   r4   r5   r     s&    


 

r   )NNNF)P
__future__r   r  loggingr   r   r   r	  optparser   pip._vendorr   pip._vendor.packaging.utilsr   pip._internal.cacher   Zpip._internal.clir   Zpip._internal.cli.cmdoptionsr   pip._internal.cli.req_commandr	   pip._internal.cli.status_codesr
   r   pip._internal.exceptionsr   r   r   pip._internal.locationsr   pip._internal.operations.checkr   Zpip._internal.reqr   r   pip._internal.req.req_trackerr   pip._internal.utils.deprecationr   "pip._internal.utils.distutils_argsr   pip._internal.utils.filesystemr   pip._internal.utils.miscr   r   r   r   pip._internal.utils.temp_dirr   pip._internal.utils.typingr   pip._internal.utils.virtualenvr   pip._internal.wheel_builderr    r!   r"   typingr#   r$   r%   r&   #pip._internal.models.format_controlr'   pip._internal.req.req_installr(   r)   r*   	getLoggerr   r   r7   r8   r   r  r   r   r   r4   r4   r4   r5   <module>   sb   
   p    
<7