Git Product home page Git Product logo

Comments (5)

thebjorn avatar thebjorn commented on September 16, 2024 1

@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...

from pydeps.

thebjorn avatar thebjorn commented on September 16, 2024

It does not - only module import relationships.

from pydeps.

ewen-lbh avatar ewen-lbh commented on September 16, 2024

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.

from pydeps.

thebjorn avatar thebjorn commented on September 16, 2024

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).

from pydeps.

ewen-lbh avatar ewen-lbh commented on September 16, 2024

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)

from pydeps.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.