Skip to content

Commit 94ceace

Browse files
committed
android: add activity for running Python programs
It can be launched from the termux shell using the provided run_python.sh script, which can communicate with the Panda activity using a socket (which is the only way we can reliably get command-line output back to the program.) The Python script needs to be readable by the Panda activity (which implies it needs to be in /sdcard). The standard library is packed into the .apk, and loaded using zipimport. Extension modules are included using a special naming convention and import hook in order to comply with Android's strict demands on how libraries must be named to be included in an .apk. [skip ci]
1 parent 8e8283c commit 94ceace

File tree

7 files changed

+272
-12
lines changed

7 files changed

+272
-12
lines changed

makepanda/makepanda.py

Lines changed: 75 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5082,7 +5082,7 @@ def CreatePandaVersionFiles():
50825082
# DIRECTORY: panda/src/testbed/
50835083
#
50845084

5085-
if (not RTDIST and not RUNTIME and PkgSkip("PVIEW")==0 and GetTarget() != 'android'):
5085+
if (not RTDIST and not RUNTIME and PkgSkip("PVIEW")==0):
50865086
OPTS=['DIR:panda/src/testbed']
50875087
TargetAdd('pview_pview.obj', opts=OPTS, input='pview.cxx')
50885088
TargetAdd('pview.exe', input='pview_pview.obj')
@@ -5101,6 +5101,7 @@ def CreatePandaVersionFiles():
51015101
TargetAdd('org/panda3d/android/NativeIStream.class', opts=OPTS, input='NativeIStream.java')
51025102
TargetAdd('org/panda3d/android/NativeOStream.class', opts=OPTS, input='NativeOStream.java')
51035103
TargetAdd('org/panda3d/android/PandaActivity.class', opts=OPTS, input='PandaActivity.java')
5104+
TargetAdd('org/panda3d/android/PythonActivity.class', opts=OPTS, input='PythonActivity.java')
51045105

51055106
TargetAdd('p3android_composite1.obj', opts=OPTS, input='p3android_composite1.cxx')
51065107
TargetAdd('libp3android.dll', input='p3android_composite1.obj')
@@ -5111,17 +5112,28 @@ def CreatePandaVersionFiles():
51115112
TargetAdd('android_main.obj', opts=OPTS, input='android_main.cxx')
51125113

51135114
if (not RTDIST and PkgSkip("PVIEW")==0):
5114-
TargetAdd('pview_pview.obj', opts=OPTS, input='pview.cxx')
5115+
TargetAdd('libpview_pview.obj', opts=OPTS, input='pview.cxx')
51155116
TargetAdd('libpview.dll', input='android_native_app_glue.obj')
51165117
TargetAdd('libpview.dll', input='android_main.obj')
5117-
TargetAdd('libpview.dll', input='pview_pview.obj')
5118+
TargetAdd('libpview.dll', input='libpview_pview.obj')
51185119
TargetAdd('libpview.dll', input='libp3framework.dll')
51195120
if not PkgSkip("EGG"):
51205121
TargetAdd('libpview.dll', input='libpandaegg.dll')
51215122
TargetAdd('libpview.dll', input='libp3android.dll')
51225123
TargetAdd('libpview.dll', input=COMMON_PANDA_LIBS)
51235124
TargetAdd('libpview.dll', opts=['MODULE', 'ANDROID'])
51245125

5126+
if (not RTDIST and PkgSkip("PYTHON")==0):
5127+
OPTS += ['PYTHON']
5128+
TargetAdd('ppython_ppython.obj', opts=OPTS, input='python_main.cxx')
5129+
TargetAdd('libppython.dll', input='android_native_app_glue.obj')
5130+
TargetAdd('libppython.dll', input='android_main.obj')
5131+
TargetAdd('libppython.dll', input='ppython_ppython.obj')
5132+
TargetAdd('libppython.dll', input='libp3framework.dll')
5133+
TargetAdd('libppython.dll', input='libp3android.dll')
5134+
TargetAdd('libppython.dll', input=COMMON_PANDA_LIBS)
5135+
TargetAdd('libppython.dll', opts=['MODULE', 'ANDROID', 'PYTHON'])
5136+
51255137
#
51265138
# DIRECTORY: panda/src/androiddisplay/
51275139
#
@@ -7505,7 +7517,7 @@ def copy_library(source, base):
75057517
continue
75067518
if '.so.' in line:
75077519
dep = line.rpartition('.so.')[0] + '.so'
7508-
oscmd("patchelf --replace-needed %s %s %s" % (line, dep, target))
7520+
oscmd("patchelf --replace-needed %s %s %s" % (line, dep, target), True)
75097521
else:
75107522
dep = line
75117523

@@ -7516,6 +7528,7 @@ def copy_library(source, base):
75167528
copy_library(os.path.realpath(fulldep), dep)
75177529
break
75187530

7531+
# Now copy every lib in the lib dir, and its dependencies.
75197532
for base in os.listdir(source_dir):
75207533
if not base.startswith('lib'):
75217534
continue
@@ -7527,6 +7540,59 @@ def copy_library(source, base):
75277540
continue
75287541
copy_library(source, base)
75297542

7543+
# Same for Python extension modules. However, Android is strict about
7544+
# library naming, so we have a special naming scheme for these, in
7545+
# conjunction with a custom import hook to find these modules.
7546+
if not PkgSkip("PYTHON"):
7547+
suffix = GetExtensionSuffix()
7548+
source_dir = os.path.join(GetOutputDir(), "panda3d")
7549+
for base in os.listdir(source_dir):
7550+
if not base.endswith(suffix):
7551+
continue
7552+
modname = base[:-len(suffix)]
7553+
source = os.path.join(source_dir, base)
7554+
copy_library(source, "libpy.panda3d.{}.so".format(modname))
7555+
7556+
# Same for standard Python modules.
7557+
import _ctypes
7558+
source_dir = os.path.dirname(_ctypes.__file__)
7559+
for base in os.listdir(source_dir):
7560+
if not base.endswith('.so'):
7561+
continue
7562+
modname = base.partition('.')[0]
7563+
source = os.path.join(source_dir, base)
7564+
copy_library(source, "libpy.{}.so".format(modname))
7565+
7566+
def copy_python_tree(source_root, target_root):
7567+
for source_dir, dirs, files in os.walk(source_root):
7568+
if 'site-packages' in dirs:
7569+
dirs.remove('site-packages')
7570+
7571+
if not any(base.endswith('.py') for base in files):
7572+
continue
7573+
7574+
target_dir = os.path.join(target_root, os.path.relpath(source_dir, source_root))
7575+
target_dir = os.path.normpath(target_dir)
7576+
os.makedirs(target_dir, 0o755)
7577+
7578+
for base in files:
7579+
if base.endswith('.py'):
7580+
target = os.path.join(target_dir, base)
7581+
shutil.copy(os.path.join(source_dir, base), target)
7582+
7583+
# Copy the Python standard library to the .apk as well.
7584+
from distutils.sysconfig import get_python_lib
7585+
stdlib_source = get_python_lib(False, True)
7586+
stdlib_target = os.path.join("apkroot", "lib", "python{0}.{1}".format(*sys.version_info))
7587+
copy_python_tree(stdlib_source, stdlib_target)
7588+
7589+
# But also copy over our custom site.py.
7590+
shutil.copy("panda/src/android/site.py", os.path.join(stdlib_target, "site.py"))
7591+
7592+
# And now make a site-packages directory containing our direct/panda3d/pandac modules.
7593+
for tree in "panda3d", "direct", "pandac":
7594+
copy_python_tree(os.path.join(GetOutputDir(), tree), os.path.join(stdlib_target, "site-packages", tree))
7595+
75307596
# Copy the models and config files to the virtual assets filesystem.
75317597
oscmd("mkdir apkroot/assets")
75327598
oscmd("cp -R %s apkroot/assets/models" % (os.path.join(GetOutputDir(), "models")))
@@ -7545,7 +7611,11 @@ def copy_library(source, base):
75457611
oscmd(aapt_cmd)
75467612

75477613
# And add all the libraries to it.
7548-
oscmd("cd apkroot && aapt add ../%s classes.dex lib/%s/lib*.so" % (apk_unaligned, SDK["ANDROID_ABI"]))
7614+
oscmd("cd apkroot && aapt add ../%s classes.dex" % (apk_unaligned))
7615+
for path, dirs, files in os.walk('apkroot/lib'):
7616+
if files:
7617+
rel = os.path.relpath(path, 'apkroot')
7618+
oscmd("cd apkroot && aapt add ../%s %s/*" % (apk_unaligned, rel))
75497619

75507620
# Now align the .apk, which is necessary for Android to load it.
75517621
oscmd("zipalign -v -p 4 %s %s" % (apk_unaligned, apk_unsigned))
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* PANDA 3D SOFTWARE
3+
* Copyright (c) Carnegie Mellon University. All rights reserved.
4+
*
5+
* All use of this software is subject to the terms of the revised BSD
6+
* license. You should have received a copy of this license along
7+
* with this source code in a file named "LICENSE."
8+
*
9+
* @file PythonActivity.java
10+
* @author rdb
11+
* @date 2018-02-04
12+
*/
13+
14+
package org.panda3d.android;
15+
16+
import org.panda3d.android.PandaActivity;
17+
18+
/**
19+
* This is only declared as a separate class from PandaActivity so that we
20+
* can have two separate activity definitions in ApplicationManifest.xml.
21+
*/
22+
public class PythonActivity extends PandaActivity {
23+
}

panda/src/android/pview_manifest.xml

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,38 @@
4545
<intent-filter>
4646
<action android:name="android.intent.action.VIEW" />
4747
<category android:name="android.intent.category.DEFAULT" />
48-
<data android:mimeType="*/*" scheme="content" host="com.termux.files" />
49-
<data android:pathPattern=".*\\.egg" />
50-
<data android:pathPattern=".*\\.egg.pz" />
51-
<data android:pathPattern=".*\\.egg.gz" />
52-
<data android:pathPattern=".*\\.bam" />
53-
<data android:pathPattern=".*\\.bam.pz" />
54-
<data android:pathPattern=".*\\.bam.gz" />
48+
<data android:mimeType="*/*" android:scheme="content" android:host="com.termux.files" android:pathPattern=".*\\.egg" />
49+
</intent-filter>
50+
</activity>
51+
<activity android:name="org.panda3d.android.PythonActivity"
52+
android:label="Panda Python" android:theme="@android:style/Theme.NoTitleBar"
53+
android:configChanges="orientation|keyboardHidden"
54+
android:launchMode="singleInstance">
55+
56+
<meta-data android:name="android.app.lib_name"
57+
android:value="ppython" />
58+
<intent-filter>
59+
<action android:name="android.intent.action.MAIN" />
60+
<category android:name="android.intent.category.LAUNCHER" />
61+
</intent-filter>
62+
<intent-filter>
63+
<action android:name="android.intent.action.VIEW" />
64+
<category android:name="android.intent.category.DEFAULT" />
65+
<data android:mimeType="text/x-python" />
66+
</intent-filter>
67+
<intent-filter>
68+
<action android:name="android.intent.action.VIEW" />
69+
<category android:name="android.intent.category.DEFAULT" />
70+
<data android:mimeType="*/*" android:scheme="file" />
71+
<data android:pathPattern=".*\\.py" />
72+
<data android:pathPattern=".*\\.pyw" />
73+
</intent-filter>
74+
<intent-filter>
75+
<action android:name="android.intent.action.VIEW" />
76+
<category android:name="android.intent.category.DEFAULT" />
77+
<data android:mimeType="*/*" android:scheme="content" android:host="com.termux.files" />
78+
<data android:pathPattern=".*\\.py" />
79+
<data android:pathPattern=".*\\.pyw" />
5580
</intent-filter>
5681
</activity>
5782
</application>

panda/src/android/python_main.cxx

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/**
2+
* PANDA 3D SOFTWARE
3+
* Copyright (c) Carnegie Mellon University. All rights reserved.
4+
*
5+
* All use of this software is subject to the terms of the revised BSD
6+
* license. You should have received a copy of this license along
7+
* with this source code in a file named "LICENSE."
8+
*
9+
* @file python_main.cxx
10+
* @author rdb
11+
* @date 2018-02-12
12+
*/
13+
14+
#include "dtoolbase.h"
15+
#include "config_android.h"
16+
#include "executionEnvironment.h"
17+
18+
#undef _POSIX_C_SOURCE
19+
#undef _XOPEN_SOURCE
20+
#include <Python.h>
21+
#if PY_MAJOR_VERSION >= 3
22+
#include <wchar.h>
23+
#endif
24+
25+
#include <dlfcn.h>
26+
27+
/**
28+
* The main entry point for the Python activity. Called by android_main.
29+
*/
30+
int main(int argc, char *argv[]) {
31+
if (argc <= 1) {
32+
return 1;
33+
}
34+
35+
// Help out Python by telling it which encoding to use
36+
Py_FileSystemDefaultEncoding = "utf-8";
37+
38+
Py_SetProgramName(Py_DecodeLocale("ppython", nullptr));
39+
40+
// Set PYTHONHOME to the location of the .apk file.
41+
string apk_path = ExecutionEnvironment::get_binary_name();
42+
Py_SetPythonHome(Py_DecodeLocale(apk_path.c_str(), nullptr));
43+
44+
// We need to make zlib available to zipimport, but I don't know how
45+
// we could inject our import hook before Py_Initialize, so instead
46+
// load it as though it were a built-in module.
47+
void *zlib = dlopen("libpy.zlib.so", RTLD_NOW);
48+
if (zlib != nullptr) {
49+
void *init = dlsym(zlib, "PyInit_zlib");
50+
if (init != nullptr) {
51+
PyImport_AppendInittab("zlib", (PyObject *(*)())init);
52+
}
53+
}
54+
55+
Py_Initialize();
56+
57+
// This is used by the import hook to locate the module libraries.
58+
Filename dtool_name = ExecutionEnvironment::get_dtool_name();
59+
string native_dir = dtool_name.get_dirname();
60+
PyObject *py_native_dir = PyUnicode_FromStringAndSize(native_dir.c_str(), native_dir.size());
61+
PySys_SetObject("_native_library_dir", py_native_dir);
62+
Py_DECREF(py_native_dir);
63+
64+
int sts = 1;
65+
FILE *fp = fopen(argv[1], "r");
66+
if (fp != nullptr) {
67+
int res = PyRun_AnyFile(fp, argv[1]);
68+
if (res > 0) {
69+
sts = 0;
70+
} else {
71+
android_cat.error() << "Error running " << argv[1] << "\n";
72+
PyErr_Print();
73+
}
74+
} else {
75+
android_cat.error() << "Unable to open " << argv[1] << "\n";
76+
}
77+
78+
Py_Finalize();
79+
return sts;
80+
}

panda/src/android/run_pview.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# This script can be used for launching the Panda viewer from the Android
2+
# terminal environment, for example from within termux. It uses a socket
3+
# to pipe the command-line output back to the terminal.
4+
5+
port=12345
6+
7+
if [[ $# -eq 0 ]] ; then
8+
echo "Pass full path of model"
9+
exit 1
10+
fi
11+
12+
am start --activity-clear-task -n org.panda3d.sdk/org.panda3d.android.PandaActivity --user 0 --es org.panda3d.OUTPUT_URI tcp://127.0.0.1:$port --grant-read-uri-permission --grant-write-uri-permission file://$(realpath $1)
13+
14+
nc -l -p $port

panda/src/android/run_python.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# This script can be used for launching a Python script from the Android
2+
# terminal environment, for example from within termux. It uses a socket
3+
# to pipe the command-line output back to the terminal.
4+
5+
port=12345
6+
7+
if [[ $# -eq 0 ]] ; then
8+
echo "Pass full path of script"
9+
exit 1
10+
fi
11+
12+
am start --activity-clear-task -n org.panda3d.sdk/org.panda3d.android.PythonActivity --user 0 --es org.panda3d.OUTPUT_URI tcp://127.0.0.1:$port --grant-read-uri-permission --grant-write-uri-permission file://$(realpath $1)
13+
14+
nc -l -p $port

panda/src/android/site.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import sys
2+
import os
3+
4+
from importlib.abc import Loader, MetaPathFinder
5+
from importlib.machinery import ModuleSpec
6+
7+
if sys.version_info >= (3, 5):
8+
from importlib import _bootstrap_external
9+
else:
10+
from importlib import _bootstrap as _bootstrap_external
11+
12+
sys.platform = "android"
13+
14+
class AndroidExtensionFinder(MetaPathFinder):
15+
@classmethod
16+
def find_spec(cls, fullname, path=None, target=None):
17+
soname = 'libpy.' + fullname + '.so'
18+
path = os.path.join(sys._native_library_dir, soname)
19+
20+
if os.path.exists(path):
21+
loader = _bootstrap_external.ExtensionFileLoader(fullname, path)
22+
return ModuleSpec(fullname, loader, origin=path)
23+
24+
25+
def main():
26+
"""Adds the site-packages directory to the sys.path.
27+
Also, registers the import hook for extension modules."""
28+
29+
sys.path.append('{0}/lib/python{1}.{2}/site-packages'.format(sys.prefix, *sys.version_info))
30+
sys.meta_path.append(AndroidExtensionFinder)
31+
32+
33+
if not sys.flags.no_site:
34+
main()

0 commit comments

Comments
 (0)