Skip to content

Commit

Permalink
reproducible apks: strip file path prefix from .pyc files
Browse files Browse the repository at this point in the history
to make builds not depend on the project dir path
  • Loading branch information
SomberNight committed Sep 26, 2024
1 parent 08713b3 commit d5b6257
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
9 changes: 8 additions & 1 deletion pythonforandroid/bootstraps/common/build/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
9 changes: 8 additions & 1 deletion pythonforandroid/recipes/python3/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion tests/recipes/test_python3.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit d5b6257

Please sign in to comment.