Skip to content

Commit 3dd1b27

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 696a424 commit 3dd1b27

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
@@ -57,6 +57,21 @@ public String getAppRoot() {
5757
return app_root;
5858
}
5959

60+
public String getEntryPoint(String search_dir) {
61+
/* Get the main file (.pyc|.pyo|.py) depending on if we
62+
* have a compiled version or not.
63+
*/
64+
List<String> entryPoints = new ArrayList<String>();
65+
entryPoints.add("main.pyo"); // python 2 compiled files
66+
entryPoints.add("main.pyc"); // python 3 compiled files
67+
for (String value : entryPoints) {
68+
File mainFile = new File(search_dir + "/" + value);
69+
if (mainFile.exists()) {
70+
return value;
71+
}
72+
}
73+
return "main.py";
74+
}
6075

6176
@Override
6277
protected void onCreate(Bundle savedInstanceState) {
@@ -143,7 +158,8 @@ protected void onPostExecute(String result) {
143158
File path = new File(getIntent().getData().getSchemeSpecificPart());
144159

145160
Project p = Project.scanDirectory(path);
146-
SDLActivity.nativeSetEnv("ANDROID_ENTRYPOINT", p.dir + "/main.py");
161+
String entry_point = getEntryPoint(p.dir);
162+
SDLActivity.nativeSetEnv("ANDROID_ENTRYPOINT", p.dir + "/" + entry_point);
147163
SDLActivity.nativeSetEnv("ANDROID_ARGUMENT", p.dir);
148164
SDLActivity.nativeSetEnv("ANDROID_APP_PATH", p.dir);
149165

@@ -164,7 +180,8 @@ protected void onPostExecute(String result) {
164180
// pass
165181
}
166182
} else {
167-
SDLActivity.nativeSetEnv("ANDROID_ENTRYPOINT", "main.pyo");
183+
String entry_point = getEntryPoint(app_root_dir);
184+
SDLActivity.nativeSetEnv("ANDROID_ENTRYPOINT", entry_point);
168185
SDLActivity.nativeSetEnv("ANDROID_ARGUMENT", app_root_dir);
169186
SDLActivity.nativeSetEnv("ANDROID_APP_PATH", app_root_dir);
170187
}
@@ -347,9 +364,10 @@ public static void start_service(String serviceTitle, String serviceDescription,
347364
String argument = PythonActivity.mActivity.getFilesDir().getAbsolutePath();
348365
String filesDirectory = argument;
349366
String app_root_dir = PythonActivity.mActivity.getAppRoot();
367+
String entry_point = PythonActivity.mActivity.getEntryPoint(app_root_dir + "/service");
350368
serviceIntent.putExtra("androidPrivate", argument);
351369
serviceIntent.putExtra("androidArgument", app_root_dir);
352-
serviceIntent.putExtra("serviceEntrypoint", "service/main.pyo");
370+
serviceIntent.putExtra("serviceEntrypoint", "service/" + entry_point);
353371
serviceIntent.putExtra("pythonName", "python");
354372
serviceIntent.putExtra("pythonHome", app_root_dir);
355373
serviceIntent.putExtra("pythonPath", app_root_dir + ":" + app_root_dir + "/lib");

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)