YfA{ @s@dZddgZddlZddlZddlZddlZddlZddlZddlZddl Z ddl Z ddl Z ddl m ZddlmZyddlZWn'ek rejZddZYnXd d Zd dZd d ZdZejdZGdddZddZddZGdddZddZddZ dddZ!ddZ"GdddZ#d d!Z$dd"d#Z%d$d%Z&Gd&d'd'eZ'd(d)Z(d*d+Z)d,d-Z*d.d/Z+dd0d1Z,d2d3Z-e.d4kr<e%dS)5aprogram/module to trace Python program or function execution Sample use, command line: trace.py -c -f counts --ignore-dir '$prefix' spam.py eggs trace.py -t --ignore-dir '$prefix' spam.py eggs trace.py --trackcalls spam.py eggs Sample use, programmatically import sys # create a Trace object, telling it what to ignore, and whether to # do tracing or line-counting or both. tracer = trace.Trace(ignoredirs=[sys.base_prefix, sys.base_exec_prefix,], trace=0, count=1) # run the new command using the given tracer tracer.run('main()') # make a report, placing output in /tmp r = tracer.results() r.write_results(show_missing=True, coverdir="/tmp") TraceCoverageResultsN)warn) monotoniccCstjddS)N)syssettracerr*/opt/alt/python35/lib64/python3.5/trace.py _unsettraceEsr cCstj|tj|dS)N) threadingrr)funcrrr _settraceHs r cCstjdtjddS)N)rrr rrrr r Ls cCs|jdtjddS)Na Usage: %s [OPTIONS] [ARGS] Meta-options: --help Display this help then exit. --version Output version information then exit. Otherwise, exactly one of the following three options must be given: -t, --trace Print each line to sys.stdout before it is executed. -c, --count Count 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. -l, --listfuncs Keep track of which functions are executed at least once and write the results to sys.stdout after the program exits. -T, --trackcalls Keep track of caller/called pairs and write the results to sys.stdout after the program exits. -r, --report Generate a report from a counts file; do not execute any code. `--file' must specify the results file to read, which must have been created in a previous run with `--count --file=FILE'. Modifiers: -f, --file= File to accumulate counts over several runs. -R, --no-report Do not generate the coverage report files. Useful if you want to accumulate over several runs. -C, --coverdir= Directory where the report files. The coverage report for . is written to file //.cover. -m, --missing Annotate executable lines that were not executed with '>>>>>> '. -s, --summary Write a brief summary on stdout for each file. (Can only be used with --count or --report.) -g, --timing Prefix each line with the time since the program started. Only used while tracing. Filters, may be repeated multiple times: --ignore-module= Ignore the given module(s) and its submodules (if it is a package). Accepts comma separated list of module names --ignore-dir= Ignore files in the given directory (multiple directories can be joined by os.pathsep). r)writerargv)outfilerrr _usagePs*rz#pragma NO COVERz ^\s*(#.*)?$c@s.eZdZddddZddZdS)_IgnoreNcCsS|stn t||_|s*gndd|D|_ddi|_dS)NcSs"g|]}tjj|qSr)ospathnormpath).0drrr s z$_Ignore.__init__..z)set_mods_dirs_ignore)selfmodulesdirsrrr __init__s z_Ignore.__init__cCs||jkr|j|S||jkr:d|j|r@r?rferrrrr r!s,          zCoverageResults.__init__cCs|jdo|jdS)z_Return True if the filename does not refer to a file we want to have reported. <>)r#endswith)rr%rrr is_ignored_filenamesz#CoverageResults.is_ignored_filenamec Cs|j}|j}|j}|j}|j}|j}x,|D]$}|j|d||||r?get) rotherr<r>r?Z other_countsZother_calledfuncsZ other_callerskeyrrr rDs       "  zCoverageResults.updateTFc!Cs|jrZttd|j}x4t|D]&\}}}td|||fq0W|jr$ttdd}} xt|jD]\\} } } \} }}| |krttd| d| }d} | | kr| | krtd| | } td| | ||fqWi}xH|jD]=\}}|j|i}||<|j||f||z %s.%s -> %s.%sz.pycNrz.coverr:dzlines cov% module (path)z%5d %3d%% %s (%s)wbz"Can't save counts files because %sr;)!r>rIsortedr?r<rQitemsrPrOrrdirnameabspathr0existsmakedirsr9_find_executable_linenos linecachegetlinesjoinrAtokenizedetect_encodingreadlinewrite_results_fileintrrBdumprFrrJ)!rZ show_missingsummarycoverdirZcallsr%r&funcnameZlastfileZ lastcfileZpfileZpmodZpfunccfileZcmodZcfuncZper_filelineno lines_hitZsumscountr8lnotabsourceZ coverpathfpencoding_n_hitsn_linesZpercentmrLrrr write_results s~      +          zCoverageResults.write_resultsc CsRyt|dd|}WnFtk ra}z&td||fdtjd SWYdd}~XnXd}d} |xt|dD]\} } | |kr|jd|| | d7} |d7}n^tj| r|jd n?| |krt | kr|jd |d7}n |jd |j| j d qWWdQRX| |fS) z'Return a coverage results file in path.wrqz2trace: Could not open %r for writing: %s- skippingr;rNrz%5d: z z>>>>>> )rr) rArFrIrrJ enumeraterrx_blankmatchPRAGMA_NOCOVER expandtabs) rrlinesrnrlrqrrLrtrsrklinerrr rd`s,      !z"CoverageResults.write_results_file)r)r*r+r!rPrDrvrdrrrr rs    VcCs@i}x3tj|D]"\}}||krd||.rcSs%g|]}t|tr|qSr) isinstancer)rrrrr r#s rcSs%g|]}t|dr|qS) __bases__)hasattr)rrrrr r&s z%s.%s) f_code co_filenamer0co_namergcZ get_referrersr3r)) rframerr%r&riZclsnameZfuncsZdictsclassesrrr file_module_function_of s,         zTrace.file_module_function_ofcCsD|dkr@|j|}|j|j}d|j||fr?)rr<r@rrr)rrrr resultss  z Trace.results)r)r*r+r!rrrrrrrrrrrrrrr rs   0  )   cCs2tjjdtjd|ftjddS)Nz%s: %s rr)rrJrrexit)msgrrr _err_exits!rc#Cs,ddl}|dkr!tj}yV|j|ddddddddd d d d d dddddg\}}Wnq|jk r}zNtjjdtjd|ftjjdtjdtjdWYdd}~XnXd}d}d}d}d} d} g} g} d} d}d}d}d}x|D]\}}|dkrrttjtjd|dkrtjjdtjd|dks|dkrd}q@|dks|dkrd}q@|dks|dkrd}q@|d ks|d!krd}q@|d"ks7|d#kr@d}q@|d$ksX|d%krad}q@|d&ksy|d'krd}q@|d(ks|d)kr|} q@|d*ks|d+krd} q@|d,ks|d-kr|} q@|d.ks|d/krd}q@|d0krBx*|j d1D]}| j |j q"Wq@|d2kr@x|j t j D]}t jj|}|jd3t jjtjd4d5tjdd6}|jd7t jjtjd4d5tjdd6}t jj|}| j |qaWq@q@W|r/|s%|r/td8|pJ|pJ|pJ|pJ|sWtd9|rm|rmtd:|r| rtd;|rt|dkrtd<|rtd=| d>| }|j| d |d?| nK|t_|d}t jj |dtjd| d|}yct|}t|j|dD}WdQRXdE|dFdGdHddIdi}|j|||WnQtk r}z tdJtjd|fWYdd}~Xnt k rYnX|j!}|s(|j| d |d?| dS)KNrrztcrRf:d:msC:lTghelpversionrrmreportz no-reportrgzfile=missingzignore-module=z ignore-dir=z coverdir= listfuncsZ trackcallsrz%s: %s z%Try `%s --help' for more information Fz--helpz --versionz trace 2.0 z-Tz --trackcallsTz-lz --listfuncsz-gz--timingz-tz--tracez-cz--countz-rz--reportz-Rz --no-reportz-fz--filez-mz --missingz-Cz --coverdirz-sz --summaryz--ignore-module,z --ignore-dirz$prefixlibpythonz $exec_prefixz8cannot specify both --listfuncs and (--trace or --count)zLmust specify one of --trace, --count, --report, --listfuncs, or --trackcallsz,cannot specify both --report and --no-reportz--report requires a --filezmissing name of file to runr@rrhrrrrrrr)r __package__ __cached__zCannot run file %r because: %s)"getoptrrerrorrJrrrstdoutsplitappendstriprpathsepr expandvarsr5r` base_prefixrbase_exec_prefixrrr3rrvrrArrrrF SystemExitr)rrZoptsZ prog_argvrrrmrZ no_reportZ counts_filerZignore_modulesZ ignore_dirsrhrgrrrZoptvalr'srZprognametrprZglobsrLrrr mains     !                       -  rcCstdtdt|dS)Nz(The trace.usage() function is deprecated)_warnDeprecationWarningr)rrrr usage.s rc@s"eZdZddddZdS)IgnoreNcCs'tdtdtj|||dS)Nz$The class trace.Ignore is deprecatedr)rrrr!)rrr rrr r!4s zIgnore.__init__)r)r*r+r!rrrr r3s rcCstdtdt|S)Nz*The trace.modname() function is deprecatedr)rrr0)rrrr modname9s rcCstdtdt|S)Nz.The trace.fullmodname() function is deprecatedr)rrr9)rrrr fullmodname>s rcCstdtdt||S)Nz7The trace.find_lines_from_code() function is deprecatedr)rrr)rrrrr find_lines_from_codeCs rcCstdtdt||S)Nz-The trace.find_lines() function is deprecatedr)rrr)rrrrr find_linesHs rcCs tdtdt|ddS)Nz/The trace.find_strings() function is deprecatedrrq)rrr)r%rqrrr find_stringsMs rcCstdtdt|S)Nz:The trace.find_executable_linenos() function is deprecatedr)rrr])r%rrr find_executable_linenosRs rr)/__doc____all__r^rrerrrarrrrBwarningsrrZtimerrr ImportErrorrr r rr|rrzrr0r9rrrrr]rrrrrrrrrrrr)rrrr 1sX                -2