Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2165)

Side by Side Diff: ports/ipython_extension/python.cc

Issue 165473002: Adds an IPython kernel running in NaCl, and a wrapper as a Chrome extension. Base URL: https://naclports.googlecode.com/svn/trunk/src
Patch Set: Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 #include <python2.7/Python.h>
Sam Clegg 2014/02/18 23:39:31 Copyright
2 #include <libtar.h>
3 #include <stdio.h>
4 #include <locale.h>
5 #include <fcntl.h>
6 #include <sys/mount.h>
7 #include <errno.h>
8
9 #include "ppapi/cpp/var.h"
10 #include "ppapi/cpp/var_array.h"
11 #include "ppapi/cpp/var_dictionary.h"
12 #include "ppapi_simple/ps_interface.h"
13
14 #include "nacl_io/nacl_io.h"
15 #include "ppapi_simple/ps_main.h"
16 #include "ppapi_simple/ps_instance.h"
17
18 #ifdef __pnacl__
19 #define DATA_FILE "pydata_pnacl.tar"
20 #else
21 #error "Unknown arch"
22 #endif
23
24 static int setup_unix_environment() {
25 int ret = umount("/");
26 if (ret) {
27 printf("unmounting root fs failed\n");
28 return 1;
29 }
30
31 ret = mount("", "/", "memfs", 0, NULL);
32 if (ret) {
33 printf("mounting root fs failed\n");
34 return 1;
35 }
36
37 const char* data_url = getenv("NACL_DATA_URL");
38 if (!data_url)
39 data_url = "./";
40 mkdir("/mnt/http", 0777);
41 ret = mount(data_url, "/mnt/http", "httpfs", 0, "allow_cross_origin_requests :true allow_credentials:false");
42 if (ret) {
43 printf("mounting http filesystem failed\n");
44 return 1;
45 }
46
47 char filename[PATH_MAX];
48 strcpy(filename, "/mnt/http/");
49 strcat(filename, DATA_FILE);
50 TAR* tar;
51 ret = tar_open(&tar, filename, NULL, O_RDONLY, 0, 0);
52 if (ret) {
53 printf("error opening %s\n", filename);
54 return 1;
55 }
56
57 mkdir("/lib", 0777);
58 ret = tar_extract_all(tar, (char *)"/");
59 if (ret) {
60 printf("error extracting %s\n", filename);
61 return 1;
62 }
63
64 ret = tar_close(tar);
65
66 setenv("PYTHONHOME", "", 1);
67 return 0;
68 }
69
70 extern "C" {
71
72 static PyObject * post_json_message(PyObject * self, PyObject * args) {
73 char * stream;
74 char * json;
75 if (!PyArg_ParseTuple(args, "ss", &stream, &json)) {
76 return NULL;
77 }
78
79 pp::VarDictionary message;
80 message.Set("stream", pp::Var(stream));
81 message.Set("json", pp::Var(json));
82
83 const PPB_Messaging * messaging = PSInterfaceMessaging();
84 PP_Instance instance = PSGetInstanceId();
85 messaging->PostMessage(instance, message.pp_var());
86
87 Py_RETURN_NONE;
88 }
89
90 static PyObject * acquire_json_message_wait(PyObject *self, PyObject * args) {
91 if (!PyArg_ParseTuple(args, "")) {
92 return NULL;
93 }
94
95 const PPB_Messaging * messaging = PSInterfaceMessaging();
96 PP_Instance instance = PSGetInstanceId();
97 PSEventSetFilter(PSE_INSTANCE_HANDLEMESSAGE);
98 PSEvent *event = PSEventWaitAcquire();
99 if (event->type != PSE_INSTANCE_HANDLEMESSAGE)
100 Py_RETURN_NONE;
101
102 pp::Var message(event->as_var);
103
104 if (!message.is_dictionary())
105 Py_RETURN_NONE;
106
107 pp::VarDictionary request(message);
108 pp::Var json(request.Get("json"));
109 if (!json.is_string())
110 Py_RETURN_NONE;
111
112 return PyString_FromString(json.AsString().c_str());
113 }
114
115 }
116
117 static PyMethodDef PPMessageMethods[] = {
118 {
119 "_PostJSONMessage",
120 post_json_message,
121 METH_VARARGS,
122 "Post a message encoded as JSON"
123 },
124 {
125 "_AcquireJSONMessageWait",
126 acquire_json_message_wait,
127 METH_VARARGS,
128 "Acquire a message encoded as JSON (blocking)"},
129 {NULL, NULL, 0, NULL}
130 };
131
132 int python_main(int argc, char **argv) {
133 printf("Setting up unix environment...\n");
134 if (setup_unix_environment()) {
135 printf("Error: %s\n", strerror(errno));
136 return -1;
137 }
138 printf("done\n");
139
140 // Initialize Pepper API
141 PSInterfaceInit();
142
143 int quit = 0;
144
145 while(!quit) {
146 // Initialize Python interpreter
147 Py_Initialize();
148
149 // Load module that provides access to Pepper messaging API
150 // from within the interpreter
151 Py_InitModule("ppmessage", PPMessageMethods);
152 Py_InitModule("pepper.message", PPMessageMethods);
153
154 // Run the interpreter main loop.
155 const char * main_filename = "/mnt/http/simple_shell_main.py";
156 FILE *main = fopen(main_filename, "r");
157 if (main == NULL) {
158 printf("failed to load interpreter code\n");
159 return -1;
160 }
161
162 quit = PyRun_SimpleFileEx(main, main_filename, 1);
163
164 Py_Finalize();
165 }
166
167 return 0;
168 }
169
170 PPAPI_SIMPLE_REGISTER_MAIN(python_main)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698