GP-5133: Improvements to how we use python type stubs

This commit is contained in:
Ryan Kurtz 2024-11-15 09:53:29 -05:00
parent 32c39f3518
commit 1df4527dd3
3 changed files with 32 additions and 14 deletions

View File

@ -136,6 +136,9 @@ public class VSCodeProjectScript extends GhidraScript {
.filter(e -> e.getValue() != null) .filter(e -> e.getValue() != null)
.forEach(e -> sourcesObject.addProperty(e.getKey(), e.getValue())); .forEach(e -> sourcesObject.addProperty(e.getKey(), e.getValue()));
json.addProperty("python.analysis.stubPath",
new File(installDir, "docs/ghidra_stubs/typestubs").getAbsolutePath());
// Write settings json object // Write settings json object
if (!FileUtilities.mkdirs(settingsFile.getParentFile())) { if (!FileUtilities.mkdirs(settingsFile.getParentFile())) {
throw new IOException("Failed to create: " + settingsFile.getParentFile()); throw new IOException("Failed to create: " + settingsFile.getParentFile());

View File

@ -62,9 +62,23 @@ task installJPype(type: Exec) {
} }
} }
// Install ghidra-stubs into the development virtual environment
task installGhidraStubs(type: Exec) {
dependsOn(":createGhidraStubsWheel")
File depsDir = file("${DEPS_DIR}/PyGhidra")
File binRepoDir = file("${BIN_REPO}/ExternalPyWheels")
def dir = depsDir.exists() ? depsDir : binRepoDir
doFirst {
commandLine "$PYTHON3_VENV", "-m", "pip", "install", "--no-index", "-f", "${ROOT_PROJECT_DIR}/build/typestubs/dist", "ghidra-stubs"
}
}
// Install PyGhidra in editable mode to the development virtual environment // Install PyGhidra in editable mode to the development virtual environment
task installEditablePyGhidra(type: Exec) { task installEditablePyGhidra(type: Exec) {
dependsOn("installJPype") dependsOn("installJPype")
dependsOn("installGhidraStubs")
File depsDir = file("${DEPS_DIR}/PyGhidra") File depsDir = file("${DEPS_DIR}/PyGhidra")
File binRepoDir = file("${BIN_REPO}/ExternalPyWheels") File binRepoDir = file("${BIN_REPO}/ExternalPyWheels")

View File

@ -236,23 +236,21 @@ task createGhidraStubsWheel {
dependsOn("createPythonTypeStubs") dependsOn("createPythonTypeStubs")
String ROOT_PROJECT_DIR = rootProject.projectDir.toString() def typestubsDir = file("${ROOT_PROJECT_DIR}/build/typestubs").getAbsolutePath()
def distDir = file("${typestubsDir}/dist").getAbsolutePath()
def cwd = file(ROOT_PROJECT_DIR + "/build/typestubs") it.outputs.file("${distDir}/ghidra_stubs-${project.version}-py3-none-any.whl")
def destinationDir = file(cwd.toString() + "/dist")
it.outputs.file(destinationDir.toString() + "/ghidra_stubs-${project.version}-py3-none-any.whl")
doFirst { doFirst {
copy { copy {
from(file(ROOT_PROJECT_DIR + "/LICENSE")) from(file("${ROOT_PROJECT_DIR}/LICENSE"))
into cwd into typestubsDir
} }
def manifest = file(cwd.toString() + "/MANIFEST.in" ) def manifest = file("${typestubsDir}/MANIFEST.in")
manifest.write("graft src\n") manifest.write("graft src\n")
def pyproject = file(cwd.toString() + "/pyproject.toml" ) def pyproject = file("${typestubsDir}/pyproject.toml" )
pyproject.write("""\ pyproject.write("""\
[build-system] [build-system]
requires = ["setuptools", "wheel"] requires = ["setuptools", "wheel"]
@ -272,14 +270,14 @@ task createGhidraStubsWheel {
doLast { doLast {
File setuptools = project(":Debugger-rmi-trace").findPyDep(".") File setuptools = project(":Debugger-rmi-trace").findPyDep(".")
if (rootProject.PYTHON3 == null) { if (PYTHON3 == null) {
throw new GradleException("A supported version of Python ${SUPPORTED_PY_VERSIONS} was not found!") throw new GradleException("A supported version of Python ${SUPPORTED_PY_VERSIONS} was not found!")
} }
exec { exec {
workingDir { cwd.toString() } workingDir { typestubsDir }
commandLine rootProject.PYTHON3 commandLine PYTHON3
args "-m", "pip", "wheel", "-w", destinationDir.toString(), "--no-index", "-f", setuptools, "." args "-m", "pip", "wheel", "-w", distDir, "--no-index", "-f", setuptools, "."
} }
} }
} }
@ -431,8 +429,11 @@ task assembleDistribution (type: Copy) {
//////////////////////////// ////////////////////////////
// Ghidra Python type stubs // Ghidra Python type stubs
//////////////////////////// ////////////////////////////
from ("${ROOT_PROJECT_DIR}/build/typestubs/src") {
into 'docs/ghidra_stubs/typestubs'
}
from (createGhidraStubsWheel) { from (createGhidraStubsWheel) {
into 'docs' into 'docs/ghidra_stubs'
} }
//////////////// ////////////////