def cmd_grepfunction(args, params): "search for function name in source files in cache" cache_params = params["cache"] name = args.name signature = args.signature categories = parse_categories(args.categories) no_body = args.no_body pattern = r".*(" + name + r")[ ]*\((.*)" regex = re.compile(pattern) allmatches = grep_cache(regex, cache_params, linenumbers=True, countonly=False, signature=signature, categories=categories) for fn in sorted(allmatches): if no_body: # Just print signature lines print("\nFile '%s' matches:" % (fn, )) for i, line in allmatches[fn]: print("%5d: %s" % (i, line)) else: # Print function bodies content = read_textfile(fn) lines = content.splitlines() if content else () for i, line in allmatches[fn]: print("%s:%d" % (fn, i)) assert name in lines[i] print(extract_function(lines[i:])) print() return 0
def grep_cache(regex, cache_params, linenumbers=False, countonly=False, signature=None, categories=("inc", "src", "log")): "Search through files in cache for a pattern." allmatches = {} gc = glob_cache(cache_params, categories=categories) for category in categories: for fn in gc.get(category, ()): # Skip non-matches if specific signature is specified if signature is not None and signature not in fn: continue if countonly: matches = 0 else: matches = [] if category == "lib": # If category is "lib", use ldd # TODO: on mac need to use otool libs = ldd(fn) for k, libpath in sorted(libs.items()): if not libpath: continue m = regex.match(libpath) if m: if countonly: matches += 1 else: line = "%s => %s" % (k, libpath) matches.append(line) else: content = read_textfile(fn) lines = content.splitlines() if content else () for i, line in enumerate(lines): m = regex.match(line) if m: if countonly: matches += 1 else: line = line.rstrip("\n\r") if linenumbers: line = (i, line) matches.append(line) if matches: allmatches[fn] = matches return allmatches
def read_log(signature, cache_params): """Lookup log file in disk cache and return file contents or None.""" filename = create_log_filename(signature, cache_params) return read_textfile(filename)
def read_src(signature, cache_params): """Lookup source code in disk cache and return file contents or None.""" filename = create_src_filename(signature, cache_params) return read_textfile(filename)