3 \3p @s(ddgZddlZddlZddlZddlZddlZddlZddlZddlZddl Z ddl Z ddl Z ddl m Zy ddlZWn"ek rejZddZYnXddZd dZd ZGd d d Zd dZddZGdddZddZddZdddZddZGdddZddZedkr$edS)TraceCoverageResultsN) monotoniccCstjddS)N)syssettracerr/usr/lib64/python3.6/trace.py _unsettraceEsr cCstj|tj|dS)N) threadingrr)funcrrr _settraceHs r cCstjdtjddS)N)rrr rrrrr Ls z#pragma NO COVERc@seZdZdddZddZdS)_IgnoreNcCs:|s tnt||_|sgn dd|D|_ddi|_dS)NcSsg|]}tjj|qSr)ospathnormpath).0drrr Usz$_Ignore.__init__..z)set_mods_dirs_ignore)selfmodulesdirsrrr__init__Ss z_Ignore.__init__cCs||jkr|j|S||jkr,d|j|<dSx*|jD] }|j|dr4d|j|<dSq4W|dkrnd|j|<dSx,|jD]"}|j|tjrvd|j|<dSqvWd|j|<dS)Nr.r)rr startswithrrsep)rfilename modulenamemodrrrrnamesYs$          z _Ignore.names)NN)__name__ __module__ __qualname__rr#rrrrr Rs r cCs tjj|}tjj|\}}|S)N)rrbasenamesplitext)rbaser extrrr_modnames r+cCstjj|}d}xJtjD]@}tjj|}|j|r|t|tjkrt|t|kr|}qW|rv|t|dd}n|}tjj|\}}|jtjd}tj r|jtj d}tjj |\}}|j dS)Nrr) rrnormcaserrlenr splitdrivereplacealtsepr(lstrip)rZ comparepathZlongestdirr)Zdriver r*rrr _fullmodnames    r4c@s:eZdZdddZddZddZdd d Zdd d ZdS)rNcCs||_|jdkri|_|jj|_||_|jdkr8i|_|jj|_||_|jdkrZi|_|jj|_||_||_|jry@t|jd}tj |\}}}WdQRX|j |j |||Wn@t t tfk r}ztd|j|ftjdWYdd}~XnXdS)NrbzSkipping counts file %r: %s)file)countscopyZcounter calledfuncscallersinfileoutfileopenpickleloadupdate __class__OSErrorEOFError ValueErrorprintrstderr)rr7r9r;r:r<ferrrrrrs,      zCoverageResults.__init__cCs|jdo|jdS)N<>)rendswith)rr rrris_ignored_filenamesz#CoverageResults.is_ignored_filenamec Csz|j}|j}|j}|j}|j}|j}x$|D]}|j|d||||<q*Wx|D] }d||<qPWx|D] }d||<qfWdS)Nrr)r7r9r:get) rotherr7r9r:Z other_countsZother_calledfuncsZ other_callerskeyrrrr@s    zCoverageResults.updateTFc!Cs|jrDttd|j}x(t|D]\}}}td|||fq$W|jrttdd}} xvt|jD]h\\} } } \} }}| |krttd| d| }d} | | kr| | krtd| | } td| | ||fqlWi}x8|jD].\}}|j|i}||<|j||f||<qWi}x$|jD]\}}|j|r@q&|jdrX|dd}|dkrt j j t j j |}t |}n$|}t j j|st j|t|}|rt|}ni}tj|}t j j||d }t|d }tj|j\}}WdQRX|j|||||\}}|r&|r&td ||}||||f||<q&W|r|rtd x2t|D]&}||\}}}}td||q`W|jry&tj|j|j|jft|jdd Wn6tk r} ztd| tj dWYdd} ~ XnXdS)Nzfunctions called:z*filename: %s, modulename: %s, funcname: %szcalling relationships:r,z***z -->z %s.%s -> %s.%sz.pycrz.coverr5dzlines cov% module (path)z%5d %3d%% %s (%s)wbz"Can't save counts files because %s)r6)!r9rEsortedr:r7rMitemsrLrKrrdirnameabspathr+existsmakedirsr4_find_executable_linenos linecachegetlinesjoinr=tokenizedetect_encodingreadlinewrite_results_fileintr<r>dumprBrrF)!rZ show_missingsummarycoverdirZcallsr r!funcnameZlastfileZ lastcfileZpfileZpmodZpfunccfileZcmodZcfuncZper_filelineno lines_hitZsumscountr3lnotabsourceZ coverpathfpencoding_n_hitsn_linesZpercentmrHrrr write_resultss|              zCoverageResults.write_resultsc Csyt|d|d}Wn6tk rH}ztd||ftjdd Sd}~XnXd}d} |xt|dD]r\} } | |kr|jd|| | d7} |d7}n.| |krt| kr|jd|d7}n |jd |j| jd qdWWdQRX| |fS) Nw)rmz3trace: Could not open %r for writing: %s - skipping)r6rrz%5d: z>>>>>> z )rr) r=rBrErrF enumeratewritePRAGMA_NOCOVER expandtabs) rrlinesrjrhrmr<rHrprorglinerrrr`5s(    z"CoverageResults.write_results_file)NNNNN)TFN)N)r$r%r&rrLr@rrr`rrrrrs   [cCs0i}x&tj|D]\}}||krd||<qW|S)Nr)disZfindlinestarts)codestrslinenosrnrgrrr_find_lines_from_codeUs  rcCs8t||}x(|jD]}tj|r|jt||qW|S)N)r co_constsinspectZiscoder@ _find_lines)r|r}r~crrrr_s    rc Csi}tj}t||dr}tj|j}x^|D]V\}}}} } |tjkr||tjkr||\} } | \} }xt| | dD] }d||<qlW|}q*WWdQRX|S)N)rmr)tokenINDENTr=r]generate_tokensr_STRINGrange)r rmrZ prev_ttyperGtokZttypeZtstrstartendrzZslineZscolZelineZecolirrr _find_stringsks    rcCsy(tj|}|j}|j}WdQRXWn6tk r^}ztd||ftjdiSd}~XnXt||d}t ||}t ||S)Nz%Not printing coverage data for %r: %s)r6exec) r]r=readrmrBrErrFcompilerr)r rGprogrmrHr|r}rrrrYs   rYc @seZdZddddffdddf ddZddZdd d Zd d Zd dZddZddZ ddZ ddZ ddZ ddZ ddZdS)rrrNFc Cs||_||_t|||_i|_i|_d|_||_i|_i|_ i|_ d|_ | rTt |_ |rb|j |_nZ|rp|j|_nL|r|r|j|_|j|_n2|r|j|_|j|_n|r|j|_|j|_nd|_dS)Nrr)r;r<r ignorer7Zpathtobasename donothingtrace _calledfuncs_callers _caller_cache start_time_timeglobaltrace_trackcallers globaltraceglobaltrace_countfuncsglobaltrace_ltlocaltrace_trace_and_count localtracelocaltrace_tracelocaltrace_count) rrir countfuncs countcallers ignoremods ignoredirsr;r<timingrrrrs6      zTrace.__init__cCs ddl}|j}|j|||dS)Nr)__main____dict__runctx)rcmdrdictrrrrunsz Trace.runc CsL|dkr i}|dkri}|js(t|jzt|||Wd|jsFtXdS)N)rr rrr )rrglobalslocalsrrrrs z Trace.runctxc Os<d}|jstj|jz|||}Wd|js6tjdX|S)N)rrrr)rr argskwresultrrrrunfuncs  z Trace.runfuncc Cs|j}|j}|rt|}nd}|j}d}||jkrL|j|dk r|j|}nd|j|<ddtj|D}t|dkrddtj|dD}t|dkrddtj|dD} t| dkr| dj}||j|<|dk rd||f}|||fS)NcSsg|]}tj|r|qSr)rZ isfunction)rrGrrrrsz1Trace.file_module_function_of..rcSsg|]}t|tr|qSr) isinstancer)rrrrrrsrcSsg|]}t|dr|qS) __bases__)hasattr)rrrrrrsz%s.%s) f_code co_filenamer+co_namergcZ get_referrersr.r$) rframer|r r!reZclsnameZfuncsZdictsclassesrrrfile_module_function_ofs,          zTrace.file_module_function_ofcCs0|dkr,|j|}|j|j}d|j||f<dS)Ncallr)rf_backr)rrwhyarg this_funcZ parent_funcrrrrs  zTrace.globaltrace_trackcallerscCs |dkr|j|}d|j|<dS)Nrr)rr)rrrrrrrrrs zTrace.globaltrace_countfuncscCsj|dkrf|j}|jjdd}|rbt|}|dk rf|jj||}|sf|jrZtd||jf|j SndSdS)Nr__file__z! --- modulename: %s, funcname: %s) r f_globalsrMr+rr#rrErr)rrrrr|r r!Z ignore_itrrrrszTrace.globaltrace_ltcCs|dkr~|jj}|j}||f}|jj|dd|j|<|jrTtdt|jddtj j |}td||t j ||fdd|j S) Nrzrrz%.2f )rz %s(%d): %sr,)rrf_linenor7rMrrErrrr'rZgetliner)rrrrr rgrObnamerrrr2s z Trace.localtrace_trace_and_countcCsd|dkr^|jj}|j}|jr4tdt|jddtjj|}td||t j ||fdd|j S)Nrzz%.2fr)rz %s(%d): %sr,) rrrrrErrrr'rZrr)rrrrr rgrrrrrAs zTrace.localtrace_tracecCs<|dkr6|jj}|j}||f}|jj|dd|j|<|jS)Nrzrr)rrrr7rMr)rrrrr rgrOrrrrNs zTrace.localtrace_countcCst|j|j|j|j|jdS)N)r;r<r9r:)rr7r;r<rr)rrrrresultsVs z Trace.results)NN)r$r%r&rrrrrrrrrrrrrrrrrs 0  )   c stj}|jdddd|jdd}|jddd d d |jd d d dd |jddd dd |jddd dd |jd}|j}|jddd dd |jddd dd |jdddd|jd d!d"d|jd#d$d d%d |jd&d'd d(d |jd)d*d d+d |jd,d-}|jd.d/gd0d1|jd2d/gd3d1|jd4d5d6d7|jd8tjd9d7|j}|jrd:d;jt j f}t j j t jf|t j j t jf|fdd?|jD|_fd@d?|jD|_|jr|js|jdAt|j|jdB}|j|j|j|jSt|j|j|j|jgs*|jdC|jrL|jsB|jrL|jdD|jrh|j rh|jdE|jdkr~|jdF|jf|jt _ t j j!|jt j dG<t"|j|j|j|j|j|j|j|j|j#dH }yJt$|j}t%|j&|jdI}WdQRX|jdJdddK} |j'|| | WnPt(k rT} zt j)dLt j dG| fWYdd} ~ Xnt*k rhYnX|j+}|j,s|j|j|j|jdS)MNz --versionversionz trace 2.0)actionrz Main optionsz(One of these (or --report) must be givenz-cz--count store_truezCount the number of times each line is executed and write the counts to .cover for each module executed, in the module's directory. See also --coverdir, --file, --no-report below.)rhelpz-tz--tracez3Print each line to sys.stdout before it is executedz-lz --listfuncszKeep track of which functions are executed at least once and write the results to sys.stdout after the program exits. Cannot be specified alongside --trace or --count.z-Tz --trackcallsz^Keep track of caller/called pairs and write the results to sys.stdout after the program exits.Z Modifiersz-rz--reportzGenerate a report from a counts file; does not execute any code. --file must specify the results file to read, which must have been created in a previous run with --count --file=FILEz-Rz --no-reportz^Do not generate the coverage report files. Useful if you want to accumulate over several runs.z-fz--filez+File to accumulate counts over several runs)rz-Cz --coverdirzDirectory where the report files go. The coverage report for . will be written to file //.coverz-mz --missingz?Annotate executable lines that were not executed with ">>>>>> "z-sz --summaryz\Write a brief summary for each file to sys.stdout. Can only be used with --count or --reportz-gz--timingzQPrefix each line with the time since the program started. Only used while tracingZFilterszCan be specified multiple timesz--ignore-moduleappendzqIgnore the given module(s) and its submodules (if it is a package). Accepts comma separated list of module names.)rdefaultrz --ignore-dirzWIgnore files in the given directory (multiple directories can be joined by os.pathsep).r ?zfile to run as main program)nargsr argumentszarguments to the programlibzpython{0.major}.{0.minor}cs4tjjtjj|}|jdjd}tjj|S)Nz$prefixz $exec_prefix)rr expanduser expandvarsr0r)s) _exec_prefix_prefixrrparse_ignore_dirszmain..parse_ignore_dircSs$g|]}|jdD] }|jqqS),)splitstrip)rrr"rrrrszmain..cs&g|]}|jtjD] }|qqSr)rrpathsep)rrr)rrrrsz-r/--report requires -f/--file)r;r<zLmust specify one of --trace, --count, --report, --listfuncs, or --trackcallsz8cannot specify both --listfuncs and (--trace or --count)z3--summary can only be used with --count or --reportz3filename is missing: required with the main optionsr)rrrrr;r<rrr)rr$ __package__ __cached__zCannot run file %r because: %s)-argparseArgumentParser add_argumentZadd_argument_groupZadd_mutually_exclusive_groupZ REMAINDER parse_argsZ ignore_dirformatr version_inforrr\ base_prefixbase_exec_prefixZ ignore_moduleZreportr6errorrrrZmissingrcrdanyrriZ listfuncsZ trackcallsr rargvrUrrr=rrrrBexit SystemExitrZ no_report) parserZgrpZ_grpZoptsZrel_pathrtrlr|ZglobsrHr)rrrrmain\s                       (rr)N) __all__rrZrrerrr]rrr{r>Ztimerrr ImportErrorrr r rwr r+r4rrrrrYrrr$rrrr2sF  2/  M