U
    mfH                     @   s   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d
dZdd ZdddZdd Zdd Zdd Zdd Zdd Zdd ZdS )    N)Path)run)appsget_random_string)DEFAULT_LOCALE_ENCODING   )CommandErrorCommandParserutf-8c              
   C   sn   zt | dtjdkd}W n4 tk
rL } ztd| d  |W 5 d}~X Y nX |j||jjtdd|j	fS )	zf
    Friendly wrapper around Popen.

    Return stdout output, stderr output, and OS status code.
    Tnt)capture_output	close_fdszError executing %sr   Nreplace)errors)
r   osnameOSErrorr	   stdoutdecodestderrr   
returncode)argsZstdout_encodingperr r   @/tmp/pip-unpacked-wheel-siwebuq3/django/core/management/utils.pypopen_wrapper   s    $
r   c                 C   sZ   g }| D ]}| |ddd qt|D ]"\}}|ds.d||  ||< q.t|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ddt j}t|tr.|g}|d krLt jddt j}|D ]}| |rPdg} qjqP|D ]P}t j	|| }t j
|r|  S |D ]$}|| }t j
|r|    S qqnd S )NPATHr   PATHEXTz.COM;.EXE;.BAT;.CMD)r   environgetr#   pathsep
isinstancestrendswithpathjoinisfile)cmdr3   pathextr(   r   fZfextr   r   r   find_command6   s&    

r9   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_keyO   s    r<   c                 C   s   t  }t  }| D ]}d|krXzt|}W n  tk
rJ   td| Y nX || qzt|}W n. tk
r } ztt|W 5 d}~X Y nX || q||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_configr1   )labelsr   modelslabelmodelZ
app_configer   r   r   parse_apps_and_model_labelsW   s    rE   c                 C   sV   t ddd}|j|dd z|| dd \}}W n tk
rJ   Y dS X |jS d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.
    F)add_helpallow_abbrevvalue)dest   N)r
   add_argumentparse_known_argsr	   rH   )argvoptionparseroptions_r   r   r   get_command_line_optionr   s    rR   c                 C   sn   dd | D } dd dt jhD }g }| D ]>}|D ]*}||r2||dt|    q*q2|| q*|S )z9Normalize an iterable of glob style patterns based on OS.c                 S   s   g | ]}t j|qS r   )r   r3   normcase).0r   r   r   r   
<listcomp>   s     z+normalize_path_patterns.<locals>.<listcomp>c                 S   s   h | ]}d | qS )z%s*r   )rT   path_sepr   r   r   	<setcomp>   s     z*normalize_path_patterns.<locals>.<setcomp>/N)r   sepr2   appendlen)patternsZdir_suffixesZnorm_patternspatternZ
dir_suffixr   r   r   normalize_path_patterns   s    
r^   c                    s.   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| pt t | S N)fnmatchfnmatchcaser   r1   )r]   )r3   r   r   ignore   s     zis_ignored_path.<locals>.ignorec                 3   s   | ]} |V  qd S r_   r   )rT   r]   )rb   r   r   	<genexpr>   s     z"is_ignored_path.<locals>.<genexpr>)r   anyr^   )r3   ignore_patternsr   )rb   r3   r   is_ignored_path   s    rf   c                 C   s*   t d }r&tj|ddf| dd dS )z9
    Run the black formatter on the specified files.
    Zblackz--fastz--T)r   N)shutilwhich
subprocessr   )Zwritten_filesZ
black_pathr   r   r   run_formatters   s
    rj   )r   )NN)r`   r   rg   ri   pathlibr   r   Zdjango.appsr   r=   Zdjango.utils.cryptor   Zdjango.utils.encodingr   baser	   r
   r   r*   r9   r<   rE   rR   r^   rf   rj   r   r   r   r   <module>   s$   

