Does pydeps support "function-level" call visualization?
Closed this issue · 5 comments
Does pydeps support "function-level" call visualization?
It does not - only module import relationships.
are you aware of any tool that does this?
EDIT: Seems like pyan3 does exactly that, I'll go test it
EDIT 2: This one is utterly broken, even by tweaking the source code directly, it unexplicably fails.
I don't have any personal experience with any call graphing tools. I imagine it would be a hard problem to solve statically in python, but a quick google for python call graph generator
gives a few hits (I haven't tried any of them).
I've tried a few, and they were all broken and long abandonned. Then, I stumbled upon a more general solution that supports multiple languages and multiple types of graphs, called depends.
To get function-level viz, you specify --granularity method
It does something weird with its DOT format generation, it puts node names (so actual functions) in some weird format in comments atop the digraph
and only uses numbers in the actually shown content. I patched together a small python script that fixes the DOT file to actually show the functions' names, and it works well.
from pathlib import Path
names = dict()
content = Path('PUT YOUR FILE'S PATH HERE').read_text('utf-8')
for mapping_line in filter(lambda s: s.startswith('// '), content.splitlines()):
ident, rest = mapping_line.removeprefix('// ').split(':')
module, rest = rest.split('(')
module = module.removesuffix('.py')
function = rest.split('/')[-1].removeprefix('ideaseed.').removesuffix(')')
names[ident] = module + "." + function
for ident, name in names.items():
content = content.replace(f"\t{ident} ->", f"\t\"{name}\" ->")
content = content.replace(f" {ident};", f" \"{name}\";")
for line in content.splitlines():
if 'cli.run" ->' in line or 'utils.' in line:
content = content.replace(line, "")
print(content)
just putting this out here in case an internet stranger stumbles upon this issue.
To give you an idea, this is what depends could generate
// 4:config_wizard.py(/mnt/datacore/projects/ideaseed/ideaseed.prompt_for_settings)
// 6:config_wizard.py(/mnt/datacore/projects/ideaseed/ideaseed.run)
// 7:config_wizard.py(/mnt/datacore/projects/ideaseed/ideaseed.write_alias_to_rc_file)
// 35:update_checker.py(/mnt/datacore/projects/ideaseed/ideaseed.get_release_notes_between_versions)
// 36:update_checker.py(/mnt/datacore/projects/ideaseed/ideaseed.get_release_notes_for_version)
digraph
{
35 -> 36;
6 -> 7;
6 -> 4;
}
And that's what my script turns it into:
digraph
{
"update_checker.get_release_notes_between_versions" -> "update_checker.get_release_notes_for_version";
"config_wizard.run" -> "config_wizard.write_alias_to_rc_file";
"config_wizard.run" -> "config_wizard.prompt_for_settings";
}
(I've only left some nodes at random for illustration's sake)
@ewen-lbh cool. I expanded your script and made it make a narrower graph:
import sys
import os
from pathlib import Path
import re
def callgraph(src):
_drive, path = os.path.splitdrive(os.getcwd())
content = Path(src).read_text('utf-8')
content = re.sub(r'([A-Z]:)', '', content) # remove dos drive letters
content = content.replace(path, '')
content = content.replace('\\', '/') # dot doesn't like windows paths
names = dict()
lines = content.splitlines()
for mapping_line in [line for line in lines if line.startswith('// ')]:
ident, rest = mapping_line.lstrip('// ').split(':', 1)
module, rest = rest.split('(')
module = module.split('/', 1)[1].rstrip('.py')
function = rest.split('/')[-1].split('.', 1)[1].rstrip(')')
names[ident] = module + "." + function
for ident, name in names.items():
name = name.lstrip('/').replace('/', '.').replace('.', '.\\n')
content = content.replace("\t{ident} ->".format(ident=ident), '\t"{name}" ->'.format(name=name))
content = content.replace(" {ident};".format(ident=ident), ' "{name}";'.format(name=name))
return content
if __name__ == "__main__":
# usage
#
# python callgrpah.py moduledirectory
#
src = sys.argv[1]
os.system('depends --granularity=method -f dot python {} tmp'.format(src))
fixed_dot = callgraph('tmp.dot')
with open('tmp2.dot', 'w') as fp:
print(fixed_dot, file=fp)
os.system('dot -Tsvg -o tmp.svg tmp2.dot')
os.startfile('tmp.svg')
I've only tested it on a directory, not a single file...