.pyi Generator generates
.pyi type stubs
for the entire Ghidra API.
Those stub files can later be used in PyCharm to enhance the development experience.
You can either use the stubs released here, or follow the instructions below to generate them yourself.
The release contains PEP 561 stub package, which can simply be installed with
pip install ghidra-stubs*.whl
into the environment in which the real
ghidra module is available. Any conformant tool will then use the stub package
for type analysis purposes.
If you want to manually add the stub files to PyCharm, follow the instructions in Install, uninstall, and upgrade interpreter paths.
Once installed, all you need to do is import the Ghidra modules as usual, and PyCharm will do the rest.
To get support for the Ghidra builtins, you need to import them as well. The type hints for those exist in
ghidra_builtins.pyi stub. Since it is not a real Python module, importing it at runtime will fail.
.pyi gives PyCharm all the information it needs to help you.
try: from ghidra.ghidra_builtins import * except: pass
If you are using ghidra_bridge from a Python 3 environment where no real
exists you can use a snippet like the following:
import typing if typing.TYPE_CHECKING: import ghidra from ghidra.ghidra_builtins import * else: b = ghidra_bridge.GhidraBridge(namespace=globals()) # actual code follows here
typing.TYPE_CHECKING is a special value that is always
False at runtime but
True during any kind of type checking or completion.
Once done, just code & enjoy.
To properly extract all types from Ghidra, make sure to extract the API documentation.
Help -> Ghidra API Help
The script depends on both the
# Create a virtualenv for Ghidra packages. # It is important to use Python2.7 for this venv! # If you want, you can skip this step and use your default Python installation. mkvirtualenv -p python2.7 ghidra # Create Jython's site-pacakges directory. jython_site_packages=~/.local/lib/jython2.7/site-packages mkdir -p $jython_site_packages # Create a PTH file to point Jython to Python's site-packages directories. # Again, this has to be Python2.7. # Outside a virtualenv, use python2.7 -c "import site; print(site.getusersitepackages()); print(site.getsitepackages()[-1])" > $jython_site_packages/python.pth # If using virtualenv, use the following instead python2.7 -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())" > $jython_site_packages/python.pth # Use pip to install packages for Ghidra pip install attrs typing
Script Directoriesin the Ghidra Script Manager
generate_ghidra_pyi.py(will be located under
$GHIDRA_ROOT/support/analyzeHeadless /tmp tmp -scriptPath $(pwd) -preScript generate_ghidra_pyi.py ./
generate_ghidra_pyi.py generates a
setup.py inside the directory that was selected.
This allows using
pip install to install a PEP 561 stub package that is recognized by PyCharm and other tools as containing type information for the ghidra module.