B
    `’»dƒJ  ã               @   sF  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mZ ddl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!m"Z" ddl#m$Z$ ddl%m&Z& ee"e'e!f  Z(ee!ge"e)ef f Z*dgZ+e ,dej-¡Z.e ,d¡Z/e ,d¡Z0ej1ej2ej3ej4ej5ej6ej7ej8ej9ej:ej;ej<ej=gZ>ej?ej@ejAgZBdd„ eBD ƒZCG dd„ deDƒZEG dd„ deDƒZFd6dd„ZGdd„ ZHd7dd„ZId8dd „ZJd9d!d"„ZKG d#d$„ d$eDƒZLd%d&„ ZMd'd(„ ZNG d)d*„ d*eOƒZPd+d,„ ZQd-d.„ ZRd/d0„ ZSd1d2„ ZTd:d3d4„ZUe ,d5ej-¡ZVdS );z
Requirements file parsing
é    )Úabsolute_importN)Úparse)Ú
cmdoptions)ÚInstallationErrorÚRequirementsFileParseError)ÚSearchScope)Úauto_decode)ÚMYPY_CHECK_RUNNING)Úget_url_scheme)ÚValues)	ÚAnyÚCallableÚDictÚIteratorÚListÚNoReturnÚOptionalÚTextÚTuple)Ú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-hu9qqap1/pip/_internal/req/req_file.pyú
<listcomp>M   s    r   c               @   s   e Zd Zddd„ZdS )ÚParsedRequirementNc             C   s(   || _ || _|| _|| _|| _|| _d S )N)ÚrequirementÚis_editableÚ
comes_fromÚoptionsÚ
constraintÚline_source)Úselfr    r!   r"   r$   r#   r%   r   r   r   Ú__init__Q   s    
zParsedRequirement.__init__)NN)Ú__name__Ú
__module__Ú__qualname__r'   r   r   r   r   r   P   s    r   c               @   s   e Zd Zdd„ ZdS )Ú
ParsedLinec             C   s`   || _ || _|| _|| _|| _|r6d| _d| _|| _n&|jrVd| _d| _|jd | _nd| _d S )NTFr   )	ÚfilenameÚlinenor"   Úoptsr$   Úis_requirementr!   r    Ú	editables)r&   r,   r-   r"   Úargsr.   r$   r   r   r   r'   d   s    
zParsedLine.__init__N)r(   r)   r*   r'   r   r   r   r   r+   c   s   r+   Fc       
      c   sL   t |ƒ}t|||ƒ}x2| | |¡D ]"}t||||d}	|	dk	r"|	V  q"W d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.
    )r#   ÚfinderÚsessionN)Úget_line_parserÚRequirementsFileParserr   Úhandle_line)
r,   r3   r2   r"   r#   r$   Úline_parserÚparserÚparsed_lineÚ
parsed_reqr   r   r   r      s    c             C   s,   t |  ¡ dd}t|ƒ}t|ƒ}t|ƒ}|S )zxSplit, filter, and join lines, and return a line iterator

    :param content: the content of the requirements file
    é   )Ústart)Ú	enumerateÚ
splitlinesÚ
join_linesÚignore_commentsÚexpand_env_variables)ÚcontentÚ
lines_enumr   r   r   Ú
preprocess¢   s
    rD   c             C   s¼   d  | jrdnd| j| j¡}| js&t‚| jrBt| j| j|| jdS |rTt	 
|| j¡ i }x4tD ],}|| jjkr^| jj| r^| jj| ||< q^W d  | j| j¡}t| j| j|| j||dS d S )Nz{} {} (line {})z-cz-r)r    r!   r"   r$   zline {} of {})r    r!   r"   r$   r#   r%   )Úformatr$   r,   r-   r/   ÚAssertionErrorr!   r   r    r   Úcheck_install_build_globalr.   ÚSUPPORTED_OPTIONS_REQ_DESTÚ__dict__)Úliner#   Úline_comes_fromÚreq_optionsr   r%   r   r   r   Úhandle_requirement_line¯   s.    


rM   c             C   sð   | j r| j |_ nÜ|rì|j}|j}| jr.| jg}| jdkr<g }| jrN| | j¡ | jrš| jd }tj 	tj 
|¡¡}	tj |	|¡}
tj |
¡r|
}| |¡ t||d}||_| jrº| ¡  |rìx,| jpÈg D ]}d ||¡}|j||d qÊW d S )NTr   )Ú
find_linksÚ
index_urlszline {} of {})Úsource)Úrequire_hashesrN   rO   Ú	index_urlÚno_indexÚextra_index_urlsÚextendÚosÚpathÚdirnameÚabspathÚjoinÚexistsÚappendr   Úsearch_scopeÚpreÚset_allow_all_prereleasesÚtrusted_hostsrE   Úadd_trusted_host)r.   r,   r-   r2   r#   r3   rN   rO   ÚvalueÚreq_dirÚrelative_to_reqs_filer]   ÚhostrP   r   r   r   Úhandle_option_lineÛ   s8    



rf   c             C   s4   | j rt| |ƒ}|S t| j| j| j|||ƒ dS dS )a  Handle a single parsed requirements line; This can result in
    creating/yielding requirements, or updating the finder.

    :param line:        The parsed line to be processed.
    :param options:     CLI options.
    :param finder:      The finder - updated by non-requirement lines.
    :param session:     The session - updated by non-requirement lines.

    Returns a ParsedRequirement object if the line is a requirement line,
    otherwise returns None.

    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.
    N)r/   rM   rf   r.   r,   r-   )rJ   r#   r2   r3   r:   r   r   r   r6     s    
r6   c               @   s,   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	S )
r5   c             C   s   || _ || _|| _d S )N)Ú_sessionÚ_line_parserÚ_comes_from)r&   r3   r7   r"   r   r   r   r'   ;  s    zRequirementsFileParser.__init__c             c   s    x|   ||¡D ]
}|V  qW dS )z3Parse a given file, yielding parsed lines.
        N)Ú_parse_and_recurse)r&   r,   r$   rJ   r   r   r   r   F  s    zRequirementsFileParser.parsec             c   s¶   x°|   ||¡D ] }|js¨|jjs(|jjr¨|jjrB|jjd }d}n|jjd }d}t |¡rjt ||¡}n t |¡sŠt	j
 t	j
 |¡|¡}x"|  ||¡D ]
}|V  q˜W q|V  qW d S )Nr   FT)Ú_parse_filer/   r.   ÚrequirementsÚconstraintsÚ	SCHEME_REÚsearchÚurllib_parseÚurljoinrV   rW   rZ   rX   rj   )r&   r,   r$   rJ   Úreq_pathÚnested_constraintÚ
inner_liner   r   r   rj   M  s"    

z)RequirementsFileParser._parse_and_recursec             c   s–   t || j| jd\}}t|ƒ}xr|D ]j\}}y|  |¡\}}	W n8 tk
rv }
 zd ||
j¡}t|ƒ‚W d d }
~
X Y nX t	||| j||	|ƒV  q$W d S )N)r"   zInvalid requirement: {}
{})
Úget_file_contentrg   ri   rD   rh   ÚOptionParsingErrorrE   Úmsgr   r+   )r&   r,   r$   Ú_rB   rC   Úline_numberrJ   Úargs_strr.   Úerw   r   r   r   rk   n  s     z"RequirementsFileParser._parse_fileN)r(   r)   r*   r'   r   rj   rk   r   r   r   r   r5   :  s   !r5   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_valuesrR   Úformat_controlÚbreak_args_optionsÚsysÚversion_infoÚencodeÚ
parse_argsÚshlexÚsplit)rJ   r8   Údefaultsrz   Úoptions_strr.   rx   )r2   r   r   Ú
parse_lineŠ  s    

z#get_line_parser.<locals>.parse_liner   )r2   rŒ   r   )r2   r   r4   ˆ  s    r4   c             C   sh   |   d¡}g }|dd… }x8|D ]0}| d¡s8| d¡r<P q | |¡ | d¡ q W d |¡d |¡fS )z½Break 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\   ÚpoprZ   )rJ   Útokensr1   r#   Útokenr   r   r   rƒ   £  s    


rƒ   c               @   s   e Zd Zdd„ ZdS )rv   c             C   s
   || _ d S )N)rw   )r&   rw   r   r   r   r'   ¶  s    zOptionParsingError.__init__N)r(   r)   r*   r'   r   r   r   r   rv   µ  s   rv   c              C   sD   t jdd} tt }x|D ]}|ƒ }|  |¡ qW dd„ }|| _| S )z7
    Return a parser for parsing requirement lines
    F)Úadd_help_optionc             S   s   t |ƒ‚d S )N)rv   )r&   rw   r   r   r   Úparser_exitÉ  s    z!build_parser.<locals>.parser_exit)ÚoptparseÚOptionParserÚSUPPORTED_OPTIONSÚSUPPORTED_OPTIONS_REQÚ
add_optionÚexit)r8   Úoption_factoriesÚoption_factoryÚoptionr”   r   r   r   r€   »  s    
r€   c             c   s    d}g }x~| D ]v\}}|  d¡r*t |¡rlt |¡r<d| }|r`| |¡ |d |¡fV  g }q„||fV  q|st|}| | d¡¡ qW |rœ|d |¡fV  dS )z’Joins 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\   rZ   Ústrip)rC   Úprimary_line_numberÚnew_linery   rJ   r   r   r   r?   Ó  s     

r?   c             c   s8   x2| D ]*\}}t  d|¡}| ¡ }|r||fV  qW dS )z1
    Strips comments and filter empty lines.
    rŸ   N)r¡   Úsubr£   )rC   ry   rJ   r   r   r   r@   ñ  s
    r@   c             c   sT   xN| D ]F\}}x2t  |¡D ]$\}}t |¡}|s2q| ||¡}qW ||fV  qW 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ÚfindallrV   ÚgetenvÚreplace)rC   ry   rJ   Úenv_varÚvar_namerb   r   r   r   rA   ý  s    
rA   c       
   
   C   s$  t | ƒ}|dkr.| | ¡}| ¡  |j|jfS |dkrÂ|rT| d¡rTtd || ¡ƒ‚|  dd¡d }| 	dd¡}t
 |¡}|rœ| d¡d | d	d¡d  }t |¡}| d¡r¾d| d¡ }|} y&t| d
ƒ}t| ¡ ƒ}W dQ R X W n2 tk
r }	 ztd |	¡ƒ‚W d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 {} references URL {}, which is localú:r;   rž   ú/ú|ÚrbNz$Could not open requirements file: {})r
   ÚgetÚraise_for_statusÚurlÚtextr   r   rE   r‰   rª   Ú_url_slash_drive_rer¢   Úgrouprp   ÚunquoteÚlstripÚopenr   ÚreadÚIOError)
r¶   r3   r"   ÚschemeÚresprW   r¢   ÚfrB   Úexcr   r   r   ru     s4    





ru   z/*([a-z])\|)NNNF)N)NNN)NNN)N)WÚ__doc__Ú
__future__r   r•   rV   Úrerˆ   r„   Zpip._vendor.six.moves.urllibr   rp   Zpip._internal.clir   Úpip._internal.exceptionsr   r   Ú!pip._internal.models.search_scoper   Úpip._internal.utils.encodingr   Úpip._internal.utils.typingr	   Úpip._internal.utils.urlsr
   r   Útypingr   r   r   r   r   r   r   r   r   Ú"pip._internal.index.package_finderr   Úpip._internal.network.sessionr   ÚintZReqFileLinesr   Z
LineParserÚ__all__ÚcompileÚIrn   r¡   r§   rR   Úextra_index_urlrS   rm   rl   ÚeditablerN   Ú	no_binaryÚonly_binaryrQ   r^   Útrusted_hostÚalways_unzipr—   Úinstall_optionsÚglobal_optionsÚhashr˜   rH   Úobjectr   r+   r   rD   rM   rf   r6   r5   r4   rƒ   Ú	Exceptionrv   r€   r?   r@   rA   ru   r¸   r   r   r   r   Ú<module>   s‚   ,

!   

.  
.  
)N
-