forked from kivy/python-for-android
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstart.c
More file actions
140 lines (119 loc) · 3.42 KB
/
start.c
File metadata and controls
140 lines (119 loc) · 3.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#define PY_SSIZE_T_CLEAN
#include "Python.h"
#ifndef Py_PYTHON_H
#error Python headers needed to compile C extensions, please install development version of Python.
#else
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "SDL.h"
#include "android/log.h"
#define LOG(x) __android_log_write(ANDROID_LOG_INFO, "python", (x))
static PyObject *androidembed_log(PyObject *self, PyObject *args) {
char *logstr = NULL;
if (!PyArg_ParseTuple(args, "s", &logstr)) {
return NULL;
}
LOG(logstr);
Py_RETURN_NONE;
}
static PyMethodDef AndroidEmbedMethods[] = {
{"log", androidembed_log, METH_VARARGS,
"Log on android platform"},
{NULL, NULL, 0, NULL}
};
PyMODINIT_FUNC initandroidembed(void) {
(void) Py_InitModule("androidembed", AndroidEmbedMethods);
}
int file_exists(const char * filename)
{
FILE *file;
if (file = fopen(filename, "r")) {
fclose(file);
return 1;
}
return 0;
}
int main(int argc, char **argv) {
char *env_argument = NULL;
int ret = 0;
FILE *fd;
LOG("Initialize Python for Android");
env_argument = getenv("ANDROID_ARGUMENT");
setenv("ANDROID_APP_PATH", env_argument, 1);
//setenv("PYTHONVERBOSE", "2", 1);
Py_SetProgramName(argv[0]);
Py_Initialize();
PySys_SetArgv(argc, argv);
/* ensure threads will work.
*/
PyEval_InitThreads();
/* our logging module for android
*/
initandroidembed();
/* inject our bootstrap code to redirect python stdin/stdout
* replace sys.path with our path
*/
PyRun_SimpleString(
"import sys, posix\n" \
"private = posix.environ['ANDROID_PRIVATE']\n" \
"argument = posix.environ['ANDROID_ARGUMENT']\n" \
"sys.path[:] = [ \n" \
" private + '/lib/python27.zip', \n" \
" private + '/lib/python2.7/', \n" \
" private + '/lib/python2.7/lib-dynload/', \n" \
" private + '/lib/python2.7/site-packages/', \n" \
" argument ]\n" \
"import androidembed\n" \
"class LogFile(object):\n" \
" def __init__(self):\n" \
" self.buffer = ''\n" \
" def write(self, s):\n" \
" s = self.buffer + s\n" \
" lines = s.split(\"\\n\")\n" \
" for l in lines[:-1]:\n" \
" androidembed.log(l)\n" \
" self.buffer = lines[-1]\n" \
"sys.stdout = sys.stderr = LogFile()\n" \
"import site; print site.getsitepackages()\n"\
"print 'Android path', sys.path\n" \
"print 'Android kivy bootstrap done. __name__ is', __name__");
/* run it !
*/
LOG("Run user program, change dir and execute main.py");
chdir(env_argument);
/* search the initial main.py
*/
char *main_py = "main.pyo";
if ( file_exists(main_py) == 0 ) {
if ( file_exists("main.py") )
main_py = "main.py";
else
main_py = NULL;
}
if ( main_py == NULL ) {
LOG("No main.pyo / main.py found.");
return -1;
}
fd = fopen(main_py, "r");
if ( fd == NULL ) {
LOG("Open the main.py(o) failed");
return -1;
}
/* run python !
*/
ret = PyRun_SimpleFile(fd, main_py);
if (PyErr_Occurred() != NULL) {
ret = 1;
PyErr_Print(); /* This exits with the right code if SystemExit. */
if (Py_FlushLine())
PyErr_Clear();
}
/* close everything
*/
Py_Finalize();
fclose(fd);
LOG("Python for android ended.");
return ret;
}
#endif