B
    `(M                 @   s<  d Z ddlZddlZddl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mZmZ ddlmZmZmZ ddlmZ dd	lmZmZ d ZZe
jryddlZW n ek
r   Y nX yddlZW n ek
r   Y nX dddZ dd Z!dd Z"dd Z#G dd de$Z%dS )z=Determining whether files are being measured/reported or not.    N)env)code_object)FileDispositiondisposition_init)TreeMatcherFnmatchMatcherModuleMatcher)prep_patternsfind_python_filescanonical_filename)CoverageException)source_for_filesource_for_morfFc             C   s.   t t| }|ds|r*tj|d }|S )zReturn the canonical path of the module or file `morf`.

    If the module is a package, then return its directory. If it is a
    module, then return its file, unless `directory` is True, in which
    case return its enclosing directory.

    z__init__.pyr   )r   r   endswithospathsplit)Zmorf	directoryZ	morf_path r   X/home/kop/projects/devel/pgwui/test_venv/lib/python3.7/site-packages/coverage/inorout.pycanonical_path*   s    r   c             C   s   |dk	r|j ni }|dkri }|dd}t|tr@|dkr@|S |dd}x:dD ]2}t||rRt||}nqRt|trR|dkrR|S qRW t| }|dk	r|S |S dS )a  Get the name of the module for a filename and frame.

    For configurability's sake, we allow __main__ modules to be matched by
    their importable name.

    If loaded via runpy (aka -m), we can usually recover the "original"
    full dotted module name, otherwise, we resort to interpreting the
    file name to get the module's name.  In the case that the module name
    can't be determined, None is returned.

    N__name____main__
__loader__)fullnamename)	f_globalsget
isinstancestrhasattrgetattrinspectgetmodulename)filenameframemodule_globalsZdunder_nameloaderattrnamer   Zinspectednamer   r   r   name_for_module8   s"    


r)   c             C   s   t | dot| dddkS )z5Is the module object `mod` a PEP420 namespace module?__path____file__N)r    r!   )modr   r   r   module_is_namespaceb   s    r-   c             C   s$   t | dd}|dkrdS tj|S )z8Does the module object `mod` have an existing __file__ ?r+   NF)r!   r   r   exists)r,   Zmod__file__r   r   r   module_has_fileg   s    r/   c               @   sr   e Zd ZdZ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d Zdd Zdd ZdS )InOrOutz0Machinery for determining what files to measure.c             C   sj   || _ || _d | _d | _d  | _| _d  | _| _d  | _| _	g | _
t| _g | _g | _g | _d  | _| _d S )N)warndebugsource_matchsource_pkgs_matchpylib_pathscover_pathspylib_matchcover_matchinclude_match
omit_matchpluginsr   
disp_classsourcesource_pkgssource_pkgs_unmatchedomitinclude)selfr1   r2   r   r   r   __init__r   s    zInOrOut.__init__c       
         sv   j |j  x<|jpg D ].}tj|r< jt| q j | qW  j dd  _t	|j
 _t	|j _t  _|jsxBttttttttfD ]*}|dk	rt|dr jt|dd qW trttdsttjj}|ds jt| ttddg _ t!j"rb j tj# j d d ddl$}ddl%}x"||gD ]} j t| qHW  fdd	} js~ j rg }	 jrt& j _'|	d
( j'  j rt) j  _*|	d( j* |dd#|	  nH j rt& j  _+|d( j+  jr*t& j _,|d( j,  jrNt- j _.|d( j.  jrrt- j _/|d( j/ dS )z7Apply the configuration to get ready for decision-time.Nr+   T)r   <r   testsc                s    j r j |  d S )N)r2   write)msg)rB   r   r   r2      s    z InOrOut.configure.<locals>.debugz
trees {!r}zmodules {!r}zSource matching against z and zCoverage code matching: {!r}zPython stdlib matching: {!r}zInclude matching: {!r}zOmit matching: {!r})0r>   extendr=   r   r   isdirappendr   r?   r	   Zrun_omitr@   Zrun_includerA   setr5   Zcover_pylibatexitr"   platform_pypy_irc_topicre
_structseq	tracebackr    addr   r   Zstructseq_newco_filename
startswithr+   r6   r   ZTESTINGjoin	contractssixr   r3   formatr   r4   r8   r7   r   r9   r:   )
rB   configsrcmZstructseq_filerV   rW   r,   r2   Zagainstr   )rB   r   	configure   s\    
zInOrOut.configureNc          	   C   s  |}t | j|}dd }|dk	rl|jo0|jd}|rlt|}|rl|dsltj|}|tj|krl|}|sz||dS |dr||dS |dr||d	S t	
d
|r||dS |dr|dd d }t|}||_d}	x| jjD ]}	|	jsqyL|	|}
|
dk	rD|	|
_d|_|
|_|
 r4d|_nt|
 |_P W q tk
r|   | d|	j  t  d|	_wY qX qW d|_||_|js|jstd|	|jf | |j|}|r||| |S )a  Decide whether to trace execution in `filename`, with a reason.

        This function is called from the trace function.  As each new file name
        is encountered, this function determines whether it is traced or not.

        Returns a FileDisposition object.

        c             S   s   d| _ || _| S )z+Simple helper to make it easy to return NO.F)tracereason)dispr^   r   r   r   nope   s    z"InOrOut.should_trace.<locals>.nopeNr+   rD   zempty string isn't a file namezmemory:zmemory isn't traceableznot a real file namez[/\\]Modules[/\\]pyexpat.czpyexpat lies about itselfz	$py.classiz.pyTz)Disabling plug-in %r due to an exception:Fz+Plugin %r didn't set source_filename for %r)r   r<   r   r   r   rT   r   r   basenamerO   searchr   r   r;   file_tracersZ_coverage_enabledfile_tracerZ_coverage_pluginr]   Zhas_dynamic_source_filenameZhas_dynamic_filenameZsource_filename	Exceptionr1   _coverage_plugin_namerQ   	print_excr   original_filenamecheck_include_omit_etc)rB   r$   r%   rh   r_   r`   Zdunder_fileorig	canonicalpluginrd   r^   r   r   r   should_trace   sn    	










zInOrOut.should_tracec             C   s   t ||}| js| jr~d}d}| jrV| j|rLd}|| jkrV| j| n
d|}|sp| jrp| j|rpd}|s|d S nD| jr| j|sdS n,| jr| j|rdS | j	r| j	|rdS | j
r| j
|rd	S y|d
 W n tk
r   dS X dS )zCheck a file name against the include, omit, etc, rules.

        Returns a string or None.  String means, don't trace, and is the reason
        why.  None means no reason found to not trace.

         FTzmodule {!r} zfalls outside the --source specz!falls outside the --include treeszis in the stdlibzis part of coverage.pyzis inside an --omit patternutf8znon-encodable filenameN)r)   r3   r4   matchr?   removerX   r9   r7   r8   r:   encodeUnicodeEncodeError)rB   r$   r%   
modulenameextraokr   r   r   ri   ;  s:    




zInOrOut.check_include_omit_etcc             C   s$   | j r | js| jr | jddd dS )z)Warn if there are settings that conflict.z,--include is ignored because --source is setzinclude-ignored)slugN)rA   r=   r>   r1   )rB   r   r   r   warn_conflicting_settingsr  s    z!InOrOut.warn_conflicting_settingsc             C   s   | j s| js| jrt }xhttj D ]V}t|dd}|dkrBq(||krLq(| 	|}|j
r(d|}| j|dd || q(W dS )zCWarn if files have already been imported that we will be measuring.r+   Nz1Already imported a file that will be measured: {}zalready-imported)rw   )rA   r=   r>   rK   listsysmodulesvaluesr!   rm   r]   rX   r1   rR   )rB   warnedr,   r$   r_   rG   r   r   r   warn_already_imported_filesx  s    

z#InOrOut.warn_already_imported_filesc             C   s   x| j D ]}| | qW dS )zCWarn about source packages that were of interest, but never traced.N)r?   _warn_about_unmeasured_code)rB   pkgr   r   r   warn_unimported_source  s    zInOrOut.warn_unimported_sourcec             C   sj   t j|}|dkr*| jd| dd dS t|r6dS t|sT| jd| dd dS | jd| dd dS )	z}Warn about a package or module that we never traced.

        `pkg` is a string, the name of the package or module.

        NzModule %s was never imported.zmodule-not-imported)rw   zModule %s has no Python source.zmodule-not-pythonz3Module %s was previously imported, but not measuredzmodule-not-measured)rz   r{   r   r1   r-   r/   )rB   r   r,   r   r   r   r     s    z#InOrOut._warn_about_unmeasured_codec             c   s   xV| j D ]L}|tjksttj| s&qttj| j}x| t|D ]
}|V  qFW qW x(| jD ]}x| |D ]
}|V  qpW q`W dS )zFind files in the areas of interest that might be untraced.

        Yields pairs: file path, and responsible plug-in name.
        N)	r>   rz   r{   r/   r   r+   _find_executable_filesr   r=   )rB   r   Zpkg_fileretrZ   r   r   r   find_possibly_unexecuted_files  s    
z&InOrOut.find_possibly_unexecuted_filesc             c   s6   x0| j jD ]$}x||D ]}||jfV  qW q
W dS )z&Get executable files from the plugins.N)r;   rc   Zfind_executable_filesrf   )rB   src_dirrl   Zx_filer   r   r   _find_plugin_files  s    zInOrOut._find_plugin_filesc             c   s`   dd t |D }| |}x>t||D ].\}}t|}| jrN| j|rNq*||fV  q*W dS )a(  Find executable files in `src_dir`.

        Search for files in `src_dir` that can be executed because they
        are probably importable. Don't include ones that have been omitted
        by the configuration.

        Yield the file path, and the plugin name that handles the file.

        c             s   s   | ]}|d fV  qd S )Nr   ).0Zpy_filer   r   r   	<genexpr>  s    z1InOrOut._find_executable_files.<locals>.<genexpr>N)r
   r   	itertoolschainr   r:   rp   )rB   r   py_filesZplugin_files	file_pathZplugin_namer   r   r   r     s    

zInOrOut._find_executable_filesc             C   s`   d| j fd| jfg}ddddddg}x6|D ].}t| |}|rF| }nd	}|||f q*W |S )
z^Our information for Coverage.sys_info.

        Returns a list of (key, value) pairs.
        r6   r5   r3   r4   r9   r:   r8   r7   z-none-)r6   r5   r!   inforJ   )rB   r   Zmatcher_namesZmatcher_nameZmatcherZmatcher_infor   r   r   sys_info  s    


zInOrOut.sys_info)N)r   
__module____qualname____doc__rC   r\   rm   ri   rx   r~   r   r   r   r   r   r   r   r   r   r   r0   o   s   M
i7r0   )F)&r   rL   r"   r   r   rM   rO   rz   rQ   Zcoverager   Zcoverage.backwardr   Zcoverage.dispositionr   r   Zcoverage.filesr   r   r   r	   r
   r   Zcoverage.miscr   Zcoverage.pythonr   r   rP   rN   ZPYPYImportErrorr   r)   r-   r/   objectr0   r   r   r   r   <module>   s<   
*