Comments (5)
@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.
It does not - only module import relationships.
from pydeps.
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.
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.
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)
- The cycles display mode shows useless information and does not work properly for larger projects. HOT 8
- [Feature Request] Specify module name instead of filename HOT 4
- Resolving relative dependencies? HOT 7
- Support for namespace packages HOT 1
- use of `--externals` results in TypeError: 'Config' object does not support item assignment HOT 1
- --dot-output option is not effective HOT 1
- Support output markdown mermaid HOT 1
- empty result with WARNING: SKIPPING ILLEGAL MODULE_NAME: D:.test.a HOT 2
- Support for specifying more than one `fname`
- 1.12.19: pitest fails in `tests/test_package_names.py::test_find_package_names` unit HOT 2
- `--exclude` option not working HOT 1
- Show only top-level imports HOT 1
- Exclude/include dependencies by filepath HOT 1
- Add type hints HOT 1
- Use of pydeps in CI to reject introduction of new import cycles? HOT 3
- Constrain output geometry / size HOT 2
- pydeps randomly generates compiled pyc files when examine python source file HOT 4
- Support for editable packages / direct_url.json HOT 10
- Different types of imports get disconnected. HOT 3
- Feature Request: Pydeps for pipelines HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pydeps.