2 #include <netinet/in.h>
4 #include "lutf_python.h"
5 #include "lutf_listener.h"
7 extern lutf_config_params_t g_lutf_cfg;
10 static char *get_path_segment(char *path, int *len, char **more)
12 char *found = strchr(path, ':');
22 if (*(found + 1) == '\n')
30 static lutf_rc_t 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;
37 lutf_rc_t rc = EN_LUTF_RC_OK;
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");
79 lutf_listener_shutdown();
80 return EN_LUTF_RC_TIMEOUT;
83 /* update the LUTF internal database */
84 PyRun_SimpleString("agents.reload()");
85 PDEBUG("Agents reloaded. Dumping");
86 PyRun_SimpleString("agents.dump()");
88 /* if a script is specified then a unique suite must be
90 * if a suite_list is specified it takes precedence over
91 * a single suite parameters. A pattern of scripts can be
92 * provided to run matching scripts.
93 * If a suite parameter is provided then run that
95 * Otherwise just run everything.
97 if (g_lutf_cfg.script && strlen(g_lutf_cfg.script) > 0) {
98 snprintf(buf, MAX_STR_LEN,
99 "suites['%s'].scripts['%s'].run()",
102 } else if (g_lutf_cfg.suite_list &&
103 strlen(g_lutf_cfg.suite_list) > 0) {
104 snprintf(buf, MAX_STR_LEN,
105 "suites.run(suite_list='%s', match='%s')",
106 g_lutf_cfg.suite_list, pattern);
107 } else if (g_lutf_cfg.suite && strlen(g_lutf_cfg.suite) > 0) {
108 snprintf(buf, MAX_STR_LEN,
109 "suites['%s'].run('%s')",
110 g_lutf_cfg.suite, pattern);
112 snprintf(buf, MAX_STR_LEN,
113 "suites.run(match='%s')", pattern);
116 PyRun_SimpleString(buf);
117 snprintf(buf, MAX_STR_LEN,
118 "dumpGlobalTestResults('%s')", g_lutf_cfg.results_file);
120 PyRun_SimpleString(buf);
121 PDEBUG("Shutting down the LUTF");
122 PyRun_SimpleString("me.exit()");
123 lutf_listener_shutdown();
124 return EN_LUTF_RC_OK;
125 } else if (g_lutf_cfg.shell == EN_LUTF_RUN_INTERACTIVE) {
129 PDEBUG("Running in Interactive mode");
131 * start an independent shell
132 * Since we imported all the necessary modules to start in
133 * the main interpreter, copying the globals should copy
134 * them in the interactive shell.
136 PyRun_SimpleString("vars = globals().copy()\n");
137 PyRun_SimpleString("vars.update(locals())\n");
138 PyRun_SimpleString("shell = code.InteractiveConsole(vars)\n");
139 PyRun_SimpleString("shell.push('sys.ps1 = \"lutf>>> \"')\n");
140 PyRun_SimpleString("shell.push('sys.ps2 = \"lutf... \"')\n");
142 /* import base lutf module */
144 intro = "shell.interact(\"Welcome to the Lustre Unit Test Framework (LUTF)\\n\""
145 "\"Convenience Functions: R() = dumpGlobalTestResults(), A() = agents.dump(), I() = me.dump_intfs(), X() = me.exit()\")";
146 rc = PyRun_SimpleString(intro);
148 goto python_shutdown;
150 /* run the telnet server. This becomes our main process
153 PDEBUG("Running in Daemon mode");
154 sprintf(segment, "fname = os.path.join('%s', '%s')\n",
155 g_lutf_cfg.tmp_dir, OUT_PY_LOG);
156 if (PyRun_SimpleString(segment)) {
157 PERROR("Failed to create log file");
158 rc = EN_LUTF_RC_FAIL;
159 goto python_shutdown;
161 sprintf(segment, "logfile = open(fname, 'w')\n");
162 if (PyRun_SimpleString(segment)) {
163 PERROR("Failed to open log file");
164 rc = EN_LUTF_RC_FAIL;
165 goto python_shutdown;
167 if (PyRun_SimpleString("sys.stdout = sys.stderr = logfile\n")) {
168 PERROR("Failed to redirect stdout and stderr");
169 rc = EN_LUTF_RC_FAIL;
170 goto python_shutdown;
172 if (PyRun_SimpleString("from lutf_telnet_sr import LutfTelnetServer\n")) {
173 PERROR("Failed to import LutfTelnetServer");
174 rc = EN_LUTF_RC_FAIL;
175 goto python_shutdown;
177 sprintf(segment, "tns = LutfTelnetServer(%d)\n",
178 g_lutf_cfg.l_info.hb_info.agent_telnet_port);
179 if (PyRun_SimpleString(segment)) {
180 PERROR("Failed to instantiate LutfTelnetServer");
181 rc = EN_LUTF_RC_FAIL;
182 goto python_shutdown;
184 if (PyRun_SimpleString("tns.run()\n")) {
185 PERROR("Failed to run LutfTelnetServer instance");
186 rc = EN_LUTF_RC_FAIL;
187 goto python_shutdown;
189 if (PyRun_SimpleString("logfile.close()")) {
190 PERROR("Failed to close logfile");
191 rc = EN_LUTF_RC_FAIL;
192 goto python_shutdown;
195 PERROR("Exiting the python interpreter");
198 lutf_listener_shutdown();
204 * gcc py.c -o py -I/usr/local/include/python2.7
205 * -L/usr/local/lib/python2.7/config -lm -ldl -lpthread -lutil -lpython2.7
207 lutf_rc_t python_init(void)
212 swprintf(program, 3, L"%hs", "lutf");
215 //char new_path[MAX_STR_LEN];
216 Py_SetProgramName(program);
219 //py_args[0] = argv[0];
223 //sprintf(new_path, "%s:%s", path, script_path);
224 //PySys_SetPath(new_path);
225 //path = Py_GetPath();
227 rc = python_run_interactive_shell();
228 PDEBUG("Python finalizing");
232 PDEBUG("Python finalized");
237 lutf_rc_t python_handle_rpc_request(char *rpc)
239 lutf_rc_t rc = EN_LUTF_RC_OK;
240 PyGILState_STATE gstate;
241 PyObject *handle_rpc_req;
249 return EN_LUTF_RC_PY_SCRIPT_FAIL;
253 gstate = PyGILState_Ensure();
255 str = PyUnicode_FromString((char *)"lutf");
256 lutf = PyImport_Import(str);
257 me = PyObject_GetAttrString(lutf, (char *)"me");
258 handle_rpc_req = PyObject_GetAttrString(me, (char *)"handle_rpc_req");
259 args = PyTuple_Pack(1, PyUnicode_FromString(rpc));
260 result = PyObject_CallObject(handle_rpc_req, args);
263 PDEBUG("handle_rpc_req() didn't return any values");
265 PyGILState_Release(gstate);