8000 Add ability of compile our python installation files into (.pyc/.pyo)… · inclement/python-for-android@b601a89 · GitHub
[go: up one dir, main page]

Skip to content

Commit b601a89

Browse files
committed
Add ability of compile our python installation files into (.pyc/.pyo) depending on our python version
Because this way the initialization of our apk it will be faster. We must use the .pyc extension for our python 3 because as of Python 3.5, the .pyo filename extension is no longer used and has been removed. Notice that the command to compile the files for python 3 is slightly different (`-b` option added). This is done that way because otherwise our compiled files would be put in `__pycache__` folder and this option makes that the compiled files gets putted in the same directory than the compiled file. See also:   - `PEP 488 -- Elimination of PYO files` (https://www.python.org/dev/peps/pep-0488/)   - `PEP 3147 -- PYC Repository Directories` as to why a separate directory is used (https://www.python.org/dev/peps/pep-3147/)
1 parent d6a927c commit b601a89

File tree

2 files changed

+44
-6
lines changed

2 files changed

+44
-6
lines changed

pythonforandroid/python.py

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from os.path import dirname, exists, join
77
from shutil import copy2
88
from os import environ
9+
import subprocess
910
import glob
1011
import sh
1112

@@ -71,7 +72,7 @@ class GuestPythonRecipe(TargetPythonRecipe):
7172
'''The directories that we want to omit for our python bundle'''
7273

7374
stdlib_filen_blacklist = [
74-
'*.pyc',
75+
'*.py',
7576
'*.exe',
7677
'*.whl',
7778
]
@@ -84,13 +85,23 @@ class GuestPythonRecipe(TargetPythonRecipe):
8485
'''The directories from site packages dir that we don't want to be included
8586
in our python bundle.'''
8687

87-
site_packages_filen_blacklist = []
88+
site_packages_filen_blacklist = [
89+
'*.py'
90+
]
8891
'''The file extensions from site packages dir that we don't want to be
8992
included in our python bundle.'''
9093

9194
opt_depends = ['sqlite3', 'libffi', 'openssl']
9295
'''The optional libraries which we would like to get our python linked'''
9396

97+
compiled_extension = '.pyc'
98+
'''the default extension for compiled python files.
99+
100+
.. note:: the default extension for compiled python files has been .pyo for
101+
python 2.x-3.4 but as of Python 3.5, the .pyo filename extension is no
102+
longer used and has been removed in favour of extension .pyc
103+
'''
104+
94105
def __init__(self, *args, **kwargs):
95106
self._ctx = None
96107
super(GuestPythonRecipe, self).__init__(*args, **kwargs)
@@ -253,15 +264,28 @@ def include_root(self, arch_name):
253264
def link_root(self, arch_name):
254265
return join(self.get_build_dir(arch_name), 'android-build')
255266

267+
def compile_python_files(self, dir):
268+
'''
269+
Compile the python files (recursively) for the python files inside
270+
a given folder.
271+
272+
.. note:: python2 compiles the files into extension .pyo, but in
273+
python3, and as of Python 3.5, the .pyo filename extension is no
274+
longer used...uses .pyc (https://www.python.org/dev/peps/pep-0488)
275+
'''
276+
args = [self.ctx.hostpython]
277+
if self.ctx.python_recipe.name == 'python3':
278+
args += ['-OO', '-m', 'compileall', '-b', '-f', dir]
279+
else:
280+
args += ['-OO', '-m', 'compileall', '-f', dir]
281+
subprocess.call(args)
282+
256283
def create_python_bundle(self, dirn, arch):
257284
"""
258285
Create a packaged python bundle in the target directory, by
259286
copying all the modules and standard library to the right
260287
place.
261288
"""
262-
# Bundle compiled python modules to a folder
263-
modules_dir = join(dirn, 'modules')
264-
ensure_dir(modules_dir)
265289
# Todo: find a better way to find the build libs folder
266290
modules_build_dir = join(
267291
self.get_build_dir(arch.arch),
@@ -272,8 +296,20 @@ def create_python_bundle(self, dirn, arch):
272296
arch.command_prefix.split('-')[0],
273297
self.major_minor_version_string
274298
))
299+
300+
# Compile to *.pyc/*.pyo the python modules
301+
self.compile_python_files(modules_build_dir)
302+
# Compile to *.pyc/*.pyo the standard python library
303+
self.compile_python_files(join(self.get_build_dir(arch.arch), 'Lib'))
304+
# Compile to *.pyc/*.pyo the other python packages (site-packages)
305+
self.compile_python_files(self.ctx.get_python_install_dir())
306+
307+
# Bundle compiled python modules to a folder
308+
modules_dir = join(dirn, 'modules')
309+
c_ext = self.compiled_extension
310+
ensure_dir(modules_dir)
275311
module_filens = (glob.glob(join(modules_build_dir, '*.so')) +
276-
glob.glob(join(modules_build_dir, '*.py')))
312+
glob.glob(join(modules_build_dir, '*' + c_ext)))
277313
info("Copy {} files into the bundle".format(len(module_filens)))
278314
for filen in module_filens:
279315
info(" - copy {}".format(filen))

pythonforandroid/recipes/python2/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ class Python2Recipe(GuestPythonRecipe):
4747
'--prefix={prefix}',
4848
'--exec-prefix={exec_prefix}')
4949

50+
compiled_extension = '.pyo'
51+
5052
def prebuild_arch(self, arch):
5153
super(Python2Recipe, self).prebuild_arch(arch)
5254
patch_mark = join(self.get_build_dir(arch.arch), '.openssl-patched')

0 commit comments

Comments
 (0)
0