@@ -94,84 +94,32 @@ void android_main(struct android_app* state) {
9494 LOGI ("Internal data path is: %s" , state -> activity -> internalDataPath );
9595 LOGI ("External data path is: %s" , state -> activity -> externalDataPath );
9696
97- // inject our bootstrap code to redirect python stdin/stdout
98- PyRun_SimpleString (
99- "import sys, androidembed\n" \
100- "class LogFile(object):\n" \
101- " def __init__(self):\n" \
102- " self.buffer = ''\n" \
103- " def write(self, s):\n" \
104- " s = self.buffer + s\n" \
105- " lines = s.split(\"\\n\")\n" \
106- " for l in lines[:-1]:\n" \
107- " androidembed.log(l)\n" \
108- " self.buffer = lines[-1]\n" \
109- " def flush(self):\n" \
110- " return\n" \
111- "sys.stdout = sys.stderr = LogFile()\n" );
112-
113- // let python knows where the python2.7 library is within the APK
114- PyRun_SimpleString (
115- "import sys, posix;" \
116- "lib_path = '{}/assets/lib/python2.7/'.format(" \
117- " posix.environ['ANDROID_APK_FN'])\n" \
118- "sys.path[:] = [lib_path, '{}/site-packages'.format(lib_path)]\n" \
119- "import os; from os.path import exists, join\n" \
120- "config_path = join(posix.environ['ANDROID_INTERNAL_DATA_PATH'], 'python2.7', 'config')\n" \
121- "if not exists(config_path): os.makedirs(config_path)\n" \
122- "import sysconfig\n" \
123- "sysconfig._get_makefile_filename = lambda: '{}/Makefile'.format(config_path)\n" \
124- "sysconfig.get_config_h_filename = lambda: '{}/pyconfig.h'.format(config_path)\n" \
125- );
126-
12797 // extract the Makefile, needed for sysconfig
12898 AAssetManager * am = state -> activity -> assetManager ;
129- char dest_fn [512 ];
130-
131- snprintf (dest_fn , 512 , "%s/python2.7/config/Makefile" , state -> activity -> internalDataPath );
132- if (asset_extract (am , "lib/python2.7/config/Makefile" , dest_fn ) < 0 )
133- return ;
134-
135- snprintf (dest_fn , 512 , "%s/python2.7/config/pyconfig.h" , state -> activity -> internalDataPath );
136- if (asset_extract (am , "include/python2.7/pyconfig.h" , dest_fn ) < 0 )
137- return ;
138-
139- // test import site
140- PyRun_SimpleString (
141- "import site; print site.getsitepackages()\n" \
142- "print 'Android path', sys.path\n" \
143- "print 'Android bootstrap done. __name__ is', __name__" );
144-
145- /* run it !
146- */
147- LOGI ("Extract main.py from assets" );
148- char main_fn [512 ];
149- snprintf (main_fn , 512 , "%s/main.pyo" , state -> activity -> internalDataPath );
150- if (asset_extract (am , "main.pyo" , main_fn ) < 0 )
99+ char bootstrap_fn [512 ];
100+ snprintf (bootstrap_fn , 512 , "%s/_bootstrap.py" , state -> activity -> internalDataPath );
101+ if (asset_extract (am , "_bootstrap.py" , bootstrap_fn ) < 0 ) {
102+ LOGW ("Unable to extract _bootstrap.py" );
151103 return ;
104+ }
152105
153- /* run python !
154- */
155- LOGI ("Run main.py >>>" );
156- FILE * fhd = fopen (main_fn , "rb" );
106+ // run the python bootstrap
107+ LOGI ("Run _bootstrap.py >>>" );
108+ FILE * fhd = fopen (bootstrap_fn , "rb" );
157109 if (fhd == NULL ) {
158- LOGW ("Cannot open main.pyo (errno=%d:%s)" , errno , strerror (errno ));
110+ LOGW ("Cannot open _bootstrap.py (errno=%d:%s)" , errno , strerror (errno ));
159111 return ;
160112 }
161- int ret = PyRun_SimpleFile (fhd , main_fn );
113+ int ret = PyRun_SimpleFile (fhd , bootstrap_fn );
162114 fclose (fhd );
163- LOGI ("Run main .py (ret=%d) <<<" , ret );
115+ LOGI ("Run _bootstrap .py (ret=%d) <<<" , ret );
164116
165117 if (PyErr_Occurred () != NULL ) {
166- LOGW ("An error occured." );
167- PyErr_Print (); /* This exits with the right code if SystemExit. */
118+ PyErr_Print ();
168119 if (Py_FlushLine ())
169120 PyErr_Clear ();
170121 }
171122
172- /* close everything
173- */
174123 Py_Finalize ();
175-
176- LOGW ("Python for android ended." );
124+ LOGI ("Python for android ended." );
177125}
0 commit comments