Python: CG trace: sort output before writing/printing

Allows comparing output of one run with another
This commit is contained in:
Rasmus Wriedt Larsen
2020-07-15 14:37:41 +02:00
parent e6873956ca
commit ba4207fc90
2 changed files with 10 additions and 10 deletions

View File

@@ -54,7 +54,7 @@ def canonic_filename(filename):
return canonic
@dataclasses.dataclass(frozen=True)
@dataclasses.dataclass(frozen=True, eq=True, order=True)
class Call():
"""A call
"""
@@ -77,16 +77,16 @@ class Call():
)
@dataclasses.dataclass(frozen=True)
@dataclasses.dataclass(frozen=True, eq=True, order=True)
class Callee():
"""A callee (Function/Lambda/???)
should (hopefully) be uniquely identified by its name and location (filename+line
number)
"""
funcname: str
filename: str
linenum: int
funcname: str
@classmethod
def from_frame(cls, frame):
@@ -145,7 +145,7 @@ class CSVExporter(Exporter):
def export(recorded_calls, outfile_path):
with open(outfile_path, 'w', newline='') as csv_file:
writer = None
for (call, callee) in recorded_calls:
for (call, callee) in sorted(recorded_calls):
data = {
**Exporter.dataclass_to_dict(call),
**Exporter.dataclass_to_dict(callee)
@@ -170,7 +170,7 @@ class XMLExporter(Exporter):
root = ET.Element('root')
for (call, callee) in recorded_calls:
for (call, callee) in sorted(recorded_calls):
data = {
**Exporter.dataclass_to_dict(call),
**Exporter.dataclass_to_dict(callee)
@@ -234,7 +234,7 @@ if __name__ == "__main__":
elif opts.xml:
XMLExporter.export(cgt.recorded_calls, opts.xml)
else:
for (call, callee) in cgt.recorded_calls:
for (call, callee) in sorted(cgt.recorded_calls):
print(f'{call} -> {callee}')
print('--- captured stdout ---')

View File

@@ -1,6 +1,6 @@
<root>
<recorded_call call_filename="/home/rasmus/code/ql/python/tools/recorded-call-graph-metrics/example/simple.py" call_linenum="7" call_inst_index="18" callee_funcname="foo" callee_filename="/home/rasmus/code/ql/python/tools/recorded-call-graph-metrics/example/simple.py" callee_linenum="1" />
<recorded_call call_filename="/home/rasmus/code/ql/python/tools/recorded-call-graph-metrics/example/simple.py" call_linenum="8" call_inst_index="24" callee_funcname="bar" callee_filename="/home/rasmus/code/ql/python/tools/recorded-call-graph-metrics/example/simple.py" callee_linenum="4" />
<recorded_call call_filename="/home/rasmus/code/ql/python/tools/recorded-call-graph-metrics/example/simple.py" call_linenum="10" call_inst_index="30" callee_funcname="foo" callee_filename="/home/rasmus/code/ql/python/tools/recorded-call-graph-metrics/example/simple.py" callee_linenum="1" />
<recorded_call call_filename="/home/rasmus/code/ql/python/tools/recorded-call-graph-metrics/example/simple.py" call_linenum="10" call_inst_index="36" callee_funcname="bar" callee_filename="/home/rasmus/code/ql/python/tools/recorded-call-graph-metrics/example/simple.py" callee_linenum="4" />
<recorded_call call_filename="/home/rasmus/code/ql/python/tools/recorded-call-graph-metrics/example/simple.py" call_linenum="7" call_inst_index="18" callee_filename="/home/rasmus/code/ql/python/tools/recorded-call-graph-metrics/example/simple.py" callee_linenum="1" callee_funcname="foo" />
<recorded_call call_filename="/home/rasmus/code/ql/python/tools/recorded-call-graph-metrics/example/simple.py" call_linenum="8" call_inst_index="24" callee_filename="/home/rasmus/code/ql/python/tools/recorded-call-graph-metrics/example/simple.py" callee_linenum="4" callee_funcname="bar" />
<recorded_call call_filename="/home/rasmus/code/ql/python/tools/recorded-call-graph-metrics/example/simple.py" call_linenum="10" call_inst_index="30" callee_filename="/home/rasmus/code/ql/python/tools/recorded-call-graph-metrics/example/simple.py" callee_linenum="1" callee_funcname="foo" />
<recorded_call call_filename="/home/rasmus/code/ql/python/tools/recorded-call-graph-metrics/example/simple.py" call_linenum="10" call_inst_index="36" callee_filename="/home/rasmus/code/ql/python/tools/recorded-call-graph-metrics/example/simple.py" callee_linenum="4" callee_funcname="bar" />
</root>