rdb 94ceace5af 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]
2018-03-11 22:53:08 +01:00

35 lines
1003 B
Python

import sys
import os
from importlib.abc import Loader, MetaPathFinder
from importlib.machinery import ModuleSpec
if sys.version_info >= (3, 5):
from importlib import _bootstrap_external
else:
from importlib import _bootstrap as _bootstrap_external
sys.platform = "android"
class AndroidExtensionFinder(MetaPathFinder):
@classmethod
def find_spec(cls, fullname, path=None, target=None):
soname = 'libpy.' + fullname + '.so'
path = os.path.join(sys._native_library_dir, soname)
if os.path.exists(path):
loader = _bootstrap_external.ExtensionFileLoader(fullname, path)
return ModuleSpec(fullname, loader, origin=path)
def main():
"""Adds the site-packages directory to the sys.path.
Also, registers the import hook for extension modules."""
sys.path.append('{0}/lib/python{1}.{2}/site-packages'.format(sys.prefix, *sys.version_info))
sys.meta_path.append(AndroidExtensionFinder)
if not sys.flags.no_site:
main()