Package: onecode-pycg
:warning: NotesForked fromhttps://github.com/vitsalis/pycgChanges: Seehttps://github.com/deeplime-io/PyCG/tree/onecodeEssentially added code tracks the order of the calls and the code associated to it. It is used by OneCode to properly interpret code based on theexcellentPyCG.Why a new PyPi package? Well, the not-so-great PyPi doesn't allow to have forked public repositories as part of the dependencies. Nice right?PyCG - Practical Python Call GraphsPyCG generates call graphs for Python code using static analysis.
It efficiently supportsHigher order functionsTwisted class inheritance schemesAutomatic discovery of imported modules for further analysisNested definitionsYou can read the full methodology as well as a complete evaluation on theICSE 2021 paper.You can cite PyCG as follows.
Vitalis Salis, Thodoris Sotiropoulos, Panos Louridas, Diomidis Spinellis and Dimitris Mitropoulos.
PyCG: Practical Call Graph Generation in Python.
In43rd International Conference on Software Engineering, ICSE '21,
25–28 May 2021.InstallationPyCG is implemented in Python3 and requires Python version 3.4 or higher.
It also has no dependencies. Simply:pip install onecode-pycgUsage~ >>> pycg -h
usage: __main__.py [-h] [--package PACKAGE] [--fasten] [--product PRODUCT]
                        [--forge FORGE] [--version VERSION] [--timestamp TIMESTAMP]
                        [--max-iter MAX_ITER] [--operation {call-graph,key-error}]
                        [--as-graph-output AS_GRAPH_OUTPUT] [-o OUTPUT]
                        [entry_point ...]

positional arguments:
  entry_point           Entry points to be processed

optional arguments:
  -h, --help            show this help message and exit
  --package PACKAGE     Package containing the code to be analyzed
  --fasten              Produce call graph using the FASTEN format
  --product PRODUCT     Package name
  --forge FORGE         Source the product was downloaded from
  --version VERSION     Version of the product
  --timestamp TIMESTAMP
                        Timestamp of the package's version
  --max-iter MAX_ITER   Maximum number of iterations through source code. If not specified a fix-point iteration will be performed.
  --operation {call-graph,key-error}
                        Operation to perform. Choose call-graph for call graph generation (default) or key-error for key error detection on dictionaries.
  --as-graph-output AS_GRAPH_OUTPUT
                        Output for the assignment graph
  -o OUTPUT, --output OUTPUT
                        Output pathThe following command line arguments should used only when--fastenis
provied:--product: The name of the package.--forge: Source the package was downloaded from.--version: The version of the package.--timestamp: The timestamp of the package's version.Call Graph OutputSimple JSON formatThe call edges are in the form of an adjacency list where an edge(src, dst)is represented as an entry ofdstin the list assigned to keysrc:{
    "node1": ["node2", "node3"],
    "node2": ["node3"],
    "node3": []
}FASTEN FormatFor an up-to-date description of the FASTEN format refer to theFASTEN
wiki.Key Errors OutputWe are currently experimenting on identifying potential invalid dictionary
accesses on Python dictionaries (key errors).
The output format for key errors is a list of dictionaries containing:The file name in which the key error was identifiedThe line number inside the fileThe namespace of the accessed dictionaryThe key used to access the dictionary[{
    "filename": "mod.py",
    "lineno": 2,
    "namespace": "mod.<dict1>",
    "key": "key2"
},
{
    "filename": "mod.py",
    "lineno": 8,
    "namespace": "mod.<dict1>",
    "key": "nokey"
}]ExamplesAll the entry points are known and we want the simple JSON format~ >>> pycg --package pkg_root pkg_root/module1.py pkg_root/subpackage/module2.py -o cg.jsonAll entry points are not known and we want the simple JSON format~ >>> pycg --package django $(find django -type f -name "*.py") -o django.jsonWe want the FASTEN format:~ >>> pycg --package pypi_pkg --fasten --product "pypipkg" --forge "PyPI" \
        --version "0.1" --timestamp 42 \
        pypi_pkg/module1.py pkg_root/subpackage/module2.py -o cg.jsonRunning TestsFrom the root directory, first install themockpackage:pip3 install mockΤhen, simply run the tests by executing:make test