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

kivy / python-for-android / 20394619237

20 Dec 2025 12:49PM UTC coverage: 63.296%. First build
20394619237

Pull #3271

github

web-flow
Merge 8ec52ce32 into 6494ac165
Pull Request #3271: `toolchain`: auto resolve deps

1789 of 3092 branches covered (57.86%)

Branch coverage included in aggregate %.

22 of 94 new or added lines in 4 files covered. (23.4%)

5228 of 7994 relevant lines covered (65.4%)

5.22 hits per line

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

86.44
/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 = (
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
    @property
8✔
52
    def _exe_name(self):
8✔
53
        '''
54
        Returns the name of the python executable depending on the version.
55
        '''
56
        if not self.version:
8✔
57
            raise BuildInterruptingException(HOSTPYTHON_VERSION_UNSET_MESSAGE)
8✔
58
        return f'python{self.version.split(".")[0]}'
8✔
59

60
    @property
8✔
61
    def python_exe(self):
8✔
62
        '''Returns the full path of the hostpython executable.'''
63
        return join(self.get_path_to_python(), self._exe_name)
8✔
64

65
    def get_recipe_env(self, arch=None):
8✔
66
        env = os.environ.copy()
8✔
67
        openssl_prereq = OpenSSLPrerequisite()
8✔
68
        if env.get("PKG_CONFIG_PATH", ""):
8!
69
            env["PKG_CONFIG_PATH"] = os.pathsep.join(
8✔
70
                [openssl_prereq.pkg_config_location, env["PKG_CONFIG_PATH"]]
71
            )
72
        else:
73
            env["PKG_CONFIG_PATH"] = openssl_prereq.pkg_config_location
×
74
        return env
8✔
75

76
    def should_build(self, arch):
8✔
77
        if Path(self.python_exe).exists():
8✔
78
            # no need to build, but we must set hostpython for our Context
79
            self.ctx.hostpython = self.python_exe
8✔
80
            return False
8✔
81
        return True
8✔
82

83
    def get_build_container_dir(self, arch=None):
8✔
84
        choices = self.check_recipe_choices()
8✔
85
        dir_name = '-'.join([self.name] + choices)
8✔
86
        return join(self.ctx.build_dir, 'other_builds', dir_name, 'desktop')
8✔
87

88
    def get_build_dir(self, arch=None):
8✔
89
        '''
90
        .. note:: Unlike other recipes, the hostpython build dir doesn't
91
            depend on the target arch
92
        '''
93
        return join(self.get_build_container_dir(), self.name)
8✔
94

95
    def get_path_to_python(self):
8✔
96
        return join(self.get_build_dir(), self.build_subdir)
8✔
97

98
    @property
8✔
99
    def site_root(self):
8✔
100
        return join(self.get_path_to_python(), "root")
8✔
101

102
    @property
8✔
103
    def site_bin(self):
8✔
104
        return join(self.site_root, self.site_dir, "bin")
8✔
105

106
    @property
8✔
107
    def local_bin(self):
8✔
108
        return join(self.site_root, "usr/local/bin/")
8✔
109

110
    @property
8✔
111
    def site_dir(self):
8✔
112
        p_version = Version(self.version)
8✔
113
        return join(
8✔
114
            self.site_root,
115
            f"usr/local/lib/python{p_version.major}.{p_version.minor}/site-packages/"
116
        )
117

118
    @property
8✔
119
    def _pip(self):
8✔
NEW
120
        return join(self.local_bin, "pip3")
×
121

122
    @property
8✔
123
    def pip(self):
8✔
NEW
124
        return sh.Command(self._pip)
×
125

126
    def fix_pip_shebangs(self):
8✔
127

128
        if not os.path.exists(self.local_bin):
8!
129
            return
8✔
130

NEW
131
        for filename in os.listdir(self.local_bin):
×
NEW
132
            if not filename.startswith("pip"):
×
NEW
133
                continue
×
134

NEW
135
            pip_path = os.path.join(self.local_bin, filename)
×
136

NEW
137
            with open(pip_path, "rb") as file:
×
NEW
138
                file_lines = file.read().splitlines()
×
139

NEW
140
            file_lines[0] = f"#!{self.python_exe}".encode()
×
141

NEW
142
            with open(pip_path, "wb") as file:
×
NEW
143
                file.write(b"\n".join(file_lines) + b"\n")
×
144

145
    def build_arch(self, arch):
8✔
146
        env = self.get_recipe_env(arch)
8✔
147

148
        recipe_build_dir = self.get_build_dir(arch.arch)
8✔
149

150
        # Create a subdirectory to actually perform the build
151
        build_dir = join(recipe_build_dir, self.build_subdir)
8✔
152
        ensure_dir(build_dir)
8✔
153

154
        # Configure the build
155
        build_configured = False
8✔
156
        with current_directory(build_dir):
8✔
157
            if not Path('config.status').exists():
8✔
158
                shprint(sh.Command(join(recipe_build_dir, 'configure')), _env=env)
8✔
159
                build_configured = True
8✔
160

161
        with current_directory(recipe_build_dir):
8✔
162
            # Create the Setup file. This copying from Setup.dist is
163
            # the normal and expected procedure before Python 3.8, but
164
            # after this the file with default options is already named "Setup"
165
            setup_dist_location = join('Modules', 'Setup.dist')
8✔
166
            if Path(setup_dist_location).exists():
8✔
167
                shprint(sh.cp, setup_dist_location,
8✔
168
                        join(build_dir, 'Modules', 'Setup'))
169
            else:
170
                # Check the expected file does exist
171
                setup_location = join('Modules', 'Setup')
8✔
172
                if not Path(setup_location).exists():
8✔
173
                    raise BuildInterruptingException(
8✔
174
                        SETUP_DIST_NOT_FIND_MESSAGE
175
                    )
176

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

179
            # make a copy of the python executable giving it the name we want,
180
            # because we got different python's executable names depending on
181
            # the fs being case-insensitive (Mac OS X, Cygwin...) or
182
            # case-sensitive (linux)...so this way we will have an unique name
183
            # for our hostpython, regarding the used fs
184
            for exe_name in ['python.exe', 'python']:
8!
185
                exe = join(self.get_path_to_python(), exe_name)
8✔
186
                if Path(exe).is_file():
8!
187
                    shprint(sh.cp, exe, self.python_exe)
8✔
188
                    break
8✔
189

190
        ensure_dir(self.site_root)
8✔
191
        self.ctx.hostpython = self.python_exe
8✔
192

193
        if build_configured:
8✔
194

195
            shprint(
8✔
196
                sh.Command(self.python_exe), "-m", "ensurepip", "--root", self.site_root, "-U",
197
                _env={"HOME": "/tmp", "PATH": self.local_bin}
198
            )
199
            self.fix_pip_shebangs()
8✔
200

201

202
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

© 2025 Coveralls, Inc