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

kivy / python-for-android / 20393459065

20 Dec 2025 11:05AM UTC coverage: 63.293%. First build
20393459065

Pull #3271

github

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

1789 of 3092 branches covered (57.86%)

Branch coverage included in aggregate %.

21 of 93 new or added lines in 4 files covered. (22.58%)

5227 of 7993 relevant lines covered (65.39%)

5.22 hits per line

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

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

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

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

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

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

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

93
    def get_path_to_python(self):
8✔
94
        return join(self.get_build_dir(), self.build_subdir)
8✔
95

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

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

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

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

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

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

124
    def fix_pip_shebangs(self):
8✔
125

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

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

NEW
133
            pip_path = os.path.join(self.local_bin, filename)
×
134

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

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

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

143
    def build_arch(self, arch):
8✔
144
        env = self.get_recipe_env(arch)
8✔
145

146
        recipe_build_dir = self.get_build_dir(arch.arch)
8✔
147

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

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

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

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

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

188
        ensure_dir(self.site_root)
8✔
189
        self.ctx.hostpython = self.python_exe
8✔
190

191
        if build_configured:
8✔
192

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

199

200
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