python,util: Add Python MyPy Stubgen to enable Pylance IntelliSense (#307)

This allows us to generate stubs for the modules in gem5. The output
will be a "typings" directory which can be used by Pylance (Python
IntelliSense) to infer typings in Visual Studio Code.

Note: A "typings" directory in the root of the workspace is the default
location for Pylance to look for typings. This can be changed via
`python.analysis.stubPath` in "settings.json".

Usage
=====

```
pip3 install -r requirements.txt
scons build/ALL/gem5.opt -j$(nproc)
./build/ALL/gem5.opt util/gem5-stubgen.py
```
This commit is contained in:
Bobby R. Bruce
2023-09-21 11:52:16 -07:00
committed by GitHub
4 changed files with 78 additions and 1 deletions

View File

@@ -193,6 +193,14 @@ def parse_options():
callback=collect_args,
)
option(
"-P",
action="store_true",
default=False,
help="Don't prepend the script directory to the system path. "
"Mimics Python 3's `-P` option.",
)
option(
"-s",
action="store_true",
@@ -601,7 +609,11 @@ def main():
sys.argv = ["-c"] + options.c[1]
scope = {"__name__": "__m5_main__"}
else:
sys.path = [os.path.dirname(sys.argv[0])] + sys.path
# If `-P` was used (`options.P == true`), don't prepend the script
# directory to the `sys.path`. This mimics Python 3's `-P` option
# (https://docs.python.org/3/using/cmdline.html#cmdoption-P).
if not options.P:
sys.path = [os.path.dirname(sys.argv[0])] + sys.path
filename = sys.argv[0]
filedata = open(filename, "r").read()
filecode = compile(filedata, filename, "exec")