From f7537519ff10909349fd1ea47372e69dd44f7966 Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Tue, 26 May 2020 14:41:58 +0200 Subject: [PATCH] chg, make python aware of pm3 search paths --- client/pyscripts/ice.py | 7 +++-- client/src/cmdscript.c | 58 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 4 deletions(-) diff --git a/client/pyscripts/ice.py b/client/pyscripts/ice.py index a9d486813..9a223a059 100644 --- a/client/pyscripts/ice.py +++ b/client/pyscripts/ice.py @@ -1,5 +1,4 @@ +import os +import sys -lrc= 0x00 -for i in range(5): - lrc ^= 42 -print('\n final LRC XOR byte value: %02X\n' % (lrc)) +print("SP %s" % sys.path) diff --git a/client/src/cmdscript.c b/client/src/cmdscript.c index 4bbe3b631..cad94cafe 100644 --- a/client/src/cmdscript.c +++ b/client/src/cmdscript.c @@ -44,6 +44,9 @@ typedef enum { static int CmdHelp(const char *Cmd); #ifdef HAVE_PYTHON + +#define PYTHON_LIBRARIES_WILDCARD "?.py" + static int split(char *str, char **arr) { int begin_index = 0; int word_cnt = 0; @@ -67,6 +70,58 @@ static int split(char *str, char **arr) { } return word_cnt; } + +static void set_python_path(char *path) { + PyObject *syspath = PySys_GetObject("path"); + if (syspath == 0) { + PrintAndLogEx(WARNING, "Python failed to getobject"); + } + + PyObject *pName = PyUnicode_FromString(path); + if (PyList_Insert(syspath, 0, pName)) { + PrintAndLogEx(WARNING, "Error inserting extra path into sys.path list"); + } + + if (PySys_SetObject("path", syspath)) { + PrintAndLogEx(WARNING,"Error setting sys.path object"); + } +} + +static void set_python_paths(void) { + //--add to the LUA_PATH (package.path in lua) + // so we can load scripts from various places: + const char *exec_path = get_my_executable_directory(); + if (exec_path != NULL) { + // from the ./luascripts/ directory + char scripts_path[strlen(exec_path) + strlen(PYTHON_SCRIPTS_SUBDIR) + strlen(PYTHON_LIBRARIES_WILDCARD) + 1]; + strcpy(scripts_path, exec_path); + strcat(scripts_path, PYTHON_SCRIPTS_SUBDIR); +// strcat(scripts_path, PYTHON_LIBRARIES_WILDCARD); + set_python_path(scripts_path); + } + + const char *user_path = get_my_user_directory(); + if (user_path != NULL) { + // from the $HOME/.proxmark3/luascripts/ directory + char scripts_path[strlen(user_path) + strlen(PM3_USER_DIRECTORY) + strlen(PYTHON_SCRIPTS_SUBDIR) + strlen(PYTHON_LIBRARIES_WILDCARD) + 1]; + strcpy(scripts_path, user_path); + strcat(scripts_path, PM3_USER_DIRECTORY); + strcat(scripts_path, PYTHON_SCRIPTS_SUBDIR); +// strcat(scripts_path, PYTHON_LIBRARIES_WILDCARD); + set_python_path(scripts_path); + + } + + if (exec_path != NULL) { + // from the $PREFIX/share/proxmark3/luascripts/ directory + char scripts_path[strlen(exec_path) + strlen(PM3_SHARE_RELPATH) + strlen(PYTHON_SCRIPTS_SUBDIR) + strlen(PYTHON_LIBRARIES_WILDCARD) + 1]; + strcpy(scripts_path, exec_path); + strcat(scripts_path, PM3_SHARE_RELPATH); + strcat(scripts_path, PYTHON_SCRIPTS_SUBDIR); +// strcat(scripts_path, PYTHON_LIBRARIES_WILDCARD); + set_python_path(scripts_path); + } +} #endif /** @@ -247,6 +302,9 @@ static int CmdScriptRun(const char *Cmd) { free(argv[i]); } + // setup search paths. + set_python_paths(); + FILE *f = fopen(script_path, "r"); if (f == NULL) { PrintAndLogEx(ERR, "Could open file " _YELLOW_("%s"), script_path);