Skip to content

Commit 1bb30be

Browse files
committed
fix PYTHONPATH hacks
1 parent 2f107b1 commit 1bb30be

5 files changed

Lines changed: 54 additions & 67 deletions

File tree

pythonforandroid/build.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -728,10 +728,6 @@ def process_python_modules(ctx, modules, arch):
728728
host_recipe = None
729729
try:
730730
host_recipe = Recipe.get_recipe("hostpython3", ctx)
731-
_python_path = host_recipe.get_path_to_python()
732-
libdir = glob.glob(join(_python_path, "build", "lib*"))
733-
env['PYTHONPATH'] = host_recipe.site_dir + ":" + join(
734-
_python_path, "Modules") + ":" + (libdir[0] if libdir else "")
735731
pip = host_recipe.pip
736732
except Exception:
737733
# hostpython3 is unavailable, so fall back to system pip

pythonforandroid/recipe.py

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1031,10 +1031,8 @@ def install_python_package(self, arch, name=None, env=None, is_dir=True):
10311031

10321032
def get_hostrecipe_env(self, arch=None):
10331033
env = environ.copy()
1034-
_python_path = self._host_recipe.get_path_to_python()
1035-
libdir = glob.glob(join(_python_path, "build", "lib*"))
1036-
env['PYTHONPATH'] = self._host_recipe.site_dir + ":" + join(
1037-
_python_path, "Modules") + ":" + (libdir[0] if libdir else "")
1034+
env['PYTHONPATH'] = ''
1035+
env['HOME'] = '/tmp'
10381036
return env
10391037

10401038
@property
@@ -1063,14 +1061,9 @@ def install_hostpython_prerequisites(self, packages=None, force_upgrade=True):
10631061
pip_options = [
10641062
"install",
10651063
*packages,
1066-
"--target", self._host_recipe.site_dir, "--python-version",
1067-
self.ctx.python_recipe.version,
1068-
# Don't use sources, instead wheels
1069-
"--only-binary=:all:",
10701064
]
10711065
if force_upgrade:
10721066
pip_options.append("--upgrade")
1073-
# Use system's pip
10741067
pip_env = self.get_hostrecipe_env()
10751068
shprint(self._host_recipe.pip, *pip_options, _env=pip_env)
10761069

@@ -1397,8 +1390,6 @@ def build_arch(self, arch):
13971390
# make build dir separately
13981391
sub_build_dir = join(build_dir, "p4a_android_build")
13991392
ensure_dir(sub_build_dir)
1400-
# copy hostpython to built python to ensure correct selection of libs and includes
1401-
shprint(sh.cp, self.real_hostpython_location, self.ctx.python_recipe.python_exe)
14021393

14031394
build_args = [
14041395
"-m",
@@ -1411,7 +1402,7 @@ def build_arch(self, arch):
14111402
built_wheels = []
14121403
with current_directory(build_dir):
14131404
shprint(
1414-
sh.Command(self.ctx.python_recipe.python_exe), *build_args, _env=env
1405+
sh.Command(self.real_hostpython_location), *build_args, _env=env
14151406
)
14161407
built_wheels = [realpath(whl) for whl in glob.glob("dist/*.whl")]
14171408
self.install_wheel(arch, built_wheels)
@@ -1523,8 +1514,6 @@ class RustCompiledComponentsRecipe(PyProjectRecipe):
15231514
"x86": "i686-linux-android",
15241515
}
15251516

1526-
call_hostpython_via_targetpython = False
1527-
15281517
def get_recipe_env(self, arch, **kwargs):
15291518
env = super().get_recipe_env(arch, **kwargs)
15301519

@@ -1563,7 +1552,7 @@ def get_recipe_env(self, arch, **kwargs):
15631552
env["PATH"] = ("{hostpython_dir}:{old_path}").format(
15641553
hostpython_dir=Recipe.get_recipe(
15651554
"hostpython3", self.ctx
1566-
).get_path_to_python(),
1555+
).local_bin,
15671556
old_path=env["PATH"],
15681557
)
15691558
return env

pythonforandroid/recipes/hostpython3/__init__.py

Lines changed: 48 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,13 @@
1515
)
1616
from pythonforandroid.prerequisites import OpenSSLPrerequisite
1717

18-
HOSTPYTHON_VERSION_UNSET_MESSAGE = (
19-
'The hostpython recipe must have set version'
20-
)
18+
HOSTPYTHON_VERSION_UNSET_MESSAGE = "The hostpython recipe must have set version"
2119

22-
SETUP_DIST_NOT_FIND_MESSAGE = (
23-
'Could not find Setup.dist or Setup in Python build'
24-
)
20+
SETUP_DIST_NOT_FIND_MESSAGE = "Could not find Setup.dist or Setup in Python build"
2521

2622

2723
class HostPython3Recipe(Recipe):
28-
'''
24+
"""
2925
The hostpython3's recipe.
3026
3127
.. versionchanged:: 2019.10.06.post0
@@ -34,17 +30,17 @@ class HostPython3Recipe(Recipe):
3430
.. versionchanged:: 0.6.0
3531
Refactored into the new class
3632
:class:`~pythonforandroid.python.HostPythonRecipe`
37-
'''
33+
"""
3834

39-
version = '3.14.2'
35+
version = "3.14.2"
4036

41-
url = 'https://github.com/python/cpython/archive/refs/tags/v{version}.tar.gz'
42-
'''The default url to download our host python recipe. This url will
43-
change depending on the python version set in attribute :attr:`version`.'''
37+
url = "https://github.com/python/cpython/archive/refs/tags/v{version}.tar.gz"
38+
"""The default url to download our host python recipe. This url will
39+
change depending on the python version set in attribute :attr:`version`."""
4440

45-
build_subdir = 'native-build'
46-
'''Specify the sub build directory for the hostpython3 recipe. Defaults
47-
to ``native-build``.'''
41+
build_subdir = "native-build"
42+
"""Specify the sub build directory for the hostpython3 recipe. Defaults
43+
to ``native-build``."""
4844

4945
patches = ["fix_ensurepip.patch"]
5046

@@ -59,17 +55,17 @@ def download(self):
5955

6056
@property
6157
def _exe_name(self):
62-
'''
58+
"""
6359
Returns the name of the python executable depending on the version.
64-
'''
60+
"""
6561
if not self.version:
6662
raise BuildInterruptingException(HOSTPYTHON_VERSION_UNSET_MESSAGE)
67-
return f'python{self.version.split(".")[0]}'
63+
return "python"
6864

6965
@property
7066
def python_exe(self):
71-
'''Returns the full path of the hostpython executable.'''
72-
return join(self.get_path_to_python(), self._exe_name)
67+
"""Returns the full path of the hostpython executable."""
68+
return join(self.local_bin, self._exe_name)
7369

7470
def get_recipe_env(self, arch=None):
7571
env = os.environ.copy()
@@ -91,14 +87,14 @@ def should_build(self, arch):
9187

9288
def get_build_container_dir(self, arch=None):
9389
choices = self.check_recipe_choices()
94-
dir_name = '-'.join([self.name] + choices)
95-
return join(self.ctx.build_dir, 'other_builds', dir_name, 'desktop')
90+
dir_name = "-".join([self.name] + choices)
91+
return join(self.ctx.build_dir, "other_builds", dir_name, "desktop")
9692

9793
def get_build_dir(self, arch=None):
98-
'''
94+
"""
9995
.. note:: Unlike other recipes, the hostpython build dir doesn't
10096
depend on the target arch
101-
'''
97+
"""
10298
return join(self.get_build_container_dir(), self.name)
10399

104100
def get_path_to_python(self):
@@ -112,16 +108,20 @@ def site_root(self):
112108
def site_bin(self):
113109
return join(self.site_root, self.site_dir, "bin")
114110

111+
@property
112+
def local_dir(self):
113+
return join(self.site_root, "usr/local/")
114+
115115
@property
116116
def local_bin(self):
117-
return join(self.site_root, "usr/local/bin/")
117+
return join(self.local_dir, "bin")
118118

119119
@property
120120
def site_dir(self):
121121
p_version = Version(self.version)
122122
return join(
123123
self.site_root,
124-
f"usr/local/lib/python{p_version.major}.{p_version.minor}/site-packages/"
124+
f"usr/local/lib/python{p_version.major}.{p_version.minor}/site-packages/",
125125
)
126126

127127
@property
@@ -163,34 +163,40 @@ def build_arch(self, arch):
163163
# Configure the build
164164
build_configured = False
165165
with current_directory(build_dir):
166-
if not Path('config.status').exists():
167-
shprint(sh.Command(join(recipe_build_dir, 'configure')), _env=env)
166+
if not Path("config.status").exists():
167+
shprint(
168+
sh.Command(join(recipe_build_dir, "configure")),
169+
"--prefix",
170+
self.local_dir,
171+
_env=env,
172+
)
168173
build_configured = True
169174

170175
with current_directory(recipe_build_dir):
171176
# Create the Setup file. This copying from Setup.dist is
172177
# the normal and expected procedure before Python 3.8, but
173178
# after this the file with default options is already named "Setup"
174-
setup_dist_location = join('Modules', 'Setup.dist')
179+
setup_dist_location = join("Modules", "Setup.dist")
175180
if Path(setup_dist_location).exists():
176-
shprint(sh.cp, setup_dist_location,
177-
join(build_dir, 'Modules', 'Setup'))
181+
shprint(sh.cp, setup_dist_location, join(build_dir, "Modules", "Setup"))
178182
else:
179183
# Check the expected file does exist
180-
setup_location = join('Modules', 'Setup')
184+
setup_location = join("Modules", "Setup")
181185
if not Path(setup_location).exists():
182-
raise BuildInterruptingException(
183-
SETUP_DIST_NOT_FIND_MESSAGE
184-
)
186+
raise BuildInterruptingException(SETUP_DIST_NOT_FIND_MESSAGE)
185187

186-
shprint(sh.make, '-j', str(cpu_count()), '-C', build_dir, _env=env)
188+
shprint(sh.make, "-j", str(cpu_count()), "-C", build_dir, _env=env)
187189

190+
with current_directory(build_dir):
191+
shprint(sh.make, "install", _env=env)
192+
193+
with current_directory(recipe_build_dir):
188194
# make a copy of the python executable giving it the name we want,
189195
# because we got different python's executable names depending on
190196
# the fs being case-insensitive (Mac OS X, Cygwin...) or
191197
# case-sensitive (linux)...so this way we will have an unique name
192198
# for our hostpython, regarding the used fs
193-
for exe_name in ['python.exe', 'python']:
199+
for exe_name in ["python.exe", "python"]:
194200
exe = join(self.get_path_to_python(), exe_name)
195201
if Path(exe).is_file():
196202
shprint(sh.cp, exe, self.python_exe)
@@ -200,10 +206,12 @@ def build_arch(self, arch):
200206
self.ctx.hostpython = self.python_exe
201207

202208
if build_configured:
203-
204209
shprint(
205-
sh.Command(self.python_exe), "-m", "ensurepip", "--root", self.site_root, "-U",
206-
_env={"HOME": "/tmp", "PATH": self.local_bin}
210+
sh.Command(self.python_exe),
211+
"-m",
212+
"ensurepip",
213+
"-U",
214+
_env={"HOME": "/tmp", "PATH": self.local_bin},
207215
)
208216
self.fix_pip_shebangs()
209217

pythonforandroid/recipes/python3/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -360,9 +360,9 @@ def build_arch(self, arch):
360360
*(' '.join(self.configure_args).format(
361361
android_host=env['HOSTARCH'],
362362
android_build=android_build,
363-
python_host_bin=join(self.get_recipe(
363+
python_host_bin=self.get_recipe(
364364
'host' + self.name, self.ctx
365-
).get_path_to_python(), "python3"),
365+
).python_exe,
366366
prefix=sys_prefix,
367367
exec_prefix=sys_exec_prefix)).split(' '),
368368
_env=env)

tests/recipes/test_hostpython3.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,6 @@ def test_property__exe_name_no_version(self):
2929
def test_property__exe_name(self):
3030
self.assertEqual(self.recipe._exe_name, 'python3')
3131

32-
def test_property_python_exe(self):
33-
self.assertEqual(
34-
self.recipe.python_exe,
35-
join(self.recipe.get_path_to_python(), 'python3')
36-
)
37-
3832
@mock.patch("pythonforandroid.recipes.hostpython3.Path.exists")
3933
def test_should_build(self, mock_exists):
4034
# test case for existing python exe which shouldn't trigger the build

0 commit comments

Comments
 (0)