Skip to content

Commit 2b03069

Browse files
committed
Add ability to get the entry point dynamically for our PythonActivity.java files
Because as of Python 3.5, the .pyo filename extension is no longer used and has been eliminated in favour of .pyc, and we recently restored the capacity to compile our python installation files, so we must update this files in order to make it work the compiled files for both versions of python Note: this changes affects all bootstraps excepts pygame, because the pygame bootstrap can only be used with python2legacy and the extension of the compiled files are already set to .pyc...which is fine for now...because we cannot use the pygame bootstrap with python3 See also: `PEP 488 -- Elimination of PYO files` (https://www.python.org/dev/peps/pep-0488/)
1 parent 8d148cf commit 2b03069

File tree

3 files changed

+61
-7
lines changed

3 files changed

+61
-7
lines changed

pythonforandroid/bootstraps/sdl2/build/src/main/java/org/kivy/android/PythonActivity.java

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,8 @@ protected void onPostExecute(String result) {
150150
File path = new File(getIntent().getData().getSchemeSpecificPart());
151151

152152
Project p = Project.scanDirectory(path);
153-
SDLActivity.nativeSetEnv("ANDROID_ENTRYPOINT", p.dir + "/main.py");
153+
String entry_point = getEntryPoint(p.dir);
154+
SDLActivity.nativeSetEnv("ANDROID_ENTRYPOINT", p.dir + "/" + entry_point);
154155
SDLActivity.nativeSetEnv("ANDROID_ARGUMENT", p.dir);
155156
SDLActivity.nativeSetEnv("ANDROID_APP_PATH", p.dir);
156157

@@ -171,7 +172,8 @@ protected void onPostExecute(String result) {
171172
// pass
172173
}
173174
} else {
174-
SDLActivity.nativeSetEnv("ANDROID_ENTRYPOINT", "main.pyo");
175+
String entry_point = getEntryPoint(app_root_dir);
176+
SDLActivity.nativeSetEnv("ANDROID_ENTRYPOINT", entry_point);
175177
SDLActivity.nativeSetEnv("ANDROID_ARGUMENT", app_root_dir);
176178
SDLActivity.nativeSetEnv("ANDROID_APP_PATH", app_root_dir);
177179
}
@@ -368,9 +370,10 @@ public static void start_service(String serviceTitle, String serviceDescription,
368370
String argument = PythonActivity.mActivity.getFilesDir().getAbsolutePath();
369371
String filesDirectory = argument;
370372
String app_root_dir = PythonActivity.mActivity.getAppRoot();
373+
String entry_point = PythonActivity.mActivity.getEntryPoint(app_root_dir + "/service");
371374
serviceIntent.putExtra("androidPrivate", argument);
372375
serviceIntent.putExtra("androidArgument", app_root_dir);
373-
serviceIntent.putExtra("serviceEntrypoint", "service/main.pyo");
376+
serviceIntent.putExtra("serviceEntrypoint", "service/" + entry_point);
374377
serviceIntent.putExtra("pythonName", "python");
375378
serviceIntent.putExtra("pythonHome", app_root_dir);
376379
serviceIntent.putExtra("pythonPath", app_root_dir + ":" + app_root_dir + "/lib");
@@ -463,6 +466,21 @@ public void run() {
463466
});
464467
}
465468

469+
public String getEntryPoint(String search_dir) {
470+
/* Get the main file (.pyc|.pyo|.py) depending on if we
471+
* have a compiled version or not.
472+
*/
473+
List<String> entryPoints = new ArrayList<String>();
474+
entryPoints.add("main.pyo"); // python 2 compiled files
475+
entryPoints.add("main.pyc"); // python 3 compiled files
476+
for (String value : entryPoints) {
477+
File mainFile = new File(search_dir + "/" + value);
478+
if (mainFile.exists()) {
479+
return value;
480+
}
481+
}
482+
return "main.py";
483+
}
466484

467485
protected void showLoadingScreen() {
468486
// load the bitmap

pythonforandroid/bootstraps/service_only/build/src/main/java/org/kivy/android/PythonActivity.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,22 @@ public String getAppRoot() {
7474
return app_root;
7575
}
7676

77+
public String getEntryPoint(String search_dir) {
78+
/* Get the main file (.pyc|.pyo|.py) depending on if we
79+
* have a compiled version or not.
80+
*/
81+
List<String> entryPoints = new ArrayList<String>();
82+
entryPoints.add("main.pyo"); // python 2 compiled files
83+
entryPoints.add("main.pyc"); // python 3 compiled files
84+
for (String value : entryPoints) {
85+
File mainFile = new File(search_dir + "/" + value);
86+
if (mainFile.exists()) {
87+
return value;
88+
}
89+
}
90+
return "main.py";
91+
}
92+
7793
public static void initialize() {
7894
// The static nature of the singleton and Android quirkiness force us to initialize everything here
7995
// Otherwise, when exiting the app and returning to it, these variables *keep* their pre exit values
@@ -142,9 +158,10 @@ public void onClick(DialogInterface dialog,int id) {
142158
// Set up the Python environment
143159
String app_root_dir = getAppRoot();
144160
String mFilesDirectory = mActivity.getFilesDir().getAbsolutePath();
161+
String entry_point = getEntryPoint(app_root_dir);
145162

146163
Log.v(TAG, "Setting env vars for start.c and Python to use");
147-
PythonActivity.nativeSetEnv("ANDROID_ENTRYPOINT", "main.pyo");
164+
PythonActivity.nativeSetEnv("ANDROID_ENTRYPOINT", entry_point);
148165
PythonActivity.nativeSetEnv("ANDROID_ARGUMENT", app_root_dir);
149166
PythonActivity.nativeSetEnv("ANDROID_APP_PATH", app_root_dir);
150167
PythonActivity.nativeSetEnv("ANDROID_PRIVATE", mFilesDirectory);
@@ -368,9 +385,10 @@ public static void start_service(String serviceTitle, String serviceDescription,
368385
String argument = PythonActivity.mActivity.getFilesDir().getAbsolutePath();
369386
String filesDirectory = argument;
370387
String app_root_dir = PythonActivity.mActivity.getAppRoot();
388+
String entry_point = PythonActivity.mActivity.getEntryPoint(app_root_dir + "/service");
371389
serviceIntent.putExtra("androidPrivate", argument);
372390
serviceIntent.putExtra("androidArgument", app_root_dir);
373-
serviceIntent.putExtra("serviceEntrypoint", "service/main.pyo");
391+
serviceIntent.putExtra("serviceEntrypoint", "service/" + entry_point);
374392
serviceIntent.putExtra("pythonName", "python");
375393
serviceIntent.putExtra("pythonHome", app_root_dir);
376394
serviceIntent.putExtra("pythonPath", app_root_dir + ":" + app_root_dir + "/lib");

pythonforandroid/bootstraps/webview/build/src/main/java/org/kivy/android/PythonActivity.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,22 @@ public String getAppRoot() {
8181
return app_root;
8282
}
8383

84+
public String getEntryPoint(String search_dir) {
85+
/* Get the main file (.pyc|.pyo|.py) depending on if we
86+
* have a compiled version or not.
87+
*/
88+
List<String> entryPoints = new ArrayList<String>();
89+
entryPoints.add("main.pyo"); // python 2 compiled files
90+
entryPoints.add("main.pyc"); // python 3 compiled files
91+
for (String value : entryPoints) {
92+
File mainFile = new File(search_dir + "/" + value);
93+
if (mainFile.exists()) {
94+
return value;
95+
}
96+
}
97+
return "main.py";
98+
}
99+
84100
public static void initialize() {
85101
// The static nature of the singleton and Android quirkyness force us to initialize everything here
86102
// Otherwise, when exiting the app and returning to it, these variables *keep* their pre exit values
@@ -170,9 +186,10 @@ public boolean shouldOverrideUrlLoading(WebView view, String url) {
170186
setContentView(mLayout);
171187

172188
String mFilesDirectory = mActivity.getFilesDir().getAbsolutePath();
189+
String entry_point = getEntryPoint(app_root_dir);
173190

174191
Log.v(TAG, "Setting env vars for start.c and Python to use");
175-
PythonActivity.nativeSetEnv("ANDROID_ENTRYPOINT", "main.pyo");
192+
PythonActivity.nativeSetEnv("ANDROID_ENTRYPOINT", entry_point);
176193
PythonActivity.nativeSetEnv("ANDROID_ARGUMENT", app_root_dir);
177194
PythonActivity.nativeSetEnv("ANDROID_APP_PATH", app_root_dir);
178195
PythonActivity.nativeSetEnv("ANDROID_PRIVATE", mFilesDirectory);
@@ -425,9 +442,10 @@ public static void start_service(String serviceTitle, String serviceDescription,
425442
String argument = PythonActivity.mActivity.getFilesDir().getAbsolutePath();
426443
String filesDirectory = argument;
427444
String app_root_dir = PythonActivity.mActivity.getAppRoot();
445+
String entry_point = PythonActivity.mActivity.getEntryPoint(app_root_dir + "/service");
428446
serviceIntent.putExtra("androidPrivate", argument);
429447
serviceIntent.putExtra("androidArgument", app_root_dir);
430-
serviceIntent.putExtra("serviceEntrypoint", "service/main.pyo");
448+
serviceIntent.putExtra("serviceEntrypoint", "service/" + entry_point);
431449
serviceIntent.putExtra("pythonName", "python");
432450
serviceIntent.putExtra("pythonHome", app_root_dir);
433451
serviceIntent.putExtra("pythonPath", app_root_dir + ":" + app_root_dir + "/lib");

0 commit comments

Comments
 (0)