B
    =cH\                 @  sR  U 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	Z	ddl
Z
ddlZddlZddlmZ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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dl.m/Z/m0Z0m1Z1m2Z2 er<ddl3m4Z4 ddl5m6Z6 eeeee	eegZ7de8d< ej9ryddl:Z:e7;e: W n e<k
r   Y nX yddl=Z=e7;e= W n e<k
r   Y nX d2ddddddZ>ddddddZ?ddddd Z@dddd!d"ZAdd#d$d%d&ZBd'd(d)d*d+ZCd'd(d)d,d-ZDd'd(d)d.d/ZEG d0d1 d1ZFdS )3z=Determining whether files are being measured/reported or not.    )annotationsN)	FrameType
ModuleType)	castAnyIterableListOptionalSetTupleTypeTYPE_CHECKING)env)FileDispositiondisposition_init)CoverageExceptionPluginError)TreeMatcherGlobMatcherModuleMatcher)prep_patternsfind_python_filescanonical_filename)sys_modules_saved)source_for_filesource_for_morf)TFileDispositionTMorfTWarnFn	TDebugCtl)CoverageConfig)PluginszList[ModuleType]modules_we_happen_to_haveFr   boolstr)morf	directoryreturnc             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)r%   r&   Z	morf_path r,   A/var/www/cleansys/lib/python3.7/site-packages/coverage/inorout.pycanonical_path;   s    r.   zOptional[FrameType])filenameframer'   c             C  s   |dk	r|j ni }|dd}t|tr4|dkr4|S |dd}x:dD ]2}t||rFt||}nqFt|trF|dkrF|S qFW 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
isinstancer$   hasattrgetattrinspectgetmodulename)r/   r0   module_globalsZdunder_nameloaderattrnamer4   Zinspectednamer,   r,   r-   name_for_moduleI   s    


r@   r   )modr'   c             C  s   t | dot| dddkS )z5Is the module object `mod` a PEP420 namespace module?__path____file__N)r9   r:   )rA   r,   r,   r-   module_is_namespaceo   s    rD   c             C  s$   t | dd}|dkrdS tj|S )z8Does the module object `mod` have an existing __file__ ?rC   NF)r:   r)   r*   exists)rA   Zmod__file__r,   r,   r-   module_has_filet   s    rF   zTuple[Optional[str], List[str]])
modulenamer'   c             C  sR   d}g }yt j| }W n tk
r,   Y nX |dk	rJ|j}t|jpFd}||fS )zFind the file and search path for `modulename`.

    Returns:
        filename: The filename of the module, or None.
        path: A list (possibly empty) of directories to find submodules in.

    Nr,   )	importlibutil	find_spec	Exceptionoriginlistsubmodule_search_locations)rG   r/   r*   specr,   r,   r-   file_and_path_for_module|   s    rP   zSet[str]None)pathsr'   c             C  s.   x(t D ] }t|dr| t|dd qW dS )z;Add paths where the stdlib can be found to the set `paths`.rC   T)r&   N)r"   r9   addr.   )rR   mr,   r,   r-   add_stdlib_paths   s    

rU   c             C  sd   t t }xR|D ]J}|dkr"dn|}tj|dkrt|}xdD ]}| ||  qFW qW dS )z:Add locations for third-party packages to the set `paths`.pypyZ
pypy_posix_)platlibpurelibscriptsN)set	sysconfigget_scheme_namesr)   r5   r+   	get_pathsrS   )rR   Zscheme_namesschemeZbetter_schemeZconfig_paths	path_namer,   r,   r-   add_third_party_paths   s    


ra   c             C  s4   t tdd}| | tjr0| tj|d dS )zAAdd paths where coverage.py code can be found to the set `paths`.T)r&   testsN)r.   rC   rS   r   ZTESTINGr)   r*   join)rR   Z
cover_pathr,   r,   r-   add_coverage_paths   s    
rd   c               @  s   e Zd ZdZdddddddd	Zd*ddddddZddddddZddddZddddZddddZ	dddddZ
dddd Zdd!d"d#d$Zddd"d%d&Zd'dd(d)Zd
S )+InOrOutz0Machinery for determining what files to measure.r    r   zOptional[TDebugCtl]r#   rQ   )configwarndebuginclude_namespace_packagesr'   c               s  | _ | _| _g  _g  _ j|j x<|jp6g D ].}tj|rZ j	t
| q8 j	| q8W  jd d   _t|j _t|j _t  _|jst j t  _t j t  _t j ddd fdd}|dddd	 tjD   d  _d  _d  _d   _ _ js. jrg } jrZt  jd
 _|	d j  jrt! jd _|	d j |dd|  n& jrt  jd _|d j  jrt" jd _|d j  jrt" jd _|d j t  jd _#|d j# t  jd _$|d j$ d _%t&  x jD ]}y$t'|\}	}
|d|d|	 W n< t(k
r } z|d|d|  wZW d d }~X Y nX |	r j$)|	r.|d|d |	 d! _%n>x<|
D ]4} j$)|r|d"|d#d$|  d! _%qW qZW W d Q R X x2 jD ](} j$)|rF|d%| d! _%qFW   t* _+d S )&Nr$   rQ   )msgr'   c               s    j r j |  d S )N)rh   write)rj   )selfr,   r-   _debug   s    z InOrOut.__init__.<locals>._debugz	sys.path: c             s  s   | ]}d | V  qdS )z
    Nr,   ).0pr,   r,   r-   	<genexpr>   s    z#InOrOut.__init__.<locals>.<genexpr>sourceztrees source_pkgszmodules zSource matching against z and ZpylibzPython stdlib matching: includezInclude matching: omitzOmit matching: coveragezCoverage code matching: thirdzThird-party lib matching: FzImported source package z as zCouldn't import source package z: z/Source is in third-party because of source_pkg z at Tz$Source is in third-party because of z path directory zat z5Source is in third-party because of source directory ),rg   rh   ri   rr   rs   extendr)   r*   isdirappendr   source_pkgs_unmatchedr   Zrun_includert   Zrun_omitru   r[   pylib_pathsZcover_pylibrU   cover_pathsrd   third_pathsra   rc   syssource_matchsource_pkgs_matchpylib_matchinclude_match
omit_matchr   r   r   cover_matchthird_matchsource_in_thirdr   rP   r   matchr   
disp_class)rl   rf   rg   rh   ri   srcrm   ZagainstpkgZmodfiler*   excZpathdirr,   )rl   r-   __init__   s    




zInOrOut.__init__Nr$   zOptional[FrameType]r   )r/   r0   r'   c          
   C  s  |}t | j|}dddddd}|dr6||dS |dk	r|joN|jd	}|rt|}|r|dstj|}|tj|kr|}|s||d
S |dr||dS |dr||dS t	|}||_	d}	x| j
jD ]}	|	jsqyL|	|}
|
dk	r2|	|
_d|_|
|_|
 r"d|_nt	|
 |_P W q tk
rv   |	j}t }| d|d|  d|	_wY qX qW d|_||_|js|jstd|	d|j d| |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.

        r   r$   )dispreasonr'   c             S  s   d| _ || _| S )z+Simple helper to make it easy to return NO.F)tracer   )r   r   r,   r,   r-   nope5  s    z"InOrOut.should_trace.<locals>.nope<znot a real original file nameNrC   zempty string isn't a file namezmemory:zmemory isn't traceableznot a real file nameTzDisabling plug-in z due to an exception:
FzPlugin z! didn't set source_filename for '')r   r   
startswithr6   r7   r   r)   r*   basenamer   pluginsfile_tracersZ_coverage_enabledfile_tracerZ_coverage_pluginr   Zhas_dynamic_source_filenamehas_dynamic_filenameZsource_filenamerK   _coverage_plugin_name	traceback
format_excrg   r   original_filenamecheck_include_omit_etc)rl   r/   r0   r   r   r   Zdunder_fileorig	canonicalpluginr   plugin_nametbr   r,   r,   r-   should_trace)  sh    	










zInOrOut.should_tracezOptional[str]c             C  s$  t ||}| js| jrd}d}| jrX| j|rLd}|| jkrX| j| nd|d}|sr| jrr| j|rrd}|s~|d S | js| j|rdS nN| jr| j|sdS n6| j	|rd	S | j
r| j
|rd
S | 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.

        rn   FTzmodule  zfalls outside the --source specz#inside --source, but is third-partyz!falls outside the --include treeszis part of coverage.pyzis in the stdlibzis a third-party modulezis inside an --omit patternzutf-8znon-encodable filenameN)r@   r   r   r   r{   remover   r   r   r   r   r   encodeUnicodeEncodeError)rl   r/   r0   rG   extraokr,   r,   r-   r     sD    


zInOrOut.check_include_omit_etc)r'   c             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)rt   rr   rs   rg   )rl   r,   r,   r-   warn_conflicting_settings  s    z!InOrOut.warn_conflicting_settingsc             C  s   | j s| js| jrt }xttj D ]}t|dd}|dkrBq(||krLq(t	t|dddkrbq(| 
|}|jrtq(|jrd| }| j|dd || q(| jr(| jd	r(| jd
|j|j q(W dS )zCWarn if files have already been imported that we will be measuring.rC   NrB   r,      z/Already imported a file that will be measured: zalready-imported)r   r   z+Didn't trace already imported file {!r}: {})rt   rr   rs   r[   rM   r   modulesvaluesr:   lenr   r   r   rg   rS   rh   Zshouldrk   formatr   r   )rl   warnedrA   r/   r   rj   r,   r,   r-   warn_already_imported_files  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)rl   r   r,   r,   r-   warn_unimported_source  s    zInOrOut.warn_unimported_source)r   r'   c             C  sz   t j|}|dkr.| jd| ddd dS t|r:dS t|s\| jd| ddd dS d| d}| j|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 z was never imported.zmodule-not-imported)r   z has no Python source.zmodule-not-pythonz* was previously imported, but not measuredzmodule-not-measured)r   r   r7   rg   rD   rF   )rl   r   rA   rj   r,   r,   r-   r     s    z#InOrOut._warn_about_unmeasured_codez#Iterable[Tuple[str, Optional[str]]]c             c  sx   xR| j D ]H}|tjksttj| s&qttttj| j}| t	|E dH  qW x| j
D ]}| |E dH  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)rs   r   r   rF   r   r   r$   rC   _find_executable_filesr.   rr   )rl   r   Zpkg_filer   r,   r,   r-   find_possibly_unexecuted_files  s    
z&InOrOut.find_possibly_unexecuted_fileszIterable[Tuple[str, str]])src_dirr'   c             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   r   Zfind_executable_filesr   )rl   r   r   Zx_filer,   r,   r-   _find_plugin_files  s    zInOrOut._find_plugin_filesc             c  sd   dd t || jD }| |}x>t||D ].\}}t|}| jrR| j|rRq.||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,   )ro   Zpy_filer,   r,   r-   rq   -  s    z1InOrOut._find_executable_files.<locals>.<genexpr>N)r   ri   r   	itertoolschainr   r   r   )rl   r   py_filesZplugin_files	file_pathr   r,   r,   r-   r   "  s    
zInOrOut._find_executable_fileszIterable[Tuple[str, Any]]c             C  sj   d| j fd| jfd| jfg}dddddd	d
g}x6|D ].}t| |}|rP| }nd}|||f q4W |S )z^Our information for Coverage.sys_info.

        Returns a list of (key, value) pairs.
        Zcoverage_pathsZstdlib_pathsZthird_party_pathsr   r   r   r   r   r   r   z-none-)r}   r|   r~   r:   inforz   )rl   r   Zmatcher_namesZmatcher_nameZmatcherZmatcher_infor,   r,   r-   sys_info:  s    



zInOrOut.sys_info)N)r1   
__module____qualname____doc__r   r   r   r   r   r   r   r   r   r   r   r,   r,   r,   r-   re      s   ra> re   )F)Gr   
__future__r   importlib.utilrH   r;   r   r)   platformrer   r\   r   typesr   r   typingr   r   r   r   r	   r
   r   r   r   rv   r   Zcoverage.dispositionr   r   Zcoverage.exceptionsr   r   Zcoverage.filesr   r   r   r   r   r   Zcoverage.miscr   Zcoverage.pythonr   r   Zcoverage.typesr   r   r   r   Zcoverage.configr    Zcoverage.plugin_supportr!   r"   __annotations__ZPYPYZ
_structseqrz   ImportErrorZ_pypy_irc_topicr.   r@   rD   rF   rP   rU   ra   rd   re   r,   r,   r,   r-   <module>   sX   ,&	