def run(func): amountOfRuns = 10 tracer = trace.Trace(count=1, trace=0) argsGen = func_utils.getArgsGenerator(func) for i in range(amountOfRuns): # execute the function with a tracer func_utils.execute(func, argsGen, tracer) # get the code coverage results and find what is there to improve coverage = beautify_coverage(func, tracer.results()) index = get_row_with_min_coverage(coverage) # find out how to improve the coverage (if possible) iterateCode(coverage, index, argsGen)
def trace(args): import trace # create a Trace object, telling it what to ignore, and whether to # do tracing or line-counting or both. tracer = trace.Trace(ignoredirs=[sys.prefix, sys.exec_prefix], trace=False, count=True) # run the new command using the given tracer tracer.run('main(args)') # make a report, placing output in the current directory r = tracer.results() r.write_results(show_missing=True, coverdir="coverage")
def main(): evalArgs=EvalArgs() print("{!s} running with arguments {!s}".format(datetime.datetime.now(),evalArgs.args)) args = [] if evalArgs.isTrace: aTrace = trace.Trace(count=False, trace=True, # keep subprocess and os, but remove most other libs ignoremods=["shlex", "posixpath", "UserDict", "threading", "platform", "getpass", "string"]) aTrace.runctx( "run(*args)", globals(), locals()) else: run(*args)
def RunTest(test_func): """Run main test function and print logging.success() or logging.error(). Args: test_func: function, the function to be tested. """ try: tracer = trace.Trace(ignoredirs=[sys.prefix, sys.exec_prefix], trace=1, count=0) tracer.runfunc(test_func) logging.success('Test finished.') except Exception as e: logging.error('error: ' + str(e)) traceback.print_exc()
def traced(func, ignoredirs=None): """Decorates func such that its execution is traced, but filters out any Python code outside of the system prefix.""" import functools import sys import trace if ignoredirs is None: ignoredirs = ["/usr", sys.prefix] tracer = trace.Trace(trace=1, count=0, ignoredirs=ignoredirs) @functools.wraps(func) def wrapped(*args, **kwargs): return tracer.runfunc(func, *args, **kwargs) return wrapped
def RunTest(test_func): """Run main test function and print TestSuccess or TestFailed. Args: test_func: function, the function to be tested. """ try: tracer = trace.Trace(ignoredirs=[sys.prefix, sys.exec_prefix], trace=1, count=0) tracer.runfunc(test_func) print('TestSuccess: Test finished.') except Exception as e: print('TestFailed: error: ' + str(e)) traceback.print_exc()
def run(self): import trace tracer = trace.Trace(count=True, trace=False, ignoredirs=[sys.prefix, sys.exec_prefix]) def run_tests(): import mutagen import mutagen._util reload(mutagen._util) reload(mutagen) cmd = self.reinitialize_command("test") cmd.quick = self.quick cmd.ensure_finalized() cmd.run() tracer.runfunc(run_tests) results = tracer.results() coverage = os.path.join(os.path.dirname(__file__), "coverage") results.write_results(show_missing=True, coverdir=coverage) for match in glob.glob(os.path.join(coverage, "[!m]*.cover")): os.unlink(match) try: os.unlink(os.path.join(coverage, "..setup.cover")) except OSError: pass total_lines = 0 bad_lines = 0 for filename in glob.glob(os.path.join(coverage, "*.cover")): lines = open(filename, "rU").readlines() total_lines += len(lines) bad_lines += len([ line for line in lines if (line.startswith(">>>>>>") and "finally:" not in line and '"""' not in line) ]) pct = 100.0 * (total_lines - bad_lines) / float(total_lines) print("Coverage data written to %s (%d/%d, %0.2f%%)" % (coverage, total_lines - bad_lines, total_lines, pct)) if pct < 98.66: raise SystemExit( "Coverage percentage went down; write more tests.") if pct > 98.7: raise SystemExit("Coverage percentage went up; change setup.py.")
def run_tests_sequential(self): if self.ns.trace: import trace self.tracer = trace.Trace(trace=False, count=True) save_modules = sys.modules.keys() print("Run tests sequentially") previous_test = None for test_index, test_name in enumerate(self.tests, 1): start_time = time.monotonic() text = test_name if previous_test: text = '%s -- %s' % (text, previous_test) self.display_progress(test_index, text) if self.tracer: # If we're tracing code coverage, then we don't exit with status # if on a false return value from main. cmd = ('result = runtest(self.ns, test_name); ' 'self.accumulate_result(result)') ns = dict(locals()) self.tracer.runctx(cmd, globals=globals(), locals=ns) result = ns['result'] else: result = runtest(self.ns, test_name) self.accumulate_result(result) if result.result == INTERRUPTED: break previous_test = format_test_result(result) test_time = time.monotonic() - start_time if test_time >= PROGRESS_MIN_TIME: previous_test = "%s in %s" % (previous_test, format_duration(test_time)) elif result[0] == PASSED: # be quiet: say nothing if the test passed shortly previous_test = None # Unload the newly imported modules (best effort finalization) for module in sys.modules.keys(): if module not in save_modules and module.startswith("test."): support.unload(module) if previous_test: print(previous_test)
def init_generator(self, module, source, node_type, instance_id, collectors, client_context, http_port, sandesh_req_uve_pkg_list=None, discovery_client=None, connect_to_collector=True): self._role = self.SandeshRole.GENERATOR self._module = module self._source = source self._node_type = node_type self._instance_id = instance_id self._client_context = client_context self._collectors = collectors self._connect_to_collector = connect_to_collector self._rcv_queue = WorkQueue(self._process_rx_sandesh) self._init_logger(source + ':' + module + ':' + node_type + ':' \ + instance_id) self._logger.info('SANDESH: CONNECT TO COLLECTOR: %s', connect_to_collector) self._stats = SandeshStats() self._trace = trace.Trace() self._sandesh_request_dict = {} self._uve_type_maps = SandeshUVETypeMaps(self._logger) if sandesh_req_uve_pkg_list is None: sandesh_req_uve_pkg_list = [] # Initialize the request handling # Import here to break the cyclic import dependency import sandesh_req_impl sandesh_req_impl = sandesh_req_impl.SandeshReqImpl(self) sandesh_req_uve_pkg_list.append('pysandesh.gen_py') for pkg_name in sandesh_req_uve_pkg_list: self._create_sandesh_request_and_uve_lists(pkg_name) self._gev_httpd = None if http_port != -1: self._http_server = SandeshHttp( self, module, http_port, sandesh_req_uve_pkg_list) self._gev_httpd = gevent.spawn( self._http_server.start_http_server) primary_collector = None secondary_collector = None if self._collectors is not None: if len(self._collectors) > 0: primary_collector = self._collectors[0] if len(self._collectors) > 1: secondary_collector = self._collectors[1] if self._connect_to_collector: self._client = SandeshClient( self, primary_collector, secondary_collector, discovery_client) self._client.initiate()
def run_tests_sequential(self): if self.ns.trace: import trace self.tracer = trace.Trace(trace=False, count=True) save_modules = sys.modules.keys() print('Run tests sequentially') previous_test = None for test_index, test in enumerate(self.tests, 1): start_time = time.monotonic() text = test if previous_test: text = '%s -- %s' % (text, previous_test) self.display_progress(test_index, text) if self.tracer: cmd = ( 'result = runtest(self.ns, test); self.accumulate_result(test, result)' ) ns = dict(locals()) self.tracer.runctx(cmd, globals=globals(), locals=ns) result = ns['result'] else: try: result = runtest(self.ns, test) except KeyboardInterrupt: self.interrupted = True self.accumulate_result(test, (INTERRUPTED, None)) break else: self.accumulate_result(test, result) previous_test = format_test_result(test, result[0]) test_time = time.monotonic() - start_time if test_time >= PROGRESS_MIN_TIME: previous_test = '%s in %s' % (previous_test, format_duration(test_time)) elif result[0] == PASSED: previous_test = None if self.ns.findleaks: gc.collect() if gc.garbage: print('Warning: test created', len(gc.garbage), end=' ') print('uncollectable object(s).') self.found_garbage.extend(gc.garbage) del gc.garbage[:] for module in sys.modules.keys(): if module not in save_modules and module.startswith('test.'): support.unload(module) if previous_test: print(previous_test)
def report_tracing(func, *args, **kwargs): outputs = collections.defaultdict(list) tracing = trace.Trace(trace=False) tracing.runfunc(func, *args, **kwargs) traced = collections.defaultdict(set) for filename, line in tracing.results().counts: traced[filename].add(line) for filename, tracedlines in traced.items(): with open(filename) as f: for idx, fileline in enumerate(f, start=1): outputs[filename].append((idx, idx in tracedlines, fileline)) return outputs
def __init__(self, config=None, parent=None): self.parent = parent self.id = None self.model = None self.type = 'int' self.time = 0 self.value = 0 self.min = 0 self.max = 100 self.unit = '' self.trace = trace.Trace(self) self.trace.name = 'SE' self.direction = 1 if config is not None: self.set_config(config)
def gettracedata(file_path): temp_data = [] trace_begin = False for line in open(file_path, 'rb'): log = line.decode() log = log.strip() if (istracebegin(log)): trace_begin = True elif (trace_begin == True): if isend(log): break elif (islog(log)): data = trace.Trace(log) temp_data.append(data) # print (log) return temp_data
def run_main_with_trace(): # create a Trace object, telling it what to ignore, and whether to # do tracing or line-counting or both. tracer = trace.Trace(ignoredirs=[sys.prefix, sys.exec_prefix], trace=0, count=1, countfuncs=1, countcallers=1, infile='/tmp/cover.tmp', outfile='/tmp/cover.tmp') # 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, summary=True, coverdir="/tmp")
def test(): import log, trace np.random.seed(9973) logger = log.getLogger(name="test.log", level="DEBUG") job_trace = trace.Trace(logger).get_trace() env = DRF_Env("DRF", job_trace, logger) while not env.end: env.step() #print env.observe() # print env.data # input() print env.get_results() print env.get_job_jcts() for i in range(len(env.trace)): if i in env.trace: for job in env.trace[i]: print i + 1, job.id, job.type, job.model
def test_issue9936(self): tracer = trace.Trace(trace=0, count=1) modname = 'test.tracedmodules.testmod' if modname in sys.modules: del sys.modules[modname] cmd = 'import test.tracedmodules.testmod as t;t.func(0); t.func2();' with captured_stdout() as stdout: self._coverage(tracer, cmd) stdout.seek(0) stdout.readline() coverage = {} for line in stdout: lines, cov, module = line.split()[:3] coverage[module] = int(lines), int(cov[:-1]) modname = trace._fullmodname(sys.modules[modname].__file__) self.assertIn(modname, coverage) self.assertEqual(coverage[modname], (5, 100))
def __init__(self, _config=None): Thread.__init__(self) self.config_ = { 'serial': { 'port': '/dev/ttyUSB0', 'baudrate': 921600, 'parity': 'none', 'stopbits': 1, 'databits': 8 } } self.trace = trace.Trace() self.trace.name = 'ALPS' self.serial_ = serial.Serial() self.running_time_ = 1800 self.time_table_ = []
def iload(filename, load_data=True): try: traces = [] for tr in mseed_ext.get_traces( filename, load_data ): network, station, location, channel = tr[1:5] tmin = float(tr[5])/float(HPTMODULUS) tmax = float(tr[6])/float(HPTMODULUS) try: deltat = reuse(float(1.0)/float(tr[7])) except ZeroDivisionError, e: raise MSeedError('Trace in file %s has a sampling rate of zero.' % filename) ydata = tr[8] traces.append(trace.Trace(network, station, location, channel, tmin, tmax, deltat, ydata)) for tr in traces: yield tr
def traced(func, ignoredirs=None): """ Decorates func such that its execution is traced, but filters out any Python code outside of the system prefix. https://drake.mit.edu/python_bindings.html#debugging-with-the-python-bindings """ import functools import sys import trace if ignoredirs is None: ignoredirs = ["/usr", sys.prefix] tracer = trace.Trace(trace=1, count=0, ignoredirs=ignoredirs) @functools.wraps(func) def wrapped(*args, **kwargs): return tracer.runfunc(func, *args, **kwargs) return wrapped
def __init__(self, _config=None): Thread.__init__(self) self.trace = trace.Trace(self) self.config_ = { 'serial': { 'port': '/dev/ttyACM0', 'baudrate': 115200, 'parity': 'none', 'stopbits': 1, 'databits': 8 } } self.base_ = (None, None) self.latest_ = (None, None) self.serial_ = serial.Serial() self.setConfig(_config) self.trace.info('Create UBlox')
def track( statement ) : """ Parameters ---------- statement : str statement to be traced """ # create a Trace object, telling it what to ignore, and whether to # do tracing or line-counting or both. tracer = trace.Trace( ignoredirs=[sys.prefix, sys.exec_prefix], trace=0, count=1 ) # run the new command using the given tracer tracer.run( statement ) # make a report, placing output in the current directory r = tracer.results() r.write_results(show_missing=True, coverdir=".")
def generate_irregular_pacing_response(self, protocol): """ Args: protocol: An irregular pacing protocol Returns: A irregular pacing trace """ pacing_info = trace.IrregularPacingInfo() try: solution = integrate.solve_ivp( self.generate_irregular_pacing_function(protocol, pacing_info), [0, protocol.duration], self.y_initial, method='BDF', max_step=1e-3 * self.time_conversion) self._set_data_without_error(solution) except ValueError: return None return trace.Trace(self.t, self.y_voltage, pacing_info=pacing_info)
def create_node(uri, control_uri, trace_exec=False, attributes=None): _log.debug("create_node") n = Node(uri, control_uri, attributes) _log.debug("create_node 2") if trace_exec: _, host = uri.split('://') # Trace execution and dump in output file "<host>_<port>.trace" with open("%s.trace" % (host, ), "w") as f: tmp = sys.stdout # Modules to ignore modlist = ['fifo', 'calvin', 'actor', 'pickle', 'socket', 'uuid', 'codecs', 'copy_reg', 'string_escape', '__init__'] with f as sys.stdout: tracer = trace.Trace(trace=1, count=0, ignoremods=modlist) tracer.runfunc(n.run) sys.stdout = tmp else: n.run() _log.info('Quitting node "%s"' % n.uri)
def testLambda(self): #simple list lst = [('d',82),('a',21),('a',4),('f',29),('q',12),('j',21),('k',99)] lst.sort(key=lambda k:k[1]) print(lst) lst.sort(key=lambda k:k[0]) print(lst) lst.sort(key=lambda k:(k[1], k[0])) print(lst) # 复杂的dict,按照dict对象中某一个属性进行排序 lst = [{'level': 19, 'star': 36, 'time': 1}, {'level': 20, 'star': 40, 'time': 2}, {'level': 20, 'star': 40, 'time': 3}, {'level': 20, 'star': 40, 'time': 4}, {'level': 20, 'star': 40, 'time': 5}, {'level': 18, 'star': 40, 'time': 1}] # 需求: # level越大越靠前; # level相同, star越大越靠前; # level和star相同, time越小越靠前; # 先按time排序 lst.sort(key=lambda k: (k.get('time', 0))) t1 = trace.Trace(ignoredirs=[sys.prefix, sys.exec_prefix],trace=0,count=1) t1.run('''lst = [{'level': 19, 'star': 36, 'time': 1},{'level': 20, 'star': 40, 'time': 2},{'level': 20, 'star': 40, 'time': 3},{'level': 20, 'star': 40, 'time': 4},{'level': 20, 'star': 40, 'time': 5},{'level': 18, 'star': 40, 'time': 1}];lst.sort(key=lambda k: (k.get('time', 0)))'''); r = t1.results() r.write_results(show_missing=True, coverdir=".") cProfile.run('''lst = [{'level': 19, 'star': 36, 'time': 1},{'level': 20, 'star': 40, 'time': 2},{'level': 20, 'star': 40, 'time': 3},{'level': 20, 'star': 40, 'time': 4},{'level': 20, 'star': 40, 'time': 5},{'level': 18, 'star': 40, 'time': 1}];lst.sort(key=lambda k: (k.get('time', 0)))'''); # 再按照level和star顺序 # reverse=True表示反序排列,默认正序排列 lst.sort(key=lambda k: (k.get('level', 0), k.get('star', 0)), reverse=True) for idx, r in enumerate(lst): print('idx[%d]\tlevel: %d\t star: %d\t time: %d\t' % (idx, r['level'], r['star'], r['time']))
def process(self): try: line = self.slink.stdout.readline() if not line: return False toks = line.split(', ') if len(toks) != 1: nslc = tuple(toks[0].split('_')) if len(nslc) == 3: nslc = nslc[0], nslc[1], '', nslc[2] nsamples = int(toks[1].split()[0]) rate = float(toks[2].split()[0]) st, sms = toks[3].split()[0].split('.') us = int(sms) tstamp = calendar.timegm(time.strptime( st, '%Y,%j,%H:%M:%S')) + us * 0.000001 if nsamples != 0: self.header = nslc, nsamples, rate, tstamp else: if self.header: self.vals.extend([float(x) for x in line.split()]) if len(self.vals) == self.header[1]: nslc, nsamples, rate, tstamp = self.header deltat = 1.0 / rate net, sta, loc, cha = nslc tr = trace.Trace(network=net, station=sta, location=loc, channel=cha, tmin=tstamp, deltat=deltat, ydata=num.array(self.vals)) self.got_trace(tr) self.vals = [] self.header = None return True except: return False
def _Main(argv): result = 0 repodir = _FindRepo() if repodir is None: repodir = os.path.join(os.getcwd(), REPODIR) _MkRepoDir(repodir) repo = _Repo(repodir) try: try: init_ssh() init_http() name, gopts, argv = repo._ParseArgs(argv) run = lambda: repo._Run(name, gopts, argv) or 0 if gopts.trace_python: import trace tracer = trace.Trace(count=False, trace=True, timing=True, ignoredirs=set(sys.path[1:])) result = tracer.runfunc(run) else: result = run() finally: close_ssh() except KeyboardInterrupt: print('aborted by user', file=sys.stderr) result = 1 except ManifestParseError as mpe: print('fatal: %s' % mpe, file=sys.stderr) result = 1 except RepoChangedException as rce: # If repo changed, re-exec ourselves. # argv = list(sys.argv) argv.extend(rce.extra_args) try: os.execv(sys.executable, [__file__] + argv) except OSError as e: print('fatal: cannot restart repo after upgrade', file=sys.stderr) print('fatal: %s' % e, file=sys.stderr) result = 128 TerminatePager() return result
def test_issue9936(self): tracer = trace.Trace(trace=0, count=1) modname = 'test.tracedmodules.testmod' # Ensure that the module is executed in import if modname in sys.modules: del sys.modules[modname] cmd = ("import test.tracedmodules.testmod as t;" "t.func(0); t.func2();") with captured_stdout() as stdout: self._coverage(tracer, cmd) stdout.seek(0) stdout.readline() coverage = {} for line in stdout: lines, cov, module = line.split()[:3] coverage[module] = (int(lines), int(cov[:-1])) # XXX This is needed to run regrtest.py as a script modname = trace._fullmodname(sys.modules[modname].__file__) self.assertIn(modname, coverage) self.assertEqual(coverage[modname], (5, 100))
def RunTranslate(translate_func: typing.Callable, run_with_tracing: bool = True): """Run `translate_func`, and communicate success or failure back to Daisy. Args: translate_func: Closure to execute run_with_tracing: When enabled, the closure will be executed with trace.Trace, resulting in executed lines being printed to stdout. """ try: if run_with_tracing: tracer = trace.Trace(ignoredirs=[sys.prefix, sys.exec_prefix], trace=1, count=0) tracer.runfunc(translate_func) else: translate_func() logging.success('Translation finished.') except Exception as e: logging.error('error: %s', str(e))
def make_tracer(only_mods): ignoredirs = () all_mods = set(sys.modules.keys()) ignoremods = set() for mod in all_mods: good = False for only_mod in only_mods: if mod == only_mod or mod.startswith( f"{only_mod}.") or only_mod.startswith(f"{mod}."): good = True break if not good: ignoremods.add(mod) print("\n".join(sorted(ignoremods))) tracer = trace.Trace(trace=1, count=0, ignoredirs=ignoredirs, ignoremods=ignoremods) return tracer
def run_with_coverage(fn): """ Runs the function (that shouldn't take any arguments!) with coverage tool. Stores the results in a log and returns the results. """ coverage_trace = trace.Trace(trace=0, ignoredirs=[sys.prefix, sys.exec_prefix]) try: coverage_trace.runfunc(lambda ignore1, ignore2: fn(), [], {}) finally: results = coverage_trace.results() coverdir = os.path.join(log_dir, "pycoverage") if not os.path.exists(coverdir): os.mkdir(coverdir) os.chmod(coverdir, 0o777) # so that non-root tests could write to this directory results.write_results(show_missing=False, coverdir=coverdir) return results