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

kivy / python-for-android / 10636977072

30 Aug 2024 05:47PM UTC coverage: 59.125% (+0.03%) from 59.092%
10636977072

push

github

web-flow
Merge pull request #3054 from AndreMiras/feature/minor_refactoring

:recycle: Minor code cleaning

1046 of 2358 branches covered (44.36%)

Branch coverage included in aggregate %.

13 of 14 new or added lines in 3 files covered. (92.86%)

3 existing lines in 2 files now uncovered.

4876 of 7658 relevant lines covered (63.67%)

2.54 hits per line

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

45.64
/pythonforandroid/toolchain.py
1
#!/usr/bin/env python
2
"""
4✔
3
Tool for packaging Python apps for Android
4
==========================================
5

6
This module defines the entry point for command line and programmatic use.
7
"""
8

9
from appdirs import user_data_dir
4✔
10
import argparse
4✔
11
from functools import wraps
4✔
12
import glob
4✔
13
import logging
4✔
14
import os
4✔
15
from os import environ
4✔
16
from os.path import (join, dirname, realpath, exists, expanduser, basename)
4✔
17
import re
4✔
18
import shlex
4✔
19
import sys
4✔
20
from sys import platform
4✔
21

22
# This must be imported and run before other third-party or p4a
23
# packages.
24
from pythonforandroid.checkdependencies import check
4✔
25
check()
4✔
26

27
from packaging.version import Version
4✔
28
import sh
4✔
29

30
from pythonforandroid import __version__
4✔
31
from pythonforandroid.bootstrap import Bootstrap
4✔
32
from pythonforandroid.build import Context, build_recipes, project_has_setup_py
4✔
33
from pythonforandroid.distribution import Distribution, pretty_log_dists
4✔
34
from pythonforandroid.entrypoints import main
4✔
35
from pythonforandroid.graph import get_recipe_order_and_bootstrap
4✔
36
from pythonforandroid.logger import (logger, info, warning, setup_color,
4✔
37
                                     Out_Style, Out_Fore,
38
                                     info_notify, info_main, shprint)
39
from pythonforandroid.pythonpackage import get_dep_names_of_package
4✔
40
from pythonforandroid.recipe import Recipe
4✔
41
from pythonforandroid.recommendations import (
4✔
42
    RECOMMENDED_NDK_API, RECOMMENDED_TARGET_API, print_recommendations)
43
from pythonforandroid.util import (
4✔
44
    current_directory,
45
    BuildInterruptingException,
46
    load_source,
47
    rmdir,
48
    max_build_tool_version,
49
)
50

51
user_dir = dirname(realpath(os.path.curdir))
4✔
52
toolchain_dir = dirname(__file__)
4✔
53
sys.path.insert(0, join(toolchain_dir, "tools", "external"))
4✔
54

55

56
def add_boolean_option(parser, names, no_names=None,
4✔
57
                       default=True, dest=None, description=None):
58
    group = parser.add_argument_group(description=description)
4✔
59
    if not isinstance(names, (list, tuple)):
4!
60
        names = [names]
×
61
    if dest is None:
4!
62
        dest = names[0].strip("-").replace("-", "_")
4✔
63

64
    def add_dashes(x):
4✔
65
        return x if x.startswith("-") else "--"+x
4✔
66

67
    opts = [add_dashes(x) for x in names]
4✔
68
    group.add_argument(
4✔
69
        *opts, help=("(this is the default)" if default else None),
70
        dest=dest, action='store_true')
71
    if no_names is None:
4!
72
        def add_no(x):
4✔
73
            x = x.lstrip("-")
4✔
74
            return ("no_"+x) if "_" in x else ("no-"+x)
4✔
75
        no_names = [add_no(x) for x in names]
4✔
76
    opts = [add_dashes(x) for x in no_names]
4✔
77
    group.add_argument(
4✔
78
        *opts, help=(None if default else "(this is the default)"),
79
        dest=dest, action='store_false')
80
    parser.set_defaults(**{dest: default})
4✔
81

82

83
def require_prebuilt_dist(func):
4✔
84
    """Decorator for ToolchainCL methods. If present, the method will
85
    automatically make sure a dist has been built before continuing
86
    or, if no dists are present or can be obtained, will raise an
87
    error.
88
    """
89

90
    @wraps(func)
4✔
91
    def wrapper_func(self, args, **kw):
4✔
92
        ctx = self.ctx
4✔
93
        ctx.set_archs(self._archs)
4✔
94
        ctx.prepare_build_environment(user_sdk_dir=self.sdk_dir,
4✔
95
                                      user_ndk_dir=self.ndk_dir,
96
                                      user_android_api=self.android_api,
97
                                      user_ndk_api=self.ndk_api)
98
        dist = self._dist
4✔
99
        if dist.needs_build:
4!
100
            if dist.folder_exists():  # possible if the dist is being replaced
4!
101
                dist.delete()
×
102
            info_notify('No dist exists that meets your requirements, '
4✔
103
                        'so one will be built.')
104
            build_dist_from_args(ctx, dist, args)
4✔
105
        func(self, args, **kw)
4✔
106
    return wrapper_func
4✔
107

108

109
def dist_from_args(ctx, args):
4✔
110
    """Parses out any distribution-related arguments, and uses them to
111
    obtain a Distribution class instance for the build.
112
    """
113
    return Distribution.get_distribution(
4✔
114
        ctx,
115
        name=args.dist_name,
116
        recipes=split_argument_list(args.requirements),
117
        archs=args.arch,
118
        ndk_api=args.ndk_api,
119
        force_build=args.force_build,
120
        require_perfect_match=args.require_perfect_match,
121
        allow_replace_dist=args.allow_replace_dist)
122

123

124
def build_dist_from_args(ctx, dist, args):
4✔
125
    """Parses out any bootstrap related arguments, and uses them to build
126
    a dist."""
127
    bs = Bootstrap.get_bootstrap(args.bootstrap, ctx)
4✔
128
    blacklist = getattr(args, "blacklist_requirements", "").split(",")
4✔
129
    if len(blacklist) == 1 and blacklist[0] == "":
4!
130
        blacklist = []
4✔
131
    build_order, python_modules, bs = (
4✔
132
        get_recipe_order_and_bootstrap(
133
            ctx, dist.recipes, bs,
134
            blacklist=blacklist
135
        ))
136
    assert set(build_order).intersection(set(python_modules)) == set()
4✔
137
    ctx.recipe_build_order = build_order
4✔
138
    ctx.python_modules = python_modules
4✔
139

140
    info('The selected bootstrap is {}'.format(bs.name))
4✔
141
    info_main('# Creating dist with {} bootstrap'.format(bs.name))
4✔
142
    bs.distribution = dist
4✔
143
    info_notify('Dist will have name {} and requirements ({})'.format(
4✔
144
        dist.name, ', '.join(dist.recipes)))
145
    info('Dist contains the following requirements as recipes: {}'.format(
4✔
146
        ctx.recipe_build_order))
147
    info('Dist will also contain modules ({}) installed from pip'.format(
4✔
148
        ', '.join(ctx.python_modules)))
149
    info(
4✔
150
        'Dist will be build in mode {build_mode}{with_debug_symbols}'.format(
151
            build_mode='debug' if ctx.build_as_debuggable else 'release',
152
            with_debug_symbols=' (with debug symbols)'
153
            if ctx.with_debug_symbols
154
            else '',
155
        )
156
    )
157

158
    ctx.distribution = dist
4✔
159
    ctx.prepare_bootstrap(bs)
4✔
160
    if dist.needs_build:
4!
161
        ctx.prepare_dist()
4✔
162

163
    build_recipes(build_order, python_modules, ctx,
4✔
164
                  getattr(args, "private", None),
165
                  ignore_project_setup_py=getattr(
166
                      args, "ignore_setup_py", False
167
                  ),
168
                 )
169

170
    ctx.bootstrap.assemble_distribution()
4✔
171

172
    info_main('# Your distribution was created successfully, exiting.')
4✔
173
    info('Dist can be found at (for now) {}'
4✔
174
         .format(join(ctx.dist_dir, ctx.distribution.dist_dir)))
175

176

177
def split_argument_list(arg_list):
4✔
178
    if not len(arg_list):
4✔
179
        return []
4✔
180
    return re.split(r'[ ,]+', arg_list)
4✔
181

182

183
class NoAbbrevParser(argparse.ArgumentParser):
4✔
184
    """We want to disable argument abbreviation so as not to interfere
185
    with passing through arguments to build.py, but in python2 argparse
186
    doesn't have this option.
187

188
    This subclass alternative is follows the suggestion at
189
    https://bugs.python.org/issue14910.
190
    """
191
    def _get_option_tuples(self, option_string):
4✔
192
        return []
4✔
193

194

195
class ToolchainCL:
4✔
196

197
    def __init__(self):
4✔
198

199
        argv = sys.argv
4✔
200
        self.warn_on_carriage_return_args(argv)
4✔
201
        # Buildozer used to pass these arguments in a now-invalid order
202
        # If that happens, apply this fix
203
        # This fix will be removed once a fixed buildozer is released
204
        if (len(argv) > 2
4!
205
                and argv[1].startswith('--color')
206
                and argv[2].startswith('--storage-dir')):
207
            argv.append(argv.pop(1))  # the --color arg
×
208
            argv.append(argv.pop(1))  # the --storage-dir arg
×
209

210
        parser = NoAbbrevParser(
4✔
211
            description='A packaging tool for turning Python scripts and apps '
212
                        'into Android APKs')
213

214
        generic_parser = argparse.ArgumentParser(
4✔
215
            add_help=False,
216
            description='Generic arguments applied to all commands')
217
        argparse.ArgumentParser(
4✔
218
            add_help=False, description='Arguments for dist building')
219

220
        generic_parser.add_argument(
4✔
221
            '--debug', dest='debug', action='store_true', default=False,
222
            help='Display debug output and all build info')
223
        generic_parser.add_argument(
4✔
224
            '--color', dest='color', choices=['always', 'never', 'auto'],
225
            help='Enable or disable color output (default enabled on tty)')
226
        generic_parser.add_argument(
4✔
227
            '--sdk-dir', '--sdk_dir', dest='sdk_dir', default='',
228
            help='The filepath where the Android SDK is installed')
229
        generic_parser.add_argument(
4✔
230
            '--ndk-dir', '--ndk_dir', dest='ndk_dir', default='',
231
            help='The filepath where the Android NDK is installed')
232
        generic_parser.add_argument(
4✔
233
            '--android-api',
234
            '--android_api',
235
            dest='android_api',
236
            default=0,
237
            type=int,
238
            help=('The Android API level to build against defaults to {} if '
239
                  'not specified.').format(RECOMMENDED_TARGET_API))
240
        generic_parser.add_argument(
4✔
241
            '--ndk-version', '--ndk_version', dest='ndk_version', default=None,
242
            help=('DEPRECATED: the NDK version is now found automatically or '
243
                  'not at all.'))
244
        generic_parser.add_argument(
4✔
245
            '--ndk-api', type=int, default=None,
246
            help=('The Android API level to compile against. This should be your '
247
                  '*minimal supported* API, not normally the same as your --android-api. '
248
                  'Defaults to min(ANDROID_API, {}) if not specified.').format(RECOMMENDED_NDK_API))
249
        generic_parser.add_argument(
4✔
250
            '--symlink-bootstrap-files', '--ssymlink_bootstrap_files',
251
            action='store_true',
252
            dest='symlink_bootstrap_files',
253
            default=False,
254
            help=('If True, symlinks the bootstrap files '
255
                  'creation. This is useful for development only, it could also'
256
                  ' cause weird problems.'))
257

258
        default_storage_dir = user_data_dir('python-for-android')
4✔
259
        if ' ' in default_storage_dir:
4!
260
            default_storage_dir = '~/.python-for-android'
×
261
        generic_parser.add_argument(
4✔
262
            '--storage-dir', dest='storage_dir', default=default_storage_dir,
263
            help=('Primary storage directory for downloads and builds '
264
                  '(default: {})'.format(default_storage_dir)))
265

266
        generic_parser.add_argument(
4✔
267
            '--arch', help='The archs to build for.',
268
            action='append', default=[])
269

270
        # Options for specifying the Distribution
271
        generic_parser.add_argument(
4✔
272
            '--dist-name', '--dist_name',
273
            help='The name of the distribution to use or create', default='')
274

275
        generic_parser.add_argument(
4✔
276
            '--requirements',
277
            help=('Dependencies of your app, should be recipe names or '
278
                  'Python modules. NOT NECESSARY if you are using '
279
                  'Python 3 with --use-setup-py'),
280
            default='')
281

282
        generic_parser.add_argument(
4✔
283
            '--recipe-blacklist',
284
            help=('Blacklist an internal recipe from use. Allows '
285
                  'disabling Python 3 core modules to save size'),
286
            dest="recipe_blacklist",
287
            default='')
288

289
        generic_parser.add_argument(
4✔
290
            '--blacklist-requirements',
291
            help=('Blacklist an internal recipe from use. Allows '
292
                  'disabling Python 3 core modules to save size'),
293
            dest="blacklist_requirements",
294
            default='')
295

296
        generic_parser.add_argument(
4✔
297
            '--bootstrap',
298
            help='The bootstrap to build with. Leave unset to choose '
299
                 'automatically.',
300
            default=None)
301

302
        generic_parser.add_argument(
4✔
303
            '--hook',
304
            help='Filename to a module that contains python-for-android hooks',
305
            default=None)
306

307
        add_boolean_option(
4✔
308
            generic_parser, ["force-build"],
309
            default=False,
310
            description='Whether to force compilation of a new distribution')
311

312
        add_boolean_option(
4✔
313
            generic_parser, ["require-perfect-match"],
314
            default=False,
315
            description=('Whether the dist recipes must perfectly match '
316
                         'those requested'))
317

318
        add_boolean_option(
4✔
319
            generic_parser, ["allow-replace-dist"],
320
            default=True,
321
            description='Whether existing dist names can be automatically replaced'
322
            )
323

324
        generic_parser.add_argument(
4✔
325
            '--local-recipes', '--local_recipes',
326
            dest='local_recipes', default='./p4a-recipes',
327
            help='Directory to look for local recipes')
328

329
        generic_parser.add_argument(
4✔
330
            '--activity-class-name',
331
            dest='activity_class_name', default='org.kivy.android.PythonActivity',
332
            help='The full java class name of the main activity')
333

334
        generic_parser.add_argument(
4✔
335
            '--service-class-name',
336
            dest='service_class_name', default='org.kivy.android.PythonService',
337
            help='Full java package name of the PythonService class')
338

339
        generic_parser.add_argument(
4✔
340
            '--java-build-tool',
341
            dest='java_build_tool', default='auto',
342
            choices=['auto', 'ant', 'gradle'],
343
            help=('The java build tool to use when packaging the APK, defaults '
344
                  'to automatically selecting an appropriate tool.'))
345

346
        add_boolean_option(
4✔
347
            generic_parser, ['copy-libs'],
348
            default=False,
349
            description='Copy libraries instead of using biglink (Android 4.3+)'
350
        )
351

352
        self._read_configuration()
4✔
353

354
        subparsers = parser.add_subparsers(dest='subparser_name',
4✔
355
                                           help='The command to run')
356

357
        def add_parser(subparsers, *args, **kwargs):
4✔
358
            """
359
            argparse in python2 doesn't support the aliases option,
360
            so we just don't provide the aliases there.
361
            """
362
            if 'aliases' in kwargs and sys.version_info.major < 3:
4!
363
                kwargs.pop('aliases')
×
364
            return subparsers.add_parser(*args, **kwargs)
4✔
365

366
        add_parser(
4✔
367
            subparsers,
368
            'recommendations',
369
            parents=[generic_parser],
370
            help='List recommended p4a dependencies')
371
        parser_recipes = add_parser(
4✔
372
            subparsers,
373
            'recipes',
374
            parents=[generic_parser],
375
            help='List the available recipes')
376
        parser_recipes.add_argument(
4✔
377
            "--compact",
378
            action="store_true", default=False,
379
            help="Produce a compact list suitable for scripting")
380
        add_parser(
4✔
381
            subparsers, 'bootstraps',
382
            help='List the available bootstraps',
383
            parents=[generic_parser])
384
        add_parser(
4✔
385
            subparsers, 'clean_all',
386
            aliases=['clean-all'],
387
            help='Delete all builds, dists and caches',
388
            parents=[generic_parser])
389
        add_parser(
4✔
390
            subparsers, 'clean_dists',
391
            aliases=['clean-dists'],
392
            help='Delete all dists',
393
            parents=[generic_parser])
394
        add_parser(
4✔
395
            subparsers, 'clean_bootstrap_builds',
396
            aliases=['clean-bootstrap-builds'],
397
            help='Delete all bootstrap builds',
398
            parents=[generic_parser])
399
        add_parser(
4✔
400
            subparsers, 'clean_builds',
401
            aliases=['clean-builds'],
402
            help='Delete all builds',
403
            parents=[generic_parser])
404

405
        parser_clean = add_parser(
4✔
406
            subparsers, 'clean',
407
            help='Delete build components.',
408
            parents=[generic_parser])
409
        parser_clean.add_argument(
4✔
410
            'component', nargs='+',
411
            help=('The build component(s) to delete. You can pass any '
412
                  'number of arguments from "all", "builds", "dists", '
413
                  '"distributions", "bootstrap_builds", "downloads".'))
414

415
        parser_clean_recipe_build = add_parser(
4✔
416
            subparsers,
417
            'clean_recipe_build', aliases=['clean-recipe-build'],
418
            help=('Delete the build components of the given recipe. '
419
                  'By default this will also delete built dists'),
420
            parents=[generic_parser])
421
        parser_clean_recipe_build.add_argument(
4✔
422
            'recipe', help='The recipe name')
423
        parser_clean_recipe_build.add_argument(
4✔
424
            '--no-clean-dists', default=False,
425
            dest='no_clean_dists',
426
            action='store_true',
427
            help='If passed, do not delete existing dists')
428

429
        parser_clean_download_cache = add_parser(
4✔
430
            subparsers,
431
            'clean_download_cache', aliases=['clean-download-cache'],
432
            help='Delete cached downloads for requirement builds',
433
            parents=[generic_parser])
434
        parser_clean_download_cache.add_argument(
4✔
435
            'recipes',
436
            nargs='*',
437
            help='The recipes to clean (space-separated). If no recipe name is'
438
                  ' provided, the entire cache is cleared.')
439

440
        parser_export_dist = add_parser(
4✔
441
            subparsers,
442
            'export_dist', aliases=['export-dist'],
443
            help='Copy the named dist to the given path',
444
            parents=[generic_parser])
445
        parser_export_dist.add_argument('output_dir',
4✔
446
                                        help='The output dir to copy to')
447
        parser_export_dist.add_argument(
4✔
448
            '--symlink',
449
            action='store_true',
450
            help='Symlink the dist instead of copying')
451

452
        parser_packaging = argparse.ArgumentParser(
4✔
453
            parents=[generic_parser],
454
            add_help=False,
455
            description='common options for packaging (apk, aar)')
456

457
        # This is actually an internal argument of the build.py
458
        # (see pythonforandroid/bootstraps/common/build/build.py).
459
        # However, it is also needed before the distribution is finally
460
        # assembled for locating the setup.py / other build systems, which
461
        # is why we also add it here:
462
        parser_packaging.add_argument(
4✔
463
            '--add-asset', dest='assets',
464
            action="append", default=[],
465
            help='Put this in the assets folder in the apk.')
466
        parser_packaging.add_argument(
4✔
467
            '--add-resource', dest='resources',
468
            action="append", default=[],
469
            help='Put this in the res folder in the apk.')
470
        parser_packaging.add_argument(
4✔
471
            '--private', dest='private',
472
            help='the directory with the app source code files' +
473
                 ' (containing your main.py entrypoint)',
474
            required=False, default=None)
475
        parser_packaging.add_argument(
4✔
476
            '--use-setup-py', dest="use_setup_py",
477
            action='store_true', default=False,
478
            help="Process the setup.py of a project if present. " +
479
                 "(Experimental!")
480
        parser_packaging.add_argument(
4✔
481
            '--ignore-setup-py', dest="ignore_setup_py",
482
            action='store_true', default=False,
483
            help="Don't run the setup.py of a project if present. " +
484
                 "This may be required if the setup.py is not " +
485
                 "designed to work inside p4a (e.g. by installing " +
486
                 "dependencies that won't work or aren't desired " +
487
                 "on Android")
488
        parser_packaging.add_argument(
4✔
489
            '--release', dest='build_mode', action='store_const',
490
            const='release', default='debug',
491
            help='Build your app as a non-debug release build. '
492
                 '(Disables gdb debugging among other things)')
493
        parser_packaging.add_argument(
4✔
494
            '--with-debug-symbols', dest='with_debug_symbols',
495
            action='store_const', const=True, default=False,
496
            help='Will keep debug symbols from `.so` files.')
497
        parser_packaging.add_argument(
4✔
498
            '--keystore', dest='keystore', action='store', default=None,
499
            help=('Keystore for JAR signing key, will use jarsigner '
500
                  'default if not specified (release build only)'))
501
        parser_packaging.add_argument(
4✔
502
            '--signkey', dest='signkey', action='store', default=None,
503
            help='Key alias to sign PARSER_APK. with (release build only)')
504
        parser_packaging.add_argument(
4✔
505
            '--keystorepw', dest='keystorepw', action='store', default=None,
506
            help='Password for keystore')
507
        parser_packaging.add_argument(
4✔
508
            '--signkeypw', dest='signkeypw', action='store', default=None,
509
            help='Password for key alias')
510

511
        add_parser(
4✔
512
            subparsers,
513
            'aar', help='Build an AAR',
514
            parents=[parser_packaging])
515

516
        add_parser(
4✔
517
            subparsers,
518
            'apk', help='Build an APK',
519
            parents=[parser_packaging])
520

521
        add_parser(
4✔
522
            subparsers,
523
            'aab', help='Build an AAB',
524
            parents=[parser_packaging])
525

526
        add_parser(
4✔
527
            subparsers,
528
            'create', help='Compile a set of requirements into a dist',
529
            parents=[generic_parser])
530
        add_parser(
4✔
531
            subparsers,
532
            'archs', help='List the available target architectures',
533
            parents=[generic_parser])
534
        add_parser(
4✔
535
            subparsers,
536
            'distributions', aliases=['dists'],
537
            help='List the currently available (compiled) dists',
538
            parents=[generic_parser])
539
        add_parser(
4✔
540
            subparsers,
541
            'delete_dist', aliases=['delete-dist'], help='Delete a compiled dist',
542
            parents=[generic_parser])
543

544
        parser_sdk_tools = add_parser(
4✔
545
            subparsers,
546
            'sdk_tools', aliases=['sdk-tools'],
547
            help='Run the given binary from the SDK tools dis',
548
            parents=[generic_parser])
549
        parser_sdk_tools.add_argument(
4✔
550
            'tool', help='The binary tool name to run')
551

552
        add_parser(
4✔
553
            subparsers,
554
            'adb', help='Run adb from the given SDK',
555
            parents=[generic_parser])
556
        add_parser(
4✔
557
            subparsers,
558
            'logcat', help='Run logcat from the given SDK',
559
            parents=[generic_parser])
560
        add_parser(
4✔
561
            subparsers,
562
            'build_status', aliases=['build-status'],
563
            help='Print some debug information about current built components',
564
            parents=[generic_parser])
565

566
        parser.add_argument('-v', '--version', action='version',
4✔
567
                            version=__version__)
568

569
        args, unknown = parser.parse_known_args(sys.argv[1:])
4✔
570
        args.unknown_args = unknown
4✔
571

572
        if getattr(args, "private", None) is not None:
4!
573
            # Pass this value on to the internal bootstrap build.py:
574
            args.unknown_args += ["--private", args.private]
×
575
        if getattr(args, "build_mode", None) == "release":
4!
576
            args.unknown_args += ["--release"]
×
577
        if getattr(args, "with_debug_symbols", False):
4!
578
            args.unknown_args += ["--with-debug-symbols"]
×
579
        if getattr(args, "ignore_setup_py", False):
4!
580
            args.use_setup_py = False
×
581
        if getattr(args, "activity_class_name", "org.kivy.android.PythonActivity") != 'org.kivy.android.PythonActivity':
4✔
582
            args.unknown_args += ["--activity-class-name", args.activity_class_name]
4✔
583
        if getattr(args, "service_class_name", "org.kivy.android.PythonService") != 'org.kivy.android.PythonService':
4✔
584
            args.unknown_args += ["--service-class-name", args.service_class_name]
4✔
585

586
        self.args = args
4✔
587

588
        if args.subparser_name is None:
4✔
589
            parser.print_help()
4✔
590
            exit(1)
4✔
591

592
        setup_color(args.color)
4✔
593

594
        if args.debug:
4!
595
            logger.setLevel(logging.DEBUG)
×
596

597
        self.ctx = Context()
4✔
598
        self.ctx.use_setup_py = getattr(args, "use_setup_py", True)
4✔
599
        self.ctx.build_as_debuggable = getattr(
4✔
600
            args, "build_mode", "debug"
601
        ) == "debug"
602
        self.ctx.with_debug_symbols = getattr(
4✔
603
            args, "with_debug_symbols", False
604
        )
605

606
        # Process requirements and put version in environ
607
        if hasattr(args, 'requirements'):
4!
608
            requirements = []
4✔
609

610
            # Add dependencies from setup.py, but only if they are recipes
611
            # (because otherwise, setup.py itself will install them later)
612
            if (project_has_setup_py(getattr(args, "private", None)) and
4!
613
                    getattr(args, "use_setup_py", False)):
614
                try:
×
615
                    info("Analyzing package dependencies. MAY TAKE A WHILE.")
×
616
                    # Get all the dependencies corresponding to a recipe:
617
                    dependencies = [
×
618
                        dep.lower() for dep in
619
                        get_dep_names_of_package(
620
                            args.private,
621
                            keep_version_pins=True,
622
                            recursive=True,
623
                            verbose=True,
624
                        )
625
                    ]
626
                    info("Dependencies obtained: " + str(dependencies))
×
627
                    all_recipes = [
×
628
                        recipe.lower() for recipe in
629
                        set(Recipe.list_recipes(self.ctx))
630
                    ]
631
                    dependencies = set(dependencies).intersection(
×
632
                        set(all_recipes)
633
                    )
634
                    # Add dependencies to argument list:
635
                    if len(dependencies) > 0:
×
636
                        if len(args.requirements) > 0:
×
637
                            args.requirements += u","
×
638
                        args.requirements += u",".join(dependencies)
×
639
                except ValueError:
×
640
                    # Not a python package, apparently.
641
                    warning(
×
642
                        "Processing failed, is this project a valid "
643
                        "package? Will continue WITHOUT setup.py deps."
644
                    )
645

646
            # Parse --requirements argument list:
647
            for requirement in split_argument_list(args.requirements):
4✔
648
                if "==" in requirement:
4!
649
                    requirement, version = requirement.split(u"==", 1)
×
650
                    os.environ["VERSION_{}".format(requirement)] = version
×
651
                    info('Recipe {}: version "{}" requested'.format(
×
652
                        requirement, version))
653
                requirements.append(requirement)
4✔
654
            args.requirements = u",".join(requirements)
4✔
655

656
        self.warn_on_deprecated_args(args)
4✔
657

658
        self.storage_dir = args.storage_dir
4✔
659
        self.ctx.setup_dirs(self.storage_dir)
4✔
660
        self.sdk_dir = args.sdk_dir
4✔
661
        self.ndk_dir = args.ndk_dir
4✔
662
        self.android_api = args.android_api
4✔
663
        self.ndk_api = args.ndk_api
4✔
664
        self.ctx.symlink_bootstrap_files = args.symlink_bootstrap_files
4✔
665
        self.ctx.java_build_tool = args.java_build_tool
4✔
666

667
        self._archs = args.arch
4✔
668

669
        self.ctx.local_recipes = realpath(args.local_recipes)
4✔
670
        self.ctx.copy_libs = args.copy_libs
4✔
671

672
        self.ctx.activity_class_name = args.activity_class_name
4✔
673
        self.ctx.service_class_name = args.service_class_name
4✔
674

675
        # Each subparser corresponds to a method
676
        command = args.subparser_name.replace('-', '_')
4✔
677
        getattr(self, command)(args)
4✔
678

679
    @staticmethod
4✔
680
    def warn_on_carriage_return_args(args):
4✔
681
        for check_arg in args:
4✔
682
            if '\r' in check_arg:
4!
683
                warning("Argument '{}' contains a carriage return (\\r).".format(str(check_arg.replace('\r', ''))))
×
684
                warning("Invoking this program via scripts which use CRLF instead of LF line endings will have undefined behaviour.")
×
685

686
    def warn_on_deprecated_args(self, args):
4✔
687
        """
688
        Print warning messages for any deprecated arguments that were passed.
689
        """
690

691
        # Output warning if setup.py is present and neither --ignore-setup-py
692
        # nor --use-setup-py was specified.
693
        if project_has_setup_py(getattr(args, "private", None)):
4!
UNCOV
694
            if not getattr(args, "use_setup_py", False) and \
×
695
                    not getattr(args, "ignore_setup_py", False):
696
                warning("  **** FUTURE BEHAVIOR CHANGE WARNING ****")
×
697
                warning("Your project appears to contain a setup.py file.")
×
698
                warning("Currently, these are ignored by default.")
×
699
                warning("This will CHANGE in an upcoming version!")
×
700
                warning("")
×
701
                warning("To ensure your setup.py is ignored, please specify:")
×
702
                warning("    --ignore-setup-py")
×
703
                warning("")
×
704
                warning("To enable what will some day be the default, specify:")
×
705
                warning("    --use-setup-py")
×
706

707
        # NDK version is now determined automatically
708
        if args.ndk_version is not None:
4!
709
            warning('--ndk-version is deprecated and no longer necessary, '
×
710
                    'the value you passed is ignored')
711
        if 'ANDROIDNDKVER' in environ:
4!
712
            warning('$ANDROIDNDKVER is deprecated and no longer necessary, '
×
713
                    'the value you set is ignored')
714

715
    def hook(self, name):
4✔
716
        if not self.args.hook:
×
717
            return
×
718
        if not hasattr(self, "hook_module"):
×
719
            # first time, try to load the hook module
720
            self.hook_module = load_source(
×
721
                "pythonforandroid.hook", self.args.hook)
722
        if hasattr(self.hook_module, name):
×
723
            info("Hook: execute {}".format(name))
×
724
            getattr(self.hook_module, name)(self)
×
725
        else:
726
            info("Hook: ignore {}".format(name))
×
727

728
    @property
4✔
729
    def default_storage_dir(self):
4✔
730
        udd = user_data_dir('python-for-android')
×
731
        if ' ' in udd:
×
732
            udd = '~/.python-for-android'
×
733
        return udd
×
734

735
    @staticmethod
4✔
736
    def _read_configuration():
4✔
737
        # search for a .p4a configuration file in the current directory
738
        if not exists(".p4a"):
4!
739
            return
4✔
740
        info("Reading .p4a configuration")
×
741
        with open(".p4a") as fd:
×
742
            lines = fd.readlines()
×
743
        lines = [shlex.split(line)
×
744
                 for line in lines if not line.startswith("#")]
745
        for line in lines:
×
746
            for arg in line:
×
747
                sys.argv.append(arg)
×
748

749
    def recipes(self, args):
4✔
750
        """
751
        Prints recipes basic info, e.g.
752
        .. code-block:: bash
753
            python3      3.7.1
754
                depends: ['hostpython3', 'sqlite3', 'openssl', 'libffi']
755
                conflicts: []
756
                optional depends: ['sqlite3', 'libffi', 'openssl']
757
        """
758
        ctx = self.ctx
4✔
759
        if args.compact:
4!
760
            print(" ".join(set(Recipe.list_recipes(ctx))))
×
761
        else:
762
            for name in sorted(Recipe.list_recipes(ctx)):
4✔
763
                try:
4✔
764
                    recipe = Recipe.get_recipe(name, ctx)
4✔
765
                except (IOError, ValueError):
×
766
                    warning('Recipe "{}" could not be loaded'.format(name))
×
767
                except SyntaxError:
×
768
                    import traceback
×
769
                    traceback.print_exc()
×
770
                    warning(('Recipe "{}" could not be loaded due to a '
×
771
                             'syntax error').format(name))
772
                version = str(recipe.version)
4✔
773
                print('{Fore.BLUE}{Style.BRIGHT}{recipe.name:<12} '
4✔
774
                      '{Style.RESET_ALL}{Fore.LIGHTBLUE_EX}'
775
                      '{version:<8}{Style.RESET_ALL}'.format(
776
                            recipe=recipe, Fore=Out_Fore, Style=Out_Style,
777
                            version=version))
778
                print('    {Fore.GREEN}depends: {recipe.depends}'
4✔
779
                      '{Fore.RESET}'.format(recipe=recipe, Fore=Out_Fore))
780
                if recipe.conflicts:
4✔
781
                    print('    {Fore.RED}conflicts: {recipe.conflicts}'
4✔
782
                          '{Fore.RESET}'
783
                          .format(recipe=recipe, Fore=Out_Fore))
784
                if recipe.opt_depends:
4✔
785
                    print('    {Fore.YELLOW}optional depends: '
4✔
786
                          '{recipe.opt_depends}{Fore.RESET}'
787
                          .format(recipe=recipe, Fore=Out_Fore))
788

789
    def bootstraps(self, _args):
4✔
790
        """List all the bootstraps available to build with."""
791
        for bs in Bootstrap.all_bootstraps():
×
792
            bs = Bootstrap.get_bootstrap(bs, self.ctx)
×
793
            print('{Fore.BLUE}{Style.BRIGHT}{bs.name}{Style.RESET_ALL}'
×
794
                  .format(bs=bs, Fore=Out_Fore, Style=Out_Style))
795
            print('    {Fore.GREEN}depends: {bs.recipe_depends}{Fore.RESET}'
×
796
                  .format(bs=bs, Fore=Out_Fore))
797

798
    def clean(self, args):
4✔
799
        components = args.component
×
800

801
        component_clean_methods = {
×
802
            'all': self.clean_all,
803
            'dists': self.clean_dists,
804
            'distributions': self.clean_dists,
805
            'builds': self.clean_builds,
806
            'bootstrap_builds': self.clean_bootstrap_builds,
807
            'downloads': self.clean_download_cache}
808

809
        for component in components:
×
810
            if component not in component_clean_methods:
×
811
                raise BuildInterruptingException((
×
812
                    'Asked to clean "{}" but this argument is not '
813
                    'recognised'.format(component)))
814
            component_clean_methods[component](args)
×
815

816
    def clean_all(self, args):
4✔
817
        """Delete all build components; the package cache, package builds,
818
        bootstrap builds and distributions."""
819
        self.clean_dists(args)
×
820
        self.clean_builds(args)
×
821
        self.clean_download_cache(args)
×
822

823
    def clean_dists(self, _args):
4✔
824
        """Delete all compiled distributions in the internal distribution
825
        directory."""
826
        ctx = self.ctx
×
827
        rmdir(ctx.dist_dir)
×
828

829
    def clean_bootstrap_builds(self, _args):
4✔
830
        """Delete all the bootstrap builds."""
831
        rmdir(join(self.ctx.build_dir, 'bootstrap_builds'))
×
832
        # for bs in Bootstrap.all_bootstraps():
833
        #     bs = Bootstrap.get_bootstrap(bs, self.ctx)
834
        #     if bs.build_dir and exists(bs.build_dir):
835
        #         info('Cleaning build for {} bootstrap.'.format(bs.name))
836
        #         rmdir(bs.build_dir)
837

838
    def clean_builds(self, _args):
4✔
839
        """Delete all build caches for each recipe, python-install, java code
840
        and compiled libs collection.
841

842
        This does *not* delete the package download cache or the final
843
        distributions.  You can also use clean_recipe_build to delete the build
844
        of a specific recipe.
845
        """
846
        ctx = self.ctx
×
847
        rmdir(ctx.build_dir)
×
848
        rmdir(ctx.python_installs_dir)
×
849
        libs_dir = join(self.ctx.build_dir, 'libs_collections')
×
850
        rmdir(libs_dir)
×
851

852
    def clean_recipe_build(self, args):
4✔
853
        """Deletes the build files of the given recipe.
854

855
        This is intended for debug purposes. You may experience
856
        strange behaviour or problems with some recipes if their
857
        build has made unexpected state changes. If this happens, run
858
        clean_builds, or attempt to clean other recipes until things
859
        work again.
860
        """
861
        recipe = Recipe.get_recipe(args.recipe, self.ctx)
×
862
        info('Cleaning build for {} recipe.'.format(recipe.name))
×
863
        recipe.clean_build()
×
864
        if not args.no_clean_dists:
×
865
            self.clean_dists(args)
×
866

867
    def clean_download_cache(self, args):
4✔
868
        """ Deletes a download cache for recipes passed as arguments. If no
869
        argument is passed, it'll delete *all* downloaded caches. ::
870

871
            p4a clean_download_cache kivy,pyjnius
872

873
        This does *not* delete the build caches or final distributions.
874
        """
875
        ctx = self.ctx
×
876
        if hasattr(args, 'recipes') and args.recipes:
×
877
            for package in args.recipes:
×
878
                remove_path = join(ctx.packages_path, package)
×
879
                if exists(remove_path):
×
880
                    rmdir(remove_path)
×
881
                    info('Download cache removed for: "{}"'.format(package))
×
882
                else:
883
                    warning('No download cache found for "{}", skipping'.format(
×
884
                        package))
885
        else:
886
            if exists(ctx.packages_path):
×
887
                rmdir(ctx.packages_path)
×
888
                info('Download cache removed.')
×
889
            else:
890
                print('No cache found at "{}"'.format(ctx.packages_path))
×
891

892
    @require_prebuilt_dist
4✔
893
    def export_dist(self, args):
4✔
894
        """Copies a created dist to an output dir.
895

896
        This makes it easy to navigate to the dist to investigate it
897
        or call build.py, though you do not in general need to do this
898
        and can use the apk command instead.
899
        """
900
        ctx = self.ctx
×
901
        dist = dist_from_args(ctx, args)
×
902
        if dist.needs_build:
×
903
            raise BuildInterruptingException(
×
904
                'You asked to export a dist, but there is no dist '
905
                'with suitable recipes available. For now, you must '
906
                ' create one first with the create argument.')
907
        if args.symlink:
×
908
            shprint(sh.ln, '-s', dist.dist_dir, args.output_dir)
×
909
        else:
910
            shprint(sh.cp, '-r', dist.dist_dir, args.output_dir)
×
911

912
    @property
4✔
913
    def _dist(self):
4✔
914
        ctx = self.ctx
4✔
915
        dist = dist_from_args(ctx, self.args)
4✔
916
        ctx.distribution = dist
4✔
917
        return dist
4✔
918

919
    @staticmethod
4✔
920
    def _fix_args(args):
4✔
921
        """
922
        Manually fixing these arguments at the string stage is
923
        unsatisfactory and should probably be changed somehow, but
924
        we can't leave it until later as the build.py scripts assume
925
        they are in the current directory.
926
        works in-place
927
        :param args: parser args
928
        """
929

930
        fix_args = ('--dir', '--private', '--add-jar', '--add-source',
×
931
                    '--whitelist', '--blacklist', '--presplash', '--icon',
932
                    '--icon-bg', '--icon-fg')
933
        unknown_args = args.unknown_args
×
934

935
        for asset in args.assets:
×
936
            if ":" in asset:
×
937
                asset_src, asset_dest = asset.split(":")
×
938
            else:
939
                asset_src = asset_dest = asset
×
940
            # take abspath now, because build.py will be run in bootstrap dir
941
            unknown_args += ["--asset", os.path.abspath(asset_src)+":"+asset_dest]
×
942
        for resource in args.resources:
×
943
            if ":" in resource:
×
944
                resource_src, resource_dest = resource.split(":")
×
945
            else:
946
                resource_src = resource
×
947
                resource_dest = ""
×
948
            # take abspath now, because build.py will be run in bootstrap dir
949
            unknown_args += ["--resource", os.path.abspath(resource_src)+":"+resource_dest]
×
950
        for i, arg in enumerate(unknown_args):
×
951
            argx = arg.split('=')
×
952
            if argx[0] in fix_args:
×
953
                if len(argx) > 1:
×
954
                    unknown_args[i] = '='.join(
×
955
                        (argx[0], realpath(expanduser(argx[1]))))
956
                elif i + 1 < len(unknown_args):
×
957
                    unknown_args[i+1] = realpath(expanduser(unknown_args[i+1]))
×
958

959
    @staticmethod
4✔
960
    def _prepare_release_env(args):
4✔
961
        """
962
        prepares envitonment dict with the necessary flags for signing an apk
963
        :param args: parser args
964
        """
965
        env = os.environ.copy()
×
966
        if args.build_mode == 'release':
×
967
            if args.keystore:
×
968
                env['P4A_RELEASE_KEYSTORE'] = realpath(expanduser(args.keystore))
×
969
            if args.signkey:
×
970
                env['P4A_RELEASE_KEYALIAS'] = args.signkey
×
971
            if args.keystorepw:
×
972
                env['P4A_RELEASE_KEYSTORE_PASSWD'] = args.keystorepw
×
973
            if args.signkeypw:
×
974
                env['P4A_RELEASE_KEYALIAS_PASSWD'] = args.signkeypw
×
975
            elif args.keystorepw and 'P4A_RELEASE_KEYALIAS_PASSWD' not in env:
×
976
                env['P4A_RELEASE_KEYALIAS_PASSWD'] = args.keystorepw
×
977

978
        return env
×
979

980
    def _build_package(self, args, package_type):
4✔
981
        """
982
        Creates an android package using gradle
983
        :param args: parser args
984
        :param package_type: one of 'apk', 'aar', 'aab'
985
        :return (gradle output, build_args)
986
        """
987
        ctx = self.ctx
×
988
        dist = self._dist
×
989
        bs = Bootstrap.get_bootstrap(args.bootstrap, ctx)
×
990
        ctx.prepare_bootstrap(bs)
×
991
        self._fix_args(args)
×
992
        env = self._prepare_release_env(args)
×
993

994
        with current_directory(dist.dist_dir):
×
995
            self.hook("before_apk_build")
×
996
            os.environ["ANDROID_API"] = str(self.ctx.android_api)
×
997
            build = load_source('build', join(dist.dist_dir, 'build.py'))
×
998
            build_args = build.parse_args_and_make_package(
×
999
                args.unknown_args
1000
            )
1001

1002
            self.hook("after_apk_build")
×
1003
            self.hook("before_apk_assemble")
×
1004
            build_tools_versions = os.listdir(join(ctx.sdk_dir,
×
1005
                                                   'build-tools'))
1006
            build_tools_version = max_build_tool_version(build_tools_versions)
×
1007
            info(('Detected highest available build tools '
×
1008
                  'version to be {}').format(build_tools_version))
1009

1010
            if Version(build_tools_version.replace(" ", "")) < Version('25.0'):
×
1011
                raise BuildInterruptingException(
×
1012
                    'build_tools >= 25 is required, but %s is installed' % build_tools_version)
1013
            if not exists("gradlew"):
×
1014
                raise BuildInterruptingException("gradlew file is missing")
×
1015

1016
            env["ANDROID_NDK_HOME"] = self.ctx.ndk_dir
×
1017
            env["ANDROID_HOME"] = self.ctx.sdk_dir
×
1018

1019
            gradlew = sh.Command('./gradlew')
×
1020

1021
            if exists('/usr/bin/dos2unix'):
×
1022
                # .../dists/bdisttest_python3/gradlew
1023
                # .../build/bootstrap_builds/sdl2-python3/gradlew
1024
                # if docker on windows, gradle contains CRLF
1025
                output = shprint(
×
1026
                    sh.Command('dos2unix'), gradlew._path.decode('utf8'),
1027
                    _tail=20, _critical=True, _env=env
1028
                )
1029
            if args.build_mode == "debug":
×
1030
                if package_type == "aab":
×
1031
                    raise BuildInterruptingException(
×
1032
                        "aab is meant only for distribution and is not available in debug mode. "
1033
                        "Instead, you can use apk while building for debugging purposes."
1034
                    )
1035
                gradle_task = "assembleDebug"
×
1036
            elif args.build_mode == "release":
×
1037
                if package_type in ["apk", "aar"]:
×
1038
                    gradle_task = "assembleRelease"
×
1039
                elif package_type == "aab":
×
1040
                    gradle_task = "bundleRelease"
×
1041
            else:
1042
                raise BuildInterruptingException(
×
1043
                    "Unknown build mode {} for apk()".format(args.build_mode))
1044

1045
            # WARNING: We should make sure to clean the build directory before building.
1046
            # See PR: kivy/python-for-android#2705
1047
            output = shprint(gradlew, "clean", gradle_task, _tail=20,
×
1048
                             _critical=True, _env=env)
1049
        return output, build_args
×
1050

1051
    def _finish_package(self, args, output, build_args, package_type, output_dir):
4✔
1052
        """
1053
        Finishes the package after the gradle script run
1054
        :param args: the parser args
1055
        :param output: RunningCommand output
1056
        :param build_args: build args as returned by build.parse_args
1057
        :param package_type: one of 'apk', 'aar', 'aab'
1058
        :param output_dir: where to put the package file
1059
        """
1060

1061
        package_glob = "*-{}.%s" % package_type
×
1062
        package_add_version = True
×
1063

1064
        self.hook("after_apk_assemble")
×
1065

1066
        info_main('# Copying android package to current directory')
×
1067

1068
        package_re = re.compile(r'.*Package: (.*\.apk)$')
×
1069
        package_file = None
×
1070
        for line in reversed(output.splitlines()):
×
1071
            m = package_re.match(line)
×
1072
            if m:
×
1073
                package_file = m.groups()[0]
×
1074
                break
×
1075
        if not package_file:
×
1076
            info_main('# Android package filename not found in build output. Guessing...')
×
1077
            if args.build_mode == "release":
×
1078
                suffixes = ("release", "release-unsigned")
×
1079
            else:
1080
                suffixes = ("debug", )
×
1081
            for suffix in suffixes:
×
1082

1083
                package_files = glob.glob(join(output_dir, package_glob.format(suffix)))
×
1084
                if package_files:
×
1085
                    if len(package_files) > 1:
×
1086
                        info('More than one built APK found... guessing you '
×
1087
                             'just built {}'.format(package_files[-1]))
1088
                    package_file = package_files[-1]
×
1089
                    break
×
1090
            else:
1091
                raise BuildInterruptingException('Couldn\'t find the built APK')
×
1092

1093
        info_main('# Found android package file: {}'.format(package_file))
×
1094
        package_extension = f".{package_type}"
×
1095
        if package_add_version:
×
1096
            info('# Add version number to android package')
×
1097
            package_name = basename(package_file)[:-len(package_extension)]
×
1098
            package_file_dest = "{}-{}{}".format(
×
1099
                package_name, build_args.version, package_extension)
1100
            info('# Android package renamed to {}'.format(package_file_dest))
×
1101
            shprint(sh.cp, package_file, package_file_dest)
×
1102
        else:
1103
            shprint(sh.cp, package_file, './')
×
1104

1105
    @require_prebuilt_dist
4✔
1106
    def apk(self, args):
4✔
1107
        output, build_args = self._build_package(args, package_type='apk')
×
1108
        output_dir = join(self._dist.dist_dir, "build", "outputs", 'apk', args.build_mode)
×
1109
        self._finish_package(args, output, build_args, 'apk', output_dir)
×
1110

1111
    @require_prebuilt_dist
4✔
1112
    def aar(self, args):
4✔
1113
        output, build_args = self._build_package(args, package_type='aar')
×
1114
        output_dir = join(self._dist.dist_dir, "build", "outputs", 'aar')
×
1115
        self._finish_package(args, output, build_args, 'aar', output_dir)
×
1116

1117
    @require_prebuilt_dist
4✔
1118
    def aab(self, args):
4✔
1119
        output, build_args = self._build_package(args, package_type='aab')
×
1120
        output_dir = join(self._dist.dist_dir, "build", "outputs", 'bundle', args.build_mode)
×
1121
        self._finish_package(args, output, build_args, 'aab', output_dir)
×
1122

1123
    @require_prebuilt_dist
4✔
1124
    def create(self, args):
4✔
1125
        """Create a distribution directory if it doesn't already exist, run
1126
        any recipes if necessary, and build the apk.
1127
        """
1128
        pass  # The decorator does everything
4✔
1129

1130
    def archs(self, _args):
4✔
1131
        """List the target architectures available to be built for."""
1132
        print('{Style.BRIGHT}Available target architectures are:'
×
1133
              '{Style.RESET_ALL}'.format(Style=Out_Style))
1134
        for arch in self.ctx.archs:
×
1135
            print('    {}'.format(arch.arch))
×
1136

1137
    def dists(self, args):
4✔
1138
        """The same as :meth:`distributions`."""
1139
        self.distributions(args)
×
1140

1141
    def distributions(self, _args):
4✔
1142
        """Lists all distributions currently available (i.e. that have already
1143
        been built)."""
1144
        ctx = self.ctx
×
1145
        dists = Distribution.get_distributions(ctx)
×
1146

1147
        if dists:
×
1148
            print('{Style.BRIGHT}Distributions currently installed are:'
×
1149
                  '{Style.RESET_ALL}'.format(Style=Out_Style))
1150
            pretty_log_dists(dists, print)
×
1151
        else:
1152
            print('{Style.BRIGHT}There are no dists currently built.'
×
1153
                  '{Style.RESET_ALL}'.format(Style=Out_Style))
1154

1155
    def delete_dist(self, _args):
4✔
1156
        dist = self._dist
×
1157
        if not dist.folder_exists():
×
1158
            info('No dist exists that matches your specifications, '
×
1159
                 'exiting without deleting.')
1160
            return
×
1161
        dist.delete()
×
1162

1163
    def sdk_tools(self, args):
4✔
1164
        """Runs the android binary from the detected SDK directory, passing
1165
        all arguments straight to it. This binary is used to install
1166
        e.g. platform-tools for different API level targets. This is
1167
        intended as a convenience function if android is not in your
1168
        $PATH.
1169
        """
1170
        ctx = self.ctx
×
1171
        ctx.prepare_build_environment(user_sdk_dir=self.sdk_dir,
×
1172
                                      user_ndk_dir=self.ndk_dir,
1173
                                      user_android_api=self.android_api,
1174
                                      user_ndk_api=self.ndk_api)
1175
        android = sh.Command(join(ctx.sdk_dir, 'tools', args.tool))
×
1176
        output = android(
×
1177
            *args.unknown_args, _iter=True, _out_bufsize=1, _err_to_out=True)
1178
        for line in output:
×
1179
            sys.stdout.write(line)
×
1180
            sys.stdout.flush()
×
1181

1182
    def adb(self, args):
4✔
1183
        """Runs the adb binary from the detected SDK directory, passing all
1184
        arguments straight to it. This is intended as a convenience
1185
        function if adb is not in your $PATH.
1186
        """
1187
        self._adb(args.unknown_args)
×
1188

1189
    def logcat(self, args):
4✔
1190
        """Runs ``adb logcat`` using the adb binary from the detected SDK
1191
        directory. All extra args are passed as arguments to logcat."""
1192
        self._adb(['logcat'] + args.unknown_args)
×
1193

1194
    def _adb(self, commands):
4✔
1195
        """Call the adb executable from the SDK, passing the given commands as
1196
        arguments."""
1197
        ctx = self.ctx
×
1198
        ctx.prepare_build_environment(user_sdk_dir=self.sdk_dir,
×
1199
                                      user_ndk_dir=self.ndk_dir,
1200
                                      user_android_api=self.android_api,
1201
                                      user_ndk_api=self.ndk_api)
1202
        if platform in ('win32', 'cygwin'):
×
1203
            adb = sh.Command(join(ctx.sdk_dir, 'platform-tools', 'adb.exe'))
×
1204
        else:
1205
            adb = sh.Command(join(ctx.sdk_dir, 'platform-tools', 'adb'))
×
1206
        info_notify('Starting adb...')
×
1207
        output = adb(*commands, _iter=True, _out_bufsize=1, _err_to_out=True)
×
1208
        for line in output:
×
1209
            sys.stdout.write(line)
×
1210
            sys.stdout.flush()
×
1211

1212
    def recommendations(self, args):
4✔
1213
        print_recommendations()
4✔
1214

1215
    def build_status(self, _args):
4✔
1216
        """Print the status of the specified build. """
1217
        print('{Style.BRIGHT}Bootstraps whose core components are probably '
×
1218
              'already built:{Style.RESET_ALL}'.format(Style=Out_Style))
1219

1220
        bootstrap_dir = join(self.ctx.build_dir, 'bootstrap_builds')
×
1221
        if exists(bootstrap_dir):
×
1222
            for filen in os.listdir(bootstrap_dir):
×
1223
                print('    {Fore.GREEN}{Style.BRIGHT}{filen}{Style.RESET_ALL}'
×
1224
                      .format(filen=filen, Fore=Out_Fore, Style=Out_Style))
1225

1226
        print('{Style.BRIGHT}Recipes that are probably already built:'
×
1227
              '{Style.RESET_ALL}'.format(Style=Out_Style))
1228
        other_builds_dir = join(self.ctx.build_dir, 'other_builds')
×
1229
        if exists(other_builds_dir):
×
1230
            for filen in sorted(os.listdir(other_builds_dir)):
×
1231
                name = filen.split('-')[0]
×
1232
                dependencies = filen.split('-')[1:]
×
1233
                recipe_str = ('    {Style.BRIGHT}{Fore.GREEN}{name}'
×
1234
                              '{Style.RESET_ALL}'.format(
1235
                                  Style=Out_Style, name=name, Fore=Out_Fore))
1236
                if dependencies:
×
1237
                    recipe_str += (
×
1238
                        ' ({Fore.BLUE}with ' + ', '.join(dependencies) +
1239
                        '{Fore.RESET})').format(Fore=Out_Fore)
1240
                recipe_str += '{Style.RESET_ALL}'.format(Style=Out_Style)
×
1241
                print(recipe_str)
×
1242

1243

1244
if __name__ == "__main__":
1245
    main()
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