Created
March 22, 2018 04:44
-
-
Save hughdbrown/b8dc5cbb4c7d421e96d94dc24ca6345c to your computer and use it in GitHub Desktop.
Locate tests in which patch.start is called but patch.stop may not be
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import logging | |
import os | |
import os.path | |
import re | |
from itertools import tee, izip | |
re_class_start = re.compile(r'^class\s+(?P<name>[^(]+).*$') | |
re_func_start = re.compile(r'^\s*def\s+(?P<name>[^(]+).*$') | |
re_patch_start = re.compile(r'^.*patch.*\.start\(.*$') | |
re_patch_stop = re.compile(r'^.*patch.*\.stop\(.*$') | |
logging.basicConfig(level=logging.INFO) | |
logger = logging.getLogger(__name__) | |
def pairwise(iterable): | |
"s -> (s0,s1), (s1,s2), (s2, s3), ..." | |
a, b = tee(iterable) | |
next(b, None) | |
return izip(a, b) | |
class FileAnalyzer(object): | |
def __init__(self, filename): | |
self.filename = filename | |
with open(filename) as handle: | |
lines = [(i, line.rstrip()) for i, line in enumerate(handle)] | |
self.classes = [(i, re_class_start.match(line).groupdict()["name"]) for i, line in lines if re_class_start.match(line)] | |
self.funcs = [(i, re_func_start.match(line).groupdict()["name"]) for i, line in lines if re_func_start.match(line)] | |
self.patch_starts = [(i, line) for i, line in lines if re_patch_start.match(line)] | |
self.patch_stops = [(i, line) for i, line in lines if re_patch_stop.match(line)] | |
def report(self): | |
if len(self.patch_stops) < len(self.patch_starts): | |
logger.info("File: %s", self.filename) | |
for i, line in self.patch_starts: | |
class_name, func_name = self.class_def(i) | |
logger.info("\t%s.%s start: %s", class_name, func_name, line) | |
for i, line in self.patch_stops: | |
class_name, func_name = self.class_def(i) | |
logger.info("\t%s.%s stop: %s", class_name, func_name, line) | |
def class_def(self, i): | |
if i < self.funcs[0][0]: | |
k, func_name = i, "<global>" | |
else: | |
for first, second in pairwise(self.funcs): | |
if first[0] <= i < second[0]: | |
k, func_name = first | |
break | |
else: | |
k, func_name = self.funcs[-1] | |
if k < self.classes[0][0]: | |
class_name = "<global>" | |
else: | |
for first, second in pairwise(self.classes): | |
if first[0] <= i < second[0]: | |
k, class_name = first | |
break | |
else: | |
k, class_name = self.classes[-1] | |
return class_name, func_name | |
def report_patch_start(start_dir='.'): | |
for root, _, files in os.walk(start_dir): | |
for filename in files: | |
fullpath = os.path.join(root, filename) | |
if os.path.splitext(fullpath)[1] == '.py': | |
t = FileAnalyzer(fullpath) | |
t.report() | |
if __name__ == '__main__': | |
report_patch_start('.') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment