B
    Uvg.                 @   sV  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 ddlmZ dd	lmZmZ d
gZedejZedZedZejejejejej ej!ej"ej#ej$ej%ej&ej'ej(ej)gZ*ej+ej,ej-gZ.dd e.D Z/d"dd
Z0dd Z1d#ddZ2dd Z3dd Z4dd Z5dd Z6dd Z7d d! Z8dS )$z
Requirements file parsing
    )absolute_importN)filterfalse)parse)
cmdoptions)get_file_content)RequirementsFileParseError)install_req_from_editableinstall_req_from_lineparse_requirementsz^(http|https|file):z(^|\s)+#.*$z#(?P<var>\$\{(?P<name>[A-Z0-9_]+)\})c             C   s   g | ]}| j qS  )dest).0or   r   ;/tmp/pip-install-r_9ig3yj/pip/pip/_internal/req/req_file.py
<listcomp>;   s    r   Fc             c   sp   |dkrt dt| ||d\}}t||}	x>|	D ]6\}
}t|| |
||||||d	}x|D ]
}|V  qZW q2W dS )a  Parse a requirements file and yield InstallRequirement instances.

    :param filename:    Path or url of requirements file.
    :param finder:      Instance of pip.index.PackageFinder.
    :param comes_from:  Origin description of requirements.
    :param options:     cli options.
    :param session:     Instance of pip.download.PipSession.
    :param constraint:  If true, parsing a constraint file rather than
        requirements file.
    :param wheel_cache: Instance of pip.wheel.WheelCache
    NzCparse_requirements() missing 1 required keyword argument: 'session')
comes_fromsession)
constraint)	TypeErrorr   
preprocessprocess_line)filenamefinderr   optionsr   r   wheel_cache_content
lines_enumline_numberlinereq_iterreqr   r   r   r
   >   s    


c             C   s6   t |  dd}t|}t|}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)r   r   r   r   r   r   r   _   s    
r   c	          	   c   s  t | }	|	 }
d|
_|r"|j|
_t| \}}tjdk rB|d}|	t	
||
\}}d|rbdnd||f }|r|r||jnd}|rt|| i }x.tD ]&}||jkr|j| r|j| ||< qW t||||||dV  n|jr|r|jnd}t|jd	 ||||d
V  n|js"|jr|jr:|jd	 }d}n|jd	 }d}t|rbt||}n"t|stjtj||}t|||||||d}	x|	D ]}|V  qW n|jr|j|_n|r|jr|jg|_|j dkrg |_|j!r|j"|j! |j#rV|j#d	 }tjtj$|}tj||}tj%|rJ|}|j#&| |j'rdd|_(|j)rrd|_)|j*r|j+"dd |j*D  dS )a#  Process a single 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.

    :param constraint: If True, parsing a constraints file.
    :param options: OptionParser options that we may update
    N)         utf8z%s %s (line %s)z-cz-rF)r   isolatedr   r   r   )r   r   r.   r   T)r   r   c             s   s   | ]}d |d fV  qdS )*Nr   )r   hostr   r   r   	<genexpr>   s    zprocess_line.<locals>.<genexpr>),build_parserget_default_values	index_urlformat_controlbreak_args_optionssysversion_infoencode
parse_argsshlexsplitisolated_moder   check_install_build_globalSUPPORTED_OPTIONS_REQ_DEST__dict__r	   	editablesr   requirementsconstraints	SCHEME_REsearchurllib_parseurljoinospathjoindirnamer
   require_hashes
index_urlsno_indexextra_index_urlsextend
find_linksabspathexistsappendpreallow_all_prereleasesprocess_dependency_linkstrusted_hostssecure_origins)r   r   r   r   r   r   r   r   r   parserdefaultsargs_stroptions_stroptsr   line_comes_fromr.   req_optionsr   req_pathnested_constraintr!   valuereq_dirrelative_to_reqs_filer   r   r   r   m   s    











r   c             C   sh   |  d}g }|dd }x8|D ]0}|ds8|dr<P q || |d q W 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<   
startswithrT   poprJ   )r   tokensargsr   tokenr   r   r   r6      s    


r6   c                sH   t jdd}tt }x|D ]}| }|| qW  fdd}||_|S )z7
    Return a parser for parsing requirement lines
    F)add_help_optionc                s   d |f }t |d S )NzInvalid requirement: %s
%s)r   )selfmsg)r   r   r   parser_exit   s    z!build_parser.<locals>.parser_exit)optparseOptionParserSUPPORTED_OPTIONSSUPPORTED_OPTIONS_REQ
add_optionexit)r   rZ   option_factoriesoption_factoryoptionrp   r   )r   r   r2      s    
r2   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 )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\rf    )endswith
COMMENT_REmatchrT   rJ   strip)r   primary_line_numbernew_liner   r   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   )r   r   r   r   r   r   r'   $  s
    r'   c                s2   |r
|j nd}|r.t| t fdd| } | S )zs
    Skip lines that match '--skip-requirements-regex' pattern

    Note: the regex pattern is only built once
    Nc                s     | d S )Nr"   )rE   )e)patternr   r   <lambda>8      zskip_regex.<locals>.<lambda>)skip_requirements_regexrecompiler   )r   r   r(   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 discusssion 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findallrH   getenvreplace)r   r   r   env_varvar_namerc   r   r   r   r)   <  s    
r)   )NNNNFN)NNNNNF)9__doc__
__future__r   rq   rH   r   r;   r7   Zpip._vendor.six.movesr   Zpip._vendor.six.moves.urllibr   rF   Zpip._internal.clir   pip._internal.downloadr   pip._internal.exceptionsr   pip._internal.req.constructorsr   r	   __all__r   IrD   r}   r   rC   editablerB   rN   r4   rQ   extra_index_urlalways_unzip	no_binaryonly_binaryrU   rW   trusted_hostrL   rs   install_optionsglobal_optionshashrt   r?   r
   r   r   r6   r2   r&   r'   r(   r)   r   r   r   r   <module>   s\   

 
   
q