
v^	                 @   s   d  d l  Z  d  d l 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 m Z d	 d
 d  Z d d   Z d d d d  Z d d   Z d d   Z d d   Z d d   Z d d   Z d S)    N)Path)PIPErun)apps)get_random_string)DEFAULT_LOCALE_ENCODING   )CommandErrorCommandParserzutf-8c             C   s   y+ t  |  d t d t d t j d k } Wn< t k
 ri } z t d |  d  |  WYd d } ~ Xn X| j j |  | j j t	 d d	 | j
 f S)
zf
    Friendly wrapper around Popen.

    Return stdout output, stderr output, and OS status code.
    stdoutstderr	close_fdsntzError executing %sr   Nerrorsreplace)r   r   osnameOSErrorr	   r   decoder   r   
returncode)argsZstdout_encodingperr r   >/tmp/pip-build-8lau8j11/django/django/core/management/utils.pypopen_wrapper   s    +*r   c             C   s   g  } x0 |  D]( } | j  | j d d  j d   q Wx; t |  D]- \ } } | j d  sF d | | | | <qF Wt |  S)a  
    Organize multiple extensions that are separated with commas or passed by
    using --extension/-e multiple times.

    For example: running 'django-admin makemessages -e js,txt -e xhtml -a'
    would result in an extension list: ['.js', '.txt', '.xhtml']

    >>> handle_extensions(['.html', 'html,js,py,py,py,.py', 'py,.py'])
    {'.html', '.js', '.py'}
    >>> handle_extensions(['.html, txt,.tpl'])
    {'.html', '.tpl', '.txt'}
      ,.z.%s)extendr   split	enumerate
startswithset)
extensionsZext_listextir   r   r   handle_extensions   s    &r(   c             C   s  | d  k r- t  j j d d  j t  j  } t | t  rE | g } | d  k rr t  j j d d  j t  j  } x' | D] } |  j |  ry d g } Pqy Wxj | D]b } t  j j	 | |   } t  j j
 |  r | Sx. | D]& } | | } t  j j
 |  r | Sq Wq Wd  S)NPATHr   PATHEXTz.COM;.EXE;.BAT;.CMD)r   environgetr!   pathsep
isinstancestrendswithpathjoinisfile)cmdr1   pathextr&   r   fZfextr   r   r   find_command4   s&    !	!	
r7   c              C   s   d }  t  d |   S)zS
    Return a 50 character random string usable as a SECRET_KEY setting value.
    z2abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)2   )r   )charsr   r   r   get_random_secret_keyM   s    r:   c             C   s   t    } t    } x |  D] } d | k rs y t j |  } Wn" t k
 rb t d |   Yn X| j |  q y t j |  } Wn7 t k
 r } z t t |    WYd d } ~ Xn X| j |  q W| | f S)z
    Parse a list of "app_label.ModelName" or "app_label" strings into actual
    objects and return a two-element tuple:
        (set of model classes, set of app_configs).
    Raise a CommandError if some specified models or apps don't exist.
    r   zUnknown model: %sN)r$   installed_appsZ	get_modelLookupErrorr	   addZget_app_configr/   )labelsr   modelslabelmodelZ
app_configer   r   r   parse_apps_and_model_labelsU   s    		%rC   c             C   so   t  d d d d  } | j | d d y# | j |  d d   \ } } Wn t k
 rc d SYn X| j Sd S)z
    Return the value of a command line option (which should include leading
    dashes, e.g. '--testrunner') from an argument list. Return None if the
    option wasn't passed or if the argument list couldn't be parsed.
    add_helpFZallow_abbrevdestvalue   N)r
   add_argumentZparse_known_argsr	   rF   )argvoptionparseroptions_r   r   r   get_command_line_optionp   s    #	rN   c             C   s   d d   |  D }  d d   d t  j h D } g  } xZ |  D]R } xI | D]4 } | j |  rI | j | d t |    PqI W| j |  q< W| S)z9Normalize an iterable of glob style patterns based on OS.c             S   s"   g  |  ] } t  j j |   q Sr   )r   r1   normcase).0r   r   r   r   
<listcomp>   s   	 z+normalize_path_patterns.<locals>.<listcomp>c             S   s   h  |  ] } d  |  q S)z%s*r   )rP   path_sepr   r   r   	<setcomp>   s   	 z*normalize_path_patterns.<locals>.<setcomp>/N)r   sepr0   appendlen)patternsZdir_suffixesZnorm_patternspatternZ
dir_suffixr   r   r   normalize_path_patterns   s    rZ   c                sA   t      f d d     t   f d d   t |  D  S)zy
    Check if the given path should be ignored or not based on matching
    one of the glob style `ignore_patterns`.
    c                s+   t  j   j |   p* t  j t    |   S)N)fnmatchfnmatchcaser   r/   )rY   )r1   r   r   ignore   s    zis_ignored_path.<locals>.ignorec             3   s   |  ] }   |  Vq d  S)Nr   )rP   rY   )r]   r   r   	<genexpr>   s    z"is_ignored_path.<locals>.<genexpr>)r   anyrZ   )r1   ignore_patternsr   )r]   r1   r   is_ignored_path   s    ra   )r[   r   Zpathlibr   
subprocessr   r   Zdjango.appsr   r;   Zdjango.utils.cryptor   Zdjango.utils.encodingr   baser	   r
   r   r(   r7   r:   rC   rN   rZ   ra   r   r   r   r   <module>   s   