I have a bunch of scripts to perform a task. And I really need to know the call graph of the project because it is very confusing. I am not able to execute the code because it needs extra HW and SW to do so. However, I need to understand the logic behind it. So, I need to know if there is a tool (which do not require any python file execution) that can build a call graph using the modules instead of the trace or python parser. I have such tools for C but not for python.
Thank you.
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
You might want to check out pycallgraph:
pycallgraph
Also in this link a more manual approach is described:
generating-call-graphs-for-understanding-and-refactoring-python-code
SourceTrail will help you here. https://www.sourcetrail.com/
Sourcetrail is a free and open-source cross-platform source explorer that helps you get productive on unfamiliar source code. Supports C, C++, Java and Python
https://github.com/CoatiSoftware/Sourcetrail
Here is a link to the documentation
https://www.sourcetrail.com/documentation/
Please note that Python support is relatively new, so please don't expect it to work perfectly yet.
In short, no such tool exists. Python is far too dynamic of a language to be able to generate a call graph without executing the code.
Here's some code which clearly demonstrates some of the very dynamic features of python:
Note that we're using
eval
to create an instance ofmy_obj
and also usinggetattr
to call one of its methods. These are both methods that would make it extremely difficult to create a static call graph for python. Additionally, there are all sorts of difficult to analyze ways of importing modules.I think your best bet is going to be to sit down with the code base and a pad of paper, and start taking notes by hand. This will have the dual benefit of making you more familiar with the code base, and will not be easily tricked by difficult to parse scenarios.
The best tool I've found is called
pyan
, and was originally written by Edmund Horner, improved by him, and then given colorization and other features by Juha Jeronen. That version has useful commandline options:Here's the result of running
pyan.py --dot -c -e pyan.py | fdp -Tpng
:Edmund Horner's original code is now best found in his github repository, and somebody has also made a repository with both versions, from where you can download Juha Jeronen's version. I've made a clean version combining their contributions into my own repository just for pyan, since both repositories have lots of other software.