@sldZddgZddlZddlZddlZddlZddlZddlZddlZddl Z ddl Z ddl Z ddl m ZyddlmZWn"ek rddlmZYnXyddlZWn'ek r ejZdd ZYnXd d Zd d Zd dZdZejdZGdddZddZddZGdddZddZddZ dddZ!ddZ"Gd ddZ#d!d"Z$dd#d$Z%d%d&Z&Gd'd(d(eZ'd)d*Z(d+d,Z)d-d.Z*d/d0Z+dd1d2Z,d3d4Z-e.d5krhe%ndS)6aprogram/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) monotonic)timecCstjddS)N)syssettracer r /usr/lib/python3.4/trace.py _unsettraceHsr cCstj|tj|dS)N) threadingrr)funcr r r _settraceKs rcCstjdtjddS)N)rrr r r r r r Os 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)outfiler r r _usageSs*rz#pragma NO COVERz ^\s*(#.*)?$c@s.eZdZddddZddZdS)_IgnoreNcCsT|stn t||_|s*gndd|D|_idd6|_dS)NcSs"g|]}tjj|qSr )ospathnormpath).0dr r r s z$_Ignore.__init__..z)set_mods_dirs_ignore)selfmodulesdirsr r r __init__s z_Ignore.__init__cCs||jkr|j|S||jkr:d|j|)r$endswith)rr&r r r is_ignored_filenamesz#CoverageResults.is_ignored_filenamec Cs|j}|j}|j}|j}|j}|j}x,|D]$}|j|d||||z %s.%s -> %s.%s.pyc.pyoNrz.coverr;dzlines cov% module (path)z%5d %3d%% %s (%s)wbz"Can't save counts files because %sr<)rTrU)!r?rJsortedr@r=rQitemsrPrOrrdirnameabspathr1existsmakedirsr:_find_executable_linenos linecachegetlinesjoinrDtokenizedetect_encodingreadlinewrite_results_fileintrrBdumprGrrK)!rZ show_missingsummarycoverdirZcallsr&r'funcnameZlastfileZ lastcfileZpfileZpmodZpfunccfileZcmodZcfuncZper_filelineno lines_hitZsumscountr9lnotabsourceZ coverpathfpencoding_n_hitsn_linesZpercentmrLr r r write_results s|      +    !       zCoverageResults.write_resultsc CsNyt|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 q~W|j | |fS) z'Return a coverage results file in path.wrsz2trace: Could not open %r for writing: %s- skippingr<rNrz%5d: z z>>>>>> )rr) rDrGrJrrK enumeraterrx_blankmatchPRAGMA_NOCOVER expandtabsclose) rrlinesrprnrsrrLrvrurmliner r r rfbs,       z"CoverageResults.write_results_file)r*r+r,r"rPrErxrfr r r r rs    UcCsCi}x6tj|D]%\}}||krd||.rcSs%g|]}t|tr|qSr ) isinstancer)rrr r r r%s rcSs%g|]}t|dr|qS) __bases__)hasattr)rrr r r r(s z%s.%s) f_code co_filenamer1co_namergcZ get_referrersr4r*) rframerr&r'rkZclsnameZfuncsZdictsZclassesr r r file_module_function_ofs,        zTrace.file_module_function_ofcCsG|dkrC|j|}|j|j}d|j||f| d?| }|j| d |d@| nQ|t_|d}t jj |dtjd| d?| d|}yft|}t|j|dE}WdQXi|dF6dGdH6ddI6ddJ6}|j|||WnQt k r?}z tdKtjd|fWYdd}~Xnt!k rPYnX|j"}|s|j| d |d@| ndS)LNrrztcrRf:d:msC:lTghelpversionrroreportz no-reportrizfile=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$prefixZlibZpythonz $exec_prefixzShould never get herez8cannot 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 runrArrjrrrrrrrr* __package__ __cached__zCannot run file %r because: %s)#getoptrrerrorrKrrrstdoutsplitappendstriprpathsepr expandvarsr6rb base_prefixrbase_exec_prefixrAssertionErrorrr4rrxrrDrrrrG SystemExitr)rrZoptsZ prog_argvrrrorZ no_reportZ counts_filerZignore_modulesZ ignore_dirsrjrirrrZoptvalr(srZprognametrrrZglobsrLr r r mains     !                     -  rcCstdtdt|dS)Nz(The trace.usage() function is deprecated)_warnDeprecationWarningr)rr r r usage0s rc@s"eZdZddddZdS)IgnoreNcCs'tdtdtj|||dS)Nz$The class trace.Ignore is deprecatedr)rrrr")rr r!r r r r"6s zIgnore.__init__)r*r+r,r"r r r r r5s rcCstdtdt|S)Nz*The trace.modname() function is deprecatedr)rrr1)rr r r modname;s rcCstdtdt|S)Nz.The trace.fullmodname() function is deprecatedr)rrr:)rr r r fullmodname@s rcCstdtdt||S)Nz7The trace.find_lines_from_code() function is deprecatedr)rrr)rrr r r find_lines_from_codeEs rcCstdtdt||S)Nz-The trace.find_lines() function is deprecatedr)rrr)rrr r r find_linesJs rcCs tdtdt|ddS)Nz/The trace.find_strings() function is deprecatedrrs)rrr)r&rsr r r find_stringsOs rcCstdtdt|S)Nz:The trace.find_executable_linenos() function is deprecatedr)rrr_)r&r r r find_executable_linenosTs rr)/__doc____all__r`rrerrrcrrrrBwarningsrrrrr ImportErrorr rrr rr~rr|rr1r:rrrrr_rrrrrrrrrrrr*r r r r 1s^                 -2