• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

kivy / python-for-android / 25435676503

06 May 2026 12:37PM UTC coverage: 62.876% (-1.0%) from 63.887%
25435676503

Pull #3278

github

web-flow
Merge 3c07c5446 into 3ab2fd669
Pull Request #3278: Handling system bars and Edge-to-Edge enforcement (android 15+)

1832 of 3180 branches covered (57.61%)

Branch coverage included in aggregate %.

5329 of 8209 relevant lines covered (64.92%)

5.18 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

84.92
/pythonforandroid/recipes/hostpython3/__init__.py
1
import sh
8✔
2
import os
8✔
3

4
from multiprocessing import cpu_count
8✔
5
from pathlib import Path
8✔
6
from os.path import join
8✔
7

8
from packaging.version import Version
8✔
9
from pythonforandroid.logger import shprint
8✔
10
from pythonforandroid.recipe import Recipe
8✔
11
from pythonforandroid.util import (
8✔
12
    BuildInterruptingException,
13
    current_directory,
14
    ensure_dir,
15
)
16
from pythonforandroid.prerequisites import OpenSSLPrerequisite
8✔
17

18
HOSTPYTHON_VERSION_UNSET_MESSAGE = "The hostpython recipe must have set version"
8✔
19

20
SETUP_DIST_NOT_FIND_MESSAGE = "Could not find Setup.dist or Setup in Python build"
8✔
21

22

23
class HostPython3Recipe(Recipe):
8✔
24
    """
25
    The hostpython3's recipe.
26

27
    .. versionchanged:: 2019.10.06.post0
28
        Refactored from deleted class ``python.HostPythonRecipe`` into here.
29

30
    .. versionchanged:: 0.6.0
31
        Refactored into  the new class
32
        :class:`~pythonforandroid.python.HostPythonRecipe`
33
    """
34

35
    version = "3.14.2"
8✔
36

37
    url = "https://github.com/python/cpython/archive/refs/tags/v{version}.tar.gz"
8✔
38
    """The default url to download our host python recipe. This url will
6✔
39
    change depending on the python version set in attribute :attr:`version`."""
40

41
    build_subdir = "native-build"
8✔
42
    """Specify the sub build directory for the hostpython3 recipe. Defaults
6✔
43
    to ``native-build``."""
44

45
    patches = ["fix_ensurepip.patch"]
8✔
46

47
    # apply version guard
48
    def download(self):
8✔
49
        python_recipe = Recipe.get_recipe("python3", self.ctx)
×
50
        if python_recipe.version != self.version:
×
51
            raise BuildInterruptingException(
×
52
                f"python3 should have same version as hostpython3, {python_recipe.version} != {self.version}"
53
            )
54
        super().download()
×
55

56
    @property
8✔
57
    def _exe_name(self):
8✔
58
        """
59
        Returns the name of the python executable depending on the version.
60
        """
61
        if not self.version:
8✔
62
            raise BuildInterruptingException(HOSTPYTHON_VERSION_UNSET_MESSAGE)
8✔
63
        return "python"
8✔
64

65
    @property
8✔
66
    def python_exe(self):
8✔
67
        """Returns the full path of the hostpython executable."""
68
        return join(self.local_bin, self._exe_name)
8✔
69

70
    def get_recipe_env(self, arch=None):
8✔
71
        env = os.environ.copy()
8✔
72
        openssl_prereq = OpenSSLPrerequisite()
8✔
73
        if env.get("PKG_CONFIG_PATH", ""):
8!
74
            env["PKG_CONFIG_PATH"] = os.pathsep.join(
8✔
75
                [openssl_prereq.pkg_config_location, env["PKG_CONFIG_PATH"]]
76
            )
77
        else:
78
            env["PKG_CONFIG_PATH"] = openssl_prereq.pkg_config_location
×
79
        return env
8✔
80

81
    def should_build(self, arch):
8✔
82
        if Path(self.python_exe).exists():
8✔
83
            # no need to build, but we must set hostpython for our Context
84
            self.ctx.hostpython = self.python_exe
8✔
85
            return False
8✔
86
        return True
8✔
87

88
    def get_build_container_dir(self, arch=None):
8✔
89
        choices = self.check_recipe_choices()
8✔
90
        dir_name = "-".join([self.name] + choices)
8✔
91
        return join(self.ctx.build_dir, "other_builds", dir_name, "desktop")
8✔
92

93
    def get_build_dir(self, arch=None):
8✔
94
        """
95
        .. note:: Unlike other recipes, the hostpython build dir doesn't
96
            depend on the target arch
97
        """
98
        return join(self.get_build_container_dir(), self.name)
8✔
99

100
    def get_path_to_python(self):
8✔
101
        return join(self.get_build_dir(), self.build_subdir)
8✔
102

103
    @property
8✔
104
    def site_root(self):
8✔
105
        return join(self.get_path_to_python(), "root")
8✔
106

107
    @property
8✔
108
    def site_bin(self):
8✔
109
        return join(self.site_root, self.site_dir, "bin")
8✔
110

111
    @property
8✔
112
    def local_dir(self):
8✔
113
        return join(self.site_root, "usr/local/")
8✔
114

115
    @property
8✔
116
    def local_bin(self):
8✔
117
        return join(self.local_dir, "bin")
8✔
118

119
    @property
8✔
120
    def site_dir(self):
8✔
121
        p_version = Version(self.version)
8✔
122
        return join(
8✔
123
            self.site_root,
124
            f"usr/local/lib/python{p_version.major}.{p_version.minor}/site-packages/",
125
        )
126

127
    @property
8✔
128
    def _pip(self):
8✔
129
        return join(self.local_bin, "pip3")
×
130

131
    @property
8✔
132
    def pip(self):
8✔
133
        return sh.Command(self._pip)
×
134

135
    def fix_pip_shebangs(self):
8✔
136

137
        if not os.path.exists(self.local_bin):
8!
138
            return
8✔
139

140
        for filename in os.listdir(self.local_bin):
×
141
            if not filename.startswith("pip"):
×
142
                continue
×
143

144
            pip_path = os.path.join(self.local_bin, filename)
×
145

146
            with open(pip_path, "rb") as file:
×
147
                file_lines = file.read().splitlines()
×
148

149
            file_lines[0] = f"#!{self.python_exe}".encode()
×
150

151
            with open(pip_path, "wb") as file:
×
152
                file.write(b"\n".join(file_lines) + b"\n")
×
153

154
    def build_arch(self, arch):
8✔
155
        env = self.get_recipe_env(arch)
8✔
156

157
        recipe_build_dir = self.get_build_dir(arch.arch)
8✔
158

159
        # Create a subdirectory to actually perform the build
160
        build_dir = join(recipe_build_dir, self.build_subdir)
8✔
161
        ensure_dir(build_dir)
8✔
162

163
        # Configure the build
164
        build_configured = False
8✔
165
        with current_directory(build_dir):
8✔
166
            if not Path("config.status").exists():
8✔
167
                shprint(
8✔
168
                    sh.Command(join(recipe_build_dir, "configure")),
169
                    "--prefix",
170
                    self.local_dir,
171
                    _env=env,
172
                )
173
                build_configured = True
8✔
174

175
        with current_directory(recipe_build_dir):
8✔
176
            # Create the Setup file. This copying from Setup.dist is
177
            # the normal and expected procedure before Python 3.8, but
178
            # after this the file with default options is already named "Setup"
179
            setup_dist_location = join("Modules", "Setup.dist")
8✔
180
            if Path(setup_dist_location).exists():
8✔
181
                shprint(sh.cp, setup_dist_location, join(build_dir, "Modules", "Setup"))
8✔
182
            else:
183
                # Check the expected file does exist
184
                setup_location = join("Modules", "Setup")
8✔
185
                if not Path(setup_location).exists():
8✔
186
                    raise BuildInterruptingException(SETUP_DIST_NOT_FIND_MESSAGE)
8✔
187

188
            shprint(sh.make, "-j", str(cpu_count()), "-C", build_dir, _env=env)
8✔
189

190
        with current_directory(build_dir):
8✔
191
            shprint(sh.make, "install", _env=env)
8✔
192

193
        with current_directory(recipe_build_dir):
8✔
194
            # make a copy of the python executable giving it the name we want,
195
            # because we got different python's executable names depending on
196
            # the fs being case-insensitive (Mac OS X, Cygwin...) or
197
            # case-sensitive (linux)...so this way we will have an unique name
198
            # for our hostpython, regarding the used fs
199
            for exe_name in ["python.exe", "python"]:
8!
200
                exe = join(self.get_path_to_python(), exe_name)
8✔
201
                if Path(exe).is_file():
8!
202
                    shprint(sh.cp, exe, self.python_exe)
8✔
203
                    break
8✔
204

205
        ensure_dir(self.site_root)
8✔
206
        self.ctx.hostpython = self.python_exe
8✔
207

208
        if build_configured:
8✔
209
            shprint(
8✔
210
                sh.Command(self.python_exe),
211
                "-m",
212
                "ensurepip",
213
                "-U",
214
                _env={"HOME": "/tmp", "PATH": self.local_bin},
215
            )
216
            self.fix_pip_shebangs()
8✔
217

218

219
recipe = HostPython3Recipe()
8✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc