From d5b62570008e2107eb6448f0483c2dd4c8d7aa49 Mon Sep 17 00:00:00 2001 From: SomberNight Date: Thu, 26 Sep 2024 18:35:58 +0000 Subject: [PATCH] reproducible apks: strip file path prefix from .pyc files to make builds not depend on the project dir path --- pythonforandroid/bootstraps/common/build/build.py | 9 ++++++++- pythonforandroid/recipes/python3/__init__.py | 9 ++++++++- tests/recipes/test_python3.py | 2 +- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/pythonforandroid/bootstraps/common/build/build.py b/pythonforandroid/bootstraps/common/build/build.py index 51e0a5fa94..07230bee96 100644 --- a/pythonforandroid/bootstraps/common/build/build.py +++ b/pythonforandroid/bootstraps/common/build/build.py @@ -205,7 +205,14 @@ def compile_py_file(python_file, optimize_python=True): if PYTHON is None: return - args = [PYTHON, '-m', 'compileall', '-b', '-f', python_file] + path_prefix = os.path.commonpath([python_file, os.getcwd()]) + args = [ + PYTHON, '-m', 'compileall', + '-b', + '-s', path_prefix, # for reproducible builds, do not leak paths into pyc + '-f', + python_file, + ] if optimize_python: # -OO = strip docstrings args.insert(1, '-OO') diff --git a/pythonforandroid/recipes/python3/__init__.py b/pythonforandroid/recipes/python3/__init__.py index 2334db6add..0d969f752e 100644 --- a/pythonforandroid/recipes/python3/__init__.py +++ b/pythonforandroid/recipes/python3/__init__.py @@ -360,7 +360,14 @@ def compile_python_files(self, dir): longer used...uses .pyc (https://www.python.org/dev/peps/pep-0488) ''' args = [self.ctx.hostpython] - args += ['-OO', '-m', 'compileall', '-b', '-f', dir] + args += [ + '-OO', + '-m', 'compileall', + '-b', + '-s', dir, # for reproducible builds, do not leak paths into pyc + '-f', + dir, + ] subprocess.call(args) def create_python_bundle(self, dirn, arch): diff --git a/tests/recipes/test_python3.py b/tests/recipes/test_python3.py index f1d652b6c1..85dd3849c1 100644 --- a/tests/recipes/test_python3.py +++ b/tests/recipes/test_python3.py @@ -56,7 +56,7 @@ def test_compile_python_files(self, mock_subprocess): hostpy = self.recipe.ctx.hostpython = '/fake/hostpython3' self.recipe.compile_python_files(fake_compile_dir) mock_subprocess.assert_called_once_with( - [hostpy, '-OO', '-m', 'compileall', '-b', '-f', fake_compile_dir], + [hostpy, '-OO', '-m', 'compileall', '-b', '-s', fake_compile_dir, '-f', fake_compile_dir], ) @mock.patch("pythonforandroid.recipe.Recipe.check_recipe_choices")