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

kivy / python-for-android / 22020238684

14 Feb 2026 03:59PM UTC coverage: 63.451% (-0.4%) from 63.887%
22020238684

Pull #3280

github

web-flow
Merge 59a62c3db into 1fc026943
Pull Request #3280: Add support for prebuilt wheels

1827 of 3145 branches covered (58.09%)

Branch coverage included in aggregate %.

52 of 124 new or added lines in 5 files covered. (41.94%)

2 existing lines in 1 file now uncovered.

5315 of 8111 relevant lines covered (65.53%)

5.23 hits per line

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

83.24
/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, error
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 = (
8 ✔
19
    'The hostpython recipe must have set version'
20
)
21

22
SETUP_DIST_NOT_FIND_MESSAGE = (
8 ✔
23
    'Could not find Setup.dist or Setup in Python build'
24
)
25

26

27
class HostPython3Recipe(Recipe):
8 ✔
28
    '''
29
    The hostpython3's recipe.
30

31
    .. versionchanged:: 2019.10.06.post0
32
        Refactored from deleted class ``python.HostPythonRecipe`` into here.
33

34
    .. versionchanged:: 0.6.0
35
        Refactored into  the new class
36
        :class:`~pythonforandroid.python.HostPythonRecipe`
37
    '''
38

39
    version = '3.14.2'
8 ✔
40

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

45
    build_subdir = 'native-build'
8 ✔
46
    '''Specify the sub build directory for the hostpython3 recipe. Defaults
6 ✔
47
    to ``native-build``.'''
48

49
    patches = ["fix_ensurepip.patch"]
8 ✔
50

51
    # apply version guard
52
    def download(self):
8 ✔
NEW
53
        python_recipe = Recipe.get_recipe("python3", self.ctx)
×
NEW
54
        if python_recipe.version != self.version:
×
NEW
55
            error(
×
56
                f"python3 should have same version as hostpython3, {python_recipe.version} != {self.version}"
57
            )
NEW
58
            exit(1)
×
NEW
59
        super().download()
×
60

61
    @property
8 ✔
62
    def _exe_name(self):
8 ✔
63
        '''
64
        Returns the name of the python executable depending on the version.
65
        '''
66
        if not self.version:
8 ✔
67
            raise BuildInterruptingException(HOSTPYTHON_VERSION_UNSET_MESSAGE)
8 ✔
68
        return f'python{self.version.split(".")[0]}'
8 ✔
69

70
    @property
8 ✔
71
    def python_exe(self):
8 ✔
72
        '''Returns the full path of the hostpython executable.'''
73
        return join(self.get_path_to_python(), self._exe_name)
8 ✔
74

75
    def get_recipe_env(self, arch=None):
8 ✔
76
        env = os.environ.copy()
8 ✔
77
        openssl_prereq = OpenSSLPrerequisite()
8 ✔
78
        if env.get("PKG_CONFIG_PATH", ""):
8 !
79
            env["PKG_CONFIG_PATH"] = os.pathsep.join(
8 ✔
80
                [openssl_prereq.pkg_config_location, env["PKG_CONFIG_PATH"]]
81
            )
82
        else:
83
            env["PKG_CONFIG_PATH"] = openssl_prereq.pkg_config_location
×
84
        return env
8 ✔
85

86
    def should_build(self, arch):
8 ✔
87
        if Path(self.python_exe).exists():
8 ✔
88
            # no need to build, but we must set hostpython for our Context
89
            self.ctx.hostpython = self.python_exe
8 ✔
90
            return False
8 ✔
91
        return True
8 ✔
92

93
    def get_build_container_dir(self, arch=None):
8 ✔
94
        choices = self.check_recipe_choices()
8 ✔
95
        dir_name = '-'.join([self.name] + choices)
8 ✔
96
        return join(self.ctx.build_dir, 'other_builds', dir_name, 'desktop')
8 ✔
97

98
    def get_build_dir(self, arch=None):
8 ✔
99
        '''
100
        .. note:: Unlike other recipes, the hostpython build dir doesn't
101
            depend on the target arch
102
        '''
103
        return join(self.get_build_container_dir(), self.name)
8 ✔
104

105
    def get_path_to_python(self):
8 ✔
106
        return join(self.get_build_dir(), self.build_subdir)
8 ✔
107

108
    @property
8 ✔
109
    def site_root(self):
8 ✔
110
        return join(self.get_path_to_python(), "root")
8 ✔
111

112
    @property
8 ✔
113
    def site_bin(self):
8 ✔
114
        return join(self.site_root, self.site_dir, "bin")
8 ✔
115

116
    @property
8 ✔
117
    def local_bin(self):
8 ✔
118
        return join(self.site_root, "usr/local/bin/")
8 ✔
119

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

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

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

136
    def fix_pip_shebangs(self):
8 ✔
137

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

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

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

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

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

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

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

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

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

164
        # Configure the build
165
        build_configured = False
8 ✔
166
        with current_directory(build_dir):
8 ✔
167
            if not Path('config.status').exists():
8 ✔
168
                shprint(sh.Command(join(recipe_build_dir, 'configure')), _env=env)
8 ✔
169
                build_configured = True
8 ✔
170

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

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

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

200
        ensure_dir(self.site_root)
8 ✔
201
        self.ctx.hostpython = self.python_exe
8 ✔
202

203
        if build_configured:
8 ✔
204

205
            shprint(
8 ✔
206
                sh.Command(self.python_exe), "-m", "ensurepip", "--root", self.site_root, "-U",
207
                _env={"HOME": "/tmp", "PATH": self.local_bin}
208
            )
209
            self.fix_pip_shebangs()
8 ✔
210

211

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

© 2026 Coveralls, Inc