U
    @vg5H                     @   sZ  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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mZ dd
lmZ ddlmZ ddlmZ er<ddlmZ ddlmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z& ddl'm(Z( ddl)m*Z* ddl+m,Z, ddl-m.Z. e!e&e/e%f  Z0e e%ge&e1ef f Z2dgZ3e4dej5Z6e4dZ7e4dZ8ej9ej:ej;ej<ej=ej>ej?ej@ejAejBejCejDejEgZFejGejHejIgZJdd eJD ZKG dd deLZMd6ddZNdd ZOd7dd ZPG d!d" d"eLZQd#d$ ZRd%d& ZSG d'd( d(eTZUd)d* ZVd+d, ZWd-d. ZXd/d0 ZYd1d2 ZZd8d3d4Z[e4d5ej5Z\dS )9z
Requirements file parsing
    )absolute_importN)filterfalse)parse)
cmdoptions)InstallationErrorRequirementsFileParseError)SearchScope)install_req_from_editableinstall_req_from_line)auto_decode)MYPY_CHECK_RUNNING)get_url_scheme)Values)AnyCallableIteratorListNoReturnOptionalTextTuple)InstallRequirement)
WheelCache)PackageFinder)
PipSessionparse_requirementsz^(http|https|file):z(^|\s+)#.*$z#(?P<var>\$\{(?P<name>[A-Z0-9_]+)\})c                 C   s   g | ]}t | jqS  )strdest).0or   r   >/tmp/pip-unpacked-wheel-gw11q0wt/pip/_internal/req/req_file.py
<listcomp>T   s     r"   c                   @   s   e Zd Zdd ZdS )
ParsedLinec                 C   s(   || _ || _|| _|| _|| _|| _d S N)filenamelineno
comes_fromargsopts
constraint)selfr%   r&   r'   r(   r)   r*   r   r   r!   __init__X   s    
zParsedLine.__init__N__name__
__module____qualname__r,   r   r   r   r!   r#   W   s   r#   Fc                 c   sZ   |r
|j nd}t|}	t||	||}
|
| |D ]$}t||||||}|dk	r0|V  q0dS )a!  Parse a requirements file and yield InstallRequirement instances.

    :param filename:    Path or url of requirements file.
    :param session:     PipSession instance.
    :param finder:      Instance of pip.index.PackageFinder.
    :param comes_from:  Origin description of requirements.
    :param options:     cli options.
    :param constraint:  If true, parsing a constraint file rather than
        requirements file.
    :param wheel_cache: Instance of pip.wheel.WheelCache
    :param use_pep517:  Value of the --use-pep517 option.
    N)skip_requirements_regexget_line_parserRequirementsFileParserr   handle_line)r%   sessionfinderr'   optionsr*   wheel_cache
use_pep517r1   line_parserparserparsed_linereqr   r   r!   r   j   s(            c                 C   s:   t |  dd}t|}t|}|r.t||}t|}|S )zSplit, filter, and join lines, and return a line iterator

    :param content: the content of the requirements file
    :param options: cli options
       )start)	enumerate
splitlines
join_linesignore_comments
skip_regexexpand_env_variables)contentr1   
lines_enumr   r   r!   
preprocess   s    
rH   c              
   C   s  d| j rdnd| j| jf }| jr|r,|jnd}|rBt|| j i }tD ],}	|	| jj	krJ| jj	|	 rJ| jj	|	 ||	< qJd
| j| j}
t| j|||||| j |
dS | jjr|r|jnd}t| jjd ||| j ||dS | jjr| jj|_n|r|j}|j}| jjr| jjg}| jjd	kr&g }| jjr>|| jj | jjr| jjd }tjtj| j}tj||}tj|r|}|| t||d
}||_| jjr|  |r| jj pg D ]$}d
| j| j}|j!||d qdS )a  Handle a single parsed requirements line; This can result in
    creating/yielding requirements, or updating the finder.

    For lines that contain requirements, the only options that have an effect
    are from SUPPORTED_OPTIONS_REQ, and they are scoped to the
    requirement. Other options from SUPPORTED_OPTIONS may be present, but are
    ignored.

    For lines that do not contain requirements, the only options that have an
    effect are from SUPPORTED_OPTIONS. Options from SUPPORTED_OPTIONS_REQ may
    be present, but are ignored. These lines may contain multiple options
    (although our docs imply only one is supported), and all our parsed and
    affect the finder.
    z%s %s (line %s)z-cz-rFzline {} of {})r'   r9   isolatedr7   r8   r*   line_sourcer   )r'   r9   r*   rI   r8   T)
find_links
index_urls)sourceN)"r*   r%   r&   r(   isolated_moder   check_install_build_globalr)   SUPPORTED_OPTIONS_REQ_DEST__dict__formatr
   	editablesr	   require_hashesrK   rL   	index_urlno_indexextra_index_urlsextendospathdirnameabspathjoinexistsappendr   search_scopepreset_allow_all_prereleasestrusted_hostsadd_trusted_host)liner6   r7   r5   r8   r9   line_comes_fromrI   req_optionsr   rJ   rK   rL   valuereq_dirrelative_to_reqs_filer`   hostrM   r   r   r!   r4      s      
  





r4   c                   @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
r3   c                 C   s   || _ || _|| _|| _d S r$   )_session_line_parser_comes_from_skip_requirements_regex)r+   r5   r:   r'   r1   r   r   r!   r,   	  s    zRequirementsFileParser.__init__c                 c   s   |  ||D ]
}|V  qdS )z3Parse a given file, yielding parsed lines.
        N)_parse_and_recurse)r+   r%   r*   re   r   r   r!   r     s    zRequirementsFileParser.parsec                 c   s   |  ||D ]}|js|jjs|jjs.|jjr|jjrH|jjd }d}n|jjd }d}t|rpt	||}n t|st
jt
j||}| ||D ]
}|V  qq|V  qd S )Nr   FT)_parse_filer(   r)   rS   requirementsconstraints	SCHEME_REsearchurllib_parseurljoinrY   rZ   r]   r[   rp   )r+   r%   r*   re   req_pathnested_constraint
inner_liner   r   r!   rp     s6    


  
z)RequirementsFileParser._parse_and_recursec                 c   s   t || j| jd\}}t|| j}|D ]j\}}z| |\}}	W n8 tk
rx }
 zd||
jf }t|W 5 d }
~
X Y nX t	||| j||	|V  q&d S )N)r'   zInvalid requirement: %s
%s)
get_file_contentrl   rn   rH   ro   rm   OptionParsingErrormsgr   r#   )r+   r%   r*   _rF   rG   line_numberre   args_strr)   er}   r   r   r!   rq   ?  s(      
z"RequirementsFileParser._parse_fileN)r.   r/   r0   r,   r   rp   rq   r   r   r   r!   r3     s   "r3   c                    s    fdd}|S )Nc                    s^   t  }| }d |_ r  j|_t| \}}tjdk r@|d}|t	
||\}}||fS )N)         utf8)build_parserget_default_valuesrU   format_controlbreak_args_optionssysversion_infoencode
parse_argsshlexsplit)re   r;   defaultsr   options_strr)   r~   r6   r   r!   
parse_line[  s    

 z#get_line_parser.<locals>.parse_liner   )r6   r   r   r   r!   r2   Y  s    r2   c                 C   sf   |  d}g }|dd }|D ]2}|ds6|dr< qRq|| |d qd|d|fS )zBreak up the line into an args and options string.  We only want to shlex
    (and then optparse) the options, not the args.  args can contain markers
    which are corrupted by shlex.
     N-z--r   )r   
startswithr_   popr]   )re   tokensr(   r7   tokenr   r   r!   r   t  s    

r   c                   @   s   e Zd Zdd ZdS )r|   c                 C   s
   || _ d S r$   )r}   r+   r}   r   r   r!   r,     s    zOptionParsingError.__init__Nr-   r   r   r   r!   r|     s   r|   c                  C   s@   t jdd} tt }|D ]}| }| | qdd }|| _| S )z7
    Return a parser for parsing requirement lines
    F)add_help_optionc                 S   s   t |d S r$   )r|   r   r   r   r!   parser_exit  s    z!build_parser.<locals>.parser_exit)optparseOptionParserSUPPORTED_OPTIONSSUPPORTED_OPTIONS_REQ
add_optionexit)r;   option_factoriesoption_factoryoptionr   r   r   r!   r     s    r   c                 c   s   d}g }| D ]v\}}| dr(t|rjt|r:d| }|r^|| |d|fV  g }q||fV  q|sr|}||d q|r|d|fV  dS )zJoins a line ending in '' with the previous line (except when following
    comments).  The joined line takes on the index of the first line.
    N\r    )endswith
COMMENT_REmatchr_   r]   strip)rG   primary_line_numbernew_liner   re   r   r   r!   rB     s     

rB   c                 c   s4   | D ]*\}}t d|}| }|r||fV  qdS )z1
    Strips comments and filter empty lines.
    r   N)r   subr   )rG   r   re   r   r   r!   rC     s
    rC   c                    s    t | t fdd| } | S )zd
    Skip lines that match the provided pattern

    Note: the regex pattern is only built once
    c                    s     | d S )Nr>   )ru   )r   matcherr   r!   <lambda>      zskip_regex.<locals>.<lambda>)recompiler   )rG   patternr   r   r!   rD     s    
rD   c                 c   sL   | D ]B\}}t |D ]$\}}t|}|s.q|||}q||fV  qdS )a  Replace all environment variables that can be retrieved via `os.getenv`.

    The only allowed format for environment variables defined in the
    requirement file is `${MY_VARIABLE_1}` to ensure two things:

    1. Strings that contain a `$` aren't accidentally (partially) expanded.
    2. Ensure consistency across platforms for requirement files.

    These points are the result of a discussion on the `github pull
    request #3514 <https://github.com/pypa/pip/pull/3514>`_.

    Valid characters in variable names follow the `POSIX standard
    <http://pubs.opengroup.org/onlinepubs/9699919799/>`_ and are limited
    to uppercase letter, digits and the `_` (underscore).
    N)
ENV_VAR_REfindallrY   getenvreplace)rG   r   re   env_varvar_namerh   r   r   r!   rE     s    
rE   c           
   
   C   s&  t | }|dkr.|| }|  |j|jfS |dkr|rT|drTtd|| f | ddd }|dd}t	
|}|r|dd |d	dd  }t|}|drd|d }|} z&t| d
}t| }W 5 Q R X W n4 tk
r }	 ztdt|	 W 5 d}	~	X Y nX | |fS )aZ  Gets the content of a file; it may be a filename, file: URL, or
    http: URL.  Returns (location, content).  Content is unicode.
    Respects # -*- coding: declarations on the retrieved files.

    :param url:         File path or url.
    :param session:     PipSession instance.
    :param comes_from:  Origin description of requirements.
    )httphttpsfiler   z6Requirements file %s references URL %s, which is local:r>   r   /|rbz$Could not open requirements file: %sN)r   getraise_for_statusurltextr   r   r   r   _url_slash_drive_rer   grouprv   unquotelstripopenr   readIOErrorr   )
r   r5   r'   schemeresprZ   r   frF   excr   r   r!   r{     s:    





r{   z/*([a-z])\|)NNNFNN)NNNNN)N)]__doc__
__future__r   r   rY   r   r   r   Zpip._vendor.six.movesr   Zpip._vendor.six.moves.urllibr   rv   Zpip._internal.clir   pip._internal.exceptionsr   r   !pip._internal.models.search_scoper   pip._internal.req.constructorsr	   r
   pip._internal.utils.encodingr   pip._internal.utils.typingr   pip._internal.utils.urlsr   r   typingr   r   r   r   r   r   r   r   Zpip._internal.reqr   pip._internal.cacher   "pip._internal.index.package_finderr   pip._internal.network.sessionr   intZReqFileLinesr   Z
LineParser__all__r   Irt   r   r   rU   extra_index_urlrV   rs   rr   editablerK   	no_binaryonly_binaryrT   ra   trusted_hostalways_unzipr   install_optionsglobal_optionshashr   rP   objectr#   r   rH   r4   r3   r2   r   	Exceptionr|   r   rB   rC   rD   rE   r{   r   r   r   r   r!   <module>   s   (

      
'     
gQ
,