Merge branch 'GP-5444_ryanmkurtz_pyghidra-lock' into patch

(Closes #7536)
This commit is contained in:
Ryan Kurtz 2025-03-06 11:59:19 -05:00
commit 5a8c2fbfa6
3 changed files with 13 additions and 9 deletions

View file

@ -306,6 +306,10 @@ import pdb # imports Python's pdb
import pdb_ # imports Ghidra's pdb import pdb_ # imports Ghidra's pdb
``` ```
## Change History ## Change History
__2.0.2:__
* [`pyghidra.open_program()`](#pyghidraopen_program) now properly throws an exception if the project
exists and is locked.
__2.0.1:__ __2.0.1:__
* PyGhidra now respects the `application.settingsdir` property set in Ghidra's `launch.properties` * PyGhidra now respects the `application.settingsdir` property set in Ghidra's `launch.properties`
file. file.

View file

@ -13,7 +13,7 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
## ##
__version__ = "2.0.1" __version__ = "2.0.2"
# stub for documentation and typing # stub for documentation and typing
# this is mostly to hide the function parameter # this is mostly to hide the function parameter

View file

@ -85,7 +85,7 @@ def _setup_project(
) -> Tuple["GhidraProject", "Program"]: ) -> Tuple["GhidraProject", "Program"]:
from ghidra.base.project import GhidraProject from ghidra.base.project import GhidraProject
from java.lang import ClassLoader # type:ignore @UnresolvedImport from java.lang import ClassLoader # type:ignore @UnresolvedImport
from java.io import IOException # type:ignore @UnresolvedImport from ghidra.framework.model import ProjectLocator # type:ignore @UnresolvedImport
if binary_path is not None: if binary_path is not None:
binary_path = Path(binary_path) binary_path = Path(binary_path)
if project_location: if project_location:
@ -95,7 +95,6 @@ def _setup_project(
if not project_name: if not project_name:
project_name = f"{binary_path.name}_ghidra" project_name = f"{binary_path.name}_ghidra"
project_location /= project_name project_location /= project_name
project_location.mkdir(exist_ok=True, parents=True)
if isinstance(loader, str): if isinstance(loader, str):
from java.lang import ClassNotFoundException # type:ignore @UnresolvedImport from java.lang import ClassNotFoundException # type:ignore @UnresolvedImport
@ -113,13 +112,14 @@ def _setup_project(
# Open/Create project # Open/Create project
program: "Program" = None program: "Program" = None
try: if ProjectLocator(project_location, project_name).exists():
project = GhidraProject.openProject(project_location, project_name, True) project = GhidraProject.openProject(project_location, project_name, True)
else:
project_location.mkdir(exist_ok=True, parents=True)
project = GhidraProject.createProject(project_location, project_name, False)
if binary_path is not None: if binary_path is not None:
if project.getRootFolder().getFile(binary_path.name): if project.getRootFolder().getFile(binary_path.name):
program = project.openProgram("/", binary_path.name, False) program = project.openProgram("/", binary_path.name, False)
except IOException:
project = GhidraProject.createProject(project_location, project_name, False)
# NOTE: GhidraProject.importProgram behaves differently when a loader is provided # NOTE: GhidraProject.importProgram behaves differently when a loader is provided
# loaderClass may not be null so we must use the correct method override # loaderClass may not be null so we must use the correct method override