2 #include <netinet/in.h>
4 #include "lutf_python.h"
5 #include "lutf_listener.h"
7 extern lutf_config_params_t g_lutf_cfg;
8 bool g_py_inited = false;
10 static char *get_path_segment(char *path, int *len, char **more)
12 char *found = strchr(path, ':');
22 if (*(found + 1) == '\n')
30 static void python_run_interactive_shell(void)
32 char buf[MAX_STR_LEN + 20];
33 char segment[MAX_STR_LEN];
35 char *more = g_lutf_cfg.py_path;
39 PyRun_SimpleString("import code\n");
40 PyRun_SimpleString("import os\n");
41 PyRun_SimpleString("import sys\n");
42 PyRun_SimpleString("import readline\n");
44 /* all other paths are figured out within python */
45 snprintf(buf, MAX_STR_LEN,
46 "sys.path.append(os.path.join('%s', 'python', 'infra'))",
47 g_lutf_cfg.lutf_path);
48 PyRun_SimpleString(buf);
50 snprintf(buf, MAX_STR_LEN,
51 "sys.path.append(\"%s/src\")\n",
52 g_lutf_cfg.lutf_path);
53 PyRun_SimpleString(buf);
55 while (more != NULL) {
56 seg = get_path_segment(more, &len, &more);
57 snprintf(segment, len+1, "%s", seg);
59 snprintf(buf, sizeof(buf),
60 "sys.path.append(\"%s\")\n", segment);
61 PyRun_SimpleString(buf);
64 PyRun_SimpleString("import lutf\n");
65 PyRun_SimpleString("from lutf import me,suites,"
66 "agents,dumpGlobalTestResults,R,A,I,X\n");
70 if (g_lutf_cfg.shell == EN_LUTF_RUN_BATCH &&
71 g_lutf_cfg.l_info.type == EN_LUTF_MASTER) {
72 char *pattern = g_lutf_cfg.pattern;
74 PDEBUG("Running in Batch mode. Checking Agents are connected");
76 rc = wait_for_agents(g_lutf_cfg.agents, 20);
77 if (rc == EN_LUTF_RC_TIMEOUT) {
78 PERROR("Not all agents connected. Aborting tests");
82 /* update the LUTF internal database */
83 PyRun_SimpleString("agents.reload()");
84 PDEBUG("Agents reloaded. Dumping");
85 PyRun_SimpleString("agents.dump()");
87 if (g_lutf_cfg.script && strlen(g_lutf_cfg.script) > 0) {
88 snprintf(buf, MAX_STR_LEN,
89 "suites['%s'].scripts['%s'].run()",
92 } else if (g_lutf_cfg.suite && strlen(g_lutf_cfg.suite) > 0) {
93 snprintf(buf, MAX_STR_LEN,
94 "suites['%s'].run('%s')",
95 g_lutf_cfg.suite, pattern);
97 snprintf(buf, MAX_STR_LEN,
98 "suites.run('%s')", pattern);
101 PyRun_SimpleString(buf);
102 snprintf(buf, MAX_STR_LEN,
103 "dumpGlobalTestResults('%s')", g_lutf_cfg.results_file);
105 PyRun_SimpleString(buf);
106 PDEBUG("Shutting down the LUTF");
107 PyRun_SimpleString("me.exit()");
108 lutf_listener_shutdown();
110 } else if (g_lutf_cfg.shell == EN_LUTF_RUN_INTERACTIVE) {
114 PDEBUG("Running in Interactive mode");
116 * start an independent shell
117 * Since we imported all the necessary modules to start in
118 * the main interpreter, copying the globals should copy
119 * them in the interactive shell.
121 PyRun_SimpleString("vars = globals().copy()\n");
122 PyRun_SimpleString("vars.update(locals())\n");
123 PyRun_SimpleString("shell = code.InteractiveConsole(vars)\n");
124 PyRun_SimpleString("shell.push('sys.ps1 = \"lutf>>> \"')\n");
125 PyRun_SimpleString("shell.push('sys.ps2 = \"lutf... \"')\n");
127 /* import base lutf module */
129 intro = "shell.interact(\"Welcome to the Lustre Unit Test Framework (LUTF)\\n\""
130 "\"Convenience Functions: R() = dumpGlobalTestResults(), A() = agents.dump(), I() = me.dump_intfs(), X() = me.exit()\")";
131 rc = PyRun_SimpleString(intro);
133 goto python_shutdown;
135 /* run the telnet server. This becomes our main process
138 PDEBUG("Running in Daemon mode");
139 sprintf(segment, "fname = os.path.join('%s', '%s')\n",
140 g_lutf_cfg.lutf_path, OUT_PY_LOG);
141 if (PyRun_SimpleString(segment)) {
142 PERROR("Failed to create log file");
143 goto python_shutdown;
145 sprintf(segment, "logfile = open(fname, 'w')\n");
146 if (PyRun_SimpleString(segment)) {
147 PERROR("Failed to open log file");
148 goto python_shutdown;
150 if (PyRun_SimpleString("sys.stdout = sys.stderr = logfile\n")) {
151 PERROR("Failed to redirect stdout and stderr");
152 goto python_shutdown;
154 if (PyRun_SimpleString("from lutf_telnet_sr import LutfTelnetServer\n")) {
155 PERROR("Failed to import LutfTelnetServer");
156 goto python_shutdown;
158 sprintf(segment, "tns = LutfTelnetServer(%d)\n",
159 g_lutf_cfg.l_info.hb_info.agent_telnet_port);
160 if (PyRun_SimpleString(segment)) {
161 PERROR("Failed to instantiate LutfTelnetServer");
162 goto python_shutdown;
164 if (PyRun_SimpleString("tns.run()\n")) {
165 PERROR("Failed to run LutfTelnetServer instance");
166 goto python_shutdown;
168 if (PyRun_SimpleString("logfile.close()")) {
169 PERROR("Failed to close logfile");
170 goto python_shutdown;
173 PERROR("Exiting the python interpreter");
176 lutf_listener_shutdown();
180 * gcc py.c -o py -I/usr/local/include/python2.7
181 * -L/usr/local/lib/python2.7/config -lm -ldl -lpthread -lutil -lpython2.7
183 lutf_rc_t python_init(void)
187 swprintf(program, 3, L"%hs", "lutf");
190 //char new_path[MAX_STR_LEN];
191 Py_SetProgramName(program);
194 //py_args[0] = argv[0];
198 //sprintf(new_path, "%s:%s", path, script_path);
199 //PySys_SetPath(new_path);
200 //path = Py_GetPath();
202 python_run_interactive_shell();
203 PDEBUG("Python finalizing");
207 PDEBUG("Python finalized");
209 return EN_LUTF_RC_OK;
212 lutf_rc_t python_handle_rpc_request(char *rpc)
214 lutf_rc_t rc = EN_LUTF_RC_OK;
215 PyGILState_STATE gstate;
216 PyObject *handle_rpc_req;
224 return EN_LUTF_RC_PY_SCRIPT_FAIL;
228 gstate = PyGILState_Ensure();
230 str = PyUnicode_FromString((char*)"lutf");
231 lutf = PyImport_Import(str);
232 me = PyObject_GetAttrString(lutf, (char*)"me");
233 handle_rpc_req = PyObject_GetAttrString(me, (char*)"handle_rpc_req");
234 args = PyTuple_Pack(1, PyUnicode_FromString(rpc));
235 result = PyObject_CallObject(handle_rpc_req, args);
238 PDEBUG("handle_rpc_req() didn't return any values");
240 PyGILState_Release(gstate);