From 65330a7a093ea95fbd288652fab39a84ca15d65c Mon Sep 17 00:00:00 2001 From: ValekoZ Date: Wed, 5 Jun 2024 13:48:42 +0200 Subject: [PATCH 1/4] Add support for virtualenv Add a `gef.virtualenv_path` config var, and activate it at startup. --- gef.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/gef.py b/gef.py index c713c62ef..8c865e1e6 100644 --- a/gef.py +++ b/gef.py @@ -9854,6 +9854,9 @@ def __init__(self) -> None: plugins_dir = GefSetting("", str, "Autoload additional GEF commands from external directory", hooks={"on_write": [GefSetting.no_spaces, ]}) plugins_dir.add_hook("on_changed", [lambda _, new_val: GefSetting.must_exist(new_val), lambda _, new_val: self.load_extra_plugins(new_val), ]) gef.config["gef.extra_plugins_dir"] = plugins_dir + venv_path = GefSetting("", str, "Path to the virtualenv used by GEF", hooks={"on_write": [GefSetting.no_spaces, ]}) + venv_path.add_hook("on_changed", [lambda _, new_val: GefSetting.must_exist(new_val), lambda _, new_val: self.load_virtualenv(new_val), ]) + gef.config["gef.virtualenv_path"] = venv_path gef.config["gef.disable_color"] = GefSetting(False, bool, "Disable all colors in GEF") gef.config["gef.tempdir"] = GefSetting(GEF_TEMP_DIR, pathlib.Path, "Directory to use for temporary/cache content", hooks={"on_write": [GefSetting.no_spaces, GefSetting.create_folder_tree]}) gef.config["gef.show_deprecation_warnings"] = GefSetting(True, bool, "Toggle the display of the `deprecated` warnings") @@ -9943,6 +9946,13 @@ def load_plugins_from_directory(plugin_directory: pathlib.Path): dbg(f"Loading extra plugins from directory={directory}") return load_plugins_from_directory(directory) + + def load_virtualenv(self, new_path: Optional[pathlib.Path] = None): + path = new_path or pathlib.Path(gef.config["gef.virtualenv_path"]) + activate_script_path = path / "bin" / "activate_this.py" + if path: + exec(open(activate_script_path).read(), {'__file__': activate_script_path}) + @property def loaded_command_names(self) -> Iterable[str]: print("obsolete loaded_command_names") @@ -11717,6 +11727,9 @@ def target_remote_posthook(): gef.gdb.load() gef.gdb.show_banner() + # load venv + gef.gdb.load_virtualenv() + # load config gef.gdb.load_extra_plugins() From f5f1bc5cd63928f33206595c3a761d370aef235a Mon Sep 17 00:00:00 2001 From: ValekoZ Date: Wed, 5 Jun 2024 14:04:46 +0200 Subject: [PATCH 2/4] Fix error when no gef.virtualenv_path is provided --- gef.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/gef.py b/gef.py index 8c865e1e6..94cd9e2cf 100644 --- a/gef.py +++ b/gef.py @@ -9948,8 +9948,8 @@ def load_plugins_from_directory(plugin_directory: pathlib.Path): def load_virtualenv(self, new_path: Optional[pathlib.Path] = None): - path = new_path or pathlib.Path(gef.config["gef.virtualenv_path"]) - activate_script_path = path / "bin" / "activate_this.py" + path = new_path or gef.config["gef.virtualenv_path"] + activate_script_path = pathlib.Path(path) / "bin" / "activate_this.py" if path: exec(open(activate_script_path).read(), {'__file__': activate_script_path}) From 7c326024d3119622e07353a2b9ef44183e51015b Mon Sep 17 00:00:00 2001 From: ValekoZ Date: Wed, 5 Jun 2024 14:11:09 +0200 Subject: [PATCH 3/4] Move activate_script_path --- gef.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gef.py b/gef.py index 94cd9e2cf..0abc0b484 100644 --- a/gef.py +++ b/gef.py @@ -9949,8 +9949,8 @@ def load_plugins_from_directory(plugin_directory: pathlib.Path): def load_virtualenv(self, new_path: Optional[pathlib.Path] = None): path = new_path or gef.config["gef.virtualenv_path"] - activate_script_path = pathlib.Path(path) / "bin" / "activate_this.py" if path: + activate_script_path = pathlib.Path(path)/"bin"/"activate_this.py" exec(open(activate_script_path).read(), {'__file__': activate_script_path}) @property From 7e97732149e9488d9020f3af99d260dd60af21f8 Mon Sep 17 00:00:00 2001 From: ValekoZ Date: Sun, 9 Jun 2024 18:38:51 +0200 Subject: [PATCH 4/4] Add tests --- tests/config/virtualenv.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 tests/config/virtualenv.py diff --git a/tests/config/virtualenv.py b/tests/config/virtualenv.py new file mode 100644 index 000000000..7ddbf43b7 --- /dev/null +++ b/tests/config/virtualenv.py @@ -0,0 +1,27 @@ +""" +virtualenv config test module +""" + +from tests.base import RemoteGefUnitTestGeneric +from os import system +from tempfile import mktemp + + +class VirtualenvConfig(RemoteGefUnitTestGeneric): + """virtualenv config test module""" + + def setUp(self) -> None: + venv_path = mktemp() + system(f"virtualenv {venv_path}") + system(f"{venv_path}/bin/pip install numpy") + + self.venv_path = venv_path + + return super().setUp() + + def test_conf_venv(self): + gdb = self._gdb + gdb.execute(f"gef config gef.virtualenv_path {self.venv_path}") + + res = gdb.execute("pi __import__('numpy').test()", to_string=True) + assert 'NumPy version' in res