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

kivy / python-for-android / 4033302547

pending completion
4033302547

push

github

GitHub
Merge pull request #2740 from misl6/release-2023.01.28

907 of 2274 branches covered (39.89%)

Branch coverage included in aggregate %.

52 of 67 new or added lines in 5 files covered. (77.61%)

4665 of 7403 relevant lines covered (63.01%)

2.49 hits per line

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

0.0
/pythonforandroid/bdistapk.py
1
from setuptools import Command
×
2

3
import sys
×
4
from os.path import realpath, join, exists, dirname, curdir, basename, split
×
5
from os import makedirs
×
6
from glob import glob
×
7
from shutil import rmtree, copyfile
×
8

9

10
def argv_contains(t):
×
11
    for arg in sys.argv:
×
12
        if arg.startswith(t):
×
13
            return True
×
14
    return False
×
15

16

17
class Bdist(Command):
×
18

19
    user_options = []
×
20
    package_type = None
×
21

22
    def initialize_options(self):
×
23
        for option in self.user_options:
×
24
            setattr(self, option[0].strip('=').replace('-', '_'), None)
×
25

26
        option_dict = self.distribution.get_option_dict(self.package_type)
×
27

28
        # This is a hack, we probably aren't supposed to loop through
29
        # the option_dict so early because distutils does exactly the
30
        # same thing later to check that we support the
31
        # options. However, it works...
32
        for (option, (source, value)) in option_dict.items():
×
33
            setattr(self, option, str(value))
×
34

35
    def finalize_options(self):
×
36

37
        setup_options = self.distribution.get_option_dict(self.package_type)
×
38
        for (option, (source, value)) in setup_options.items():
×
39
            if source == 'command line':
×
40
                continue
×
41
            if not argv_contains('--' + option):
×
42
                # allow 'permissions': ['permission', 'permission] in apk
43
                if option == 'permissions':
×
44
                    for perm in value:
×
45
                        sys.argv.append('--permission={}'.format(perm))
×
NEW
46
                elif option == 'orientation':
×
NEW
47
                    for orient in value:
×
NEW
48
                        sys.argv.append('--orientation={}'.format(orient))
×
49
                elif value in (None, 'None'):
×
50
                    sys.argv.append('--{}'.format(option))
×
51
                else:
52
                    sys.argv.append('--{}={}'.format(option, value))
×
53

54
        # Inject some argv options from setup.py if the user did not
55
        # provide them
56
        if not argv_contains('--name'):
×
57
            name = self.distribution.get_name()
×
58
            sys.argv.append('--name="{}"'.format(name))
×
59
            self.name = name
×
60

61
        if not argv_contains('--package'):
×
62
            package = 'org.test.{}'.format(self.name.lower().replace(' ', ''))
×
63
            print('WARNING: You did not supply an Android package '
×
64
                  'identifier, trying {} instead.'.format(package))
65
            print('         This may fail if this is not a valid identifier')
×
66
            sys.argv.append('--package={}'.format(package))
×
67

68
        if not argv_contains('--version'):
×
69
            version = self.distribution.get_version()
×
70
            sys.argv.append('--version={}'.format(version))
×
71

72
        if not argv_contains('--arch'):
×
73
            arch = 'armeabi-v7a'
×
74
            self.arch = arch
×
75
            sys.argv.append('--arch={}'.format(arch))
×
76

77
    def run(self):
×
78
        self.prepare_build_dir()
×
79

80
        from pythonforandroid.entrypoints import main
×
81
        sys.argv[1] = self.package_type
×
82
        main()
×
83

84
    def prepare_build_dir(self):
×
85

86
        if argv_contains('--private') and not argv_contains('--launcher'):
×
87
            print('WARNING: Received --private argument when this would '
×
88
                  'normally be generated automatically.')
89
            print('         This is probably bad unless you meant to do '
×
90
                  'that.')
91

92
        bdist_dir = 'build/bdist.android-{}'.format(self.arch)
×
93
        if exists(bdist_dir):
×
94
            rmtree(bdist_dir)
×
95
        makedirs(bdist_dir)
×
96

97
        globs = []
×
98
        for directory, patterns in self.distribution.package_data.items():
×
99
            for pattern in patterns:
×
100
                globs.append(join(directory, pattern))
×
101

102
        filens = []
×
103
        for pattern in globs:
×
104
            filens.extend(glob(pattern))
×
105

106
        main_py_dirs = []
×
107
        if not argv_contains('--launcher'):
×
108
            for filen in filens:
×
109
                new_dir = join(bdist_dir, dirname(filen))
×
110
                if not exists(new_dir):
×
111
                    makedirs(new_dir)
×
112
                print('Including {}'.format(filen))
×
113
                copyfile(filen, join(bdist_dir, filen))
×
114
                if basename(filen) in ('main.py', 'main.pyc'):
×
115
                    main_py_dirs.append(filen)
×
116

117
        # This feels ridiculous, but how else to define the main.py dir?
118
        # Maybe should just fail?
119
        if not main_py_dirs and not argv_contains('--launcher'):
×
120
            print('ERROR: Could not find main.py, so no app build dir defined')
×
121
            print('You should name your app entry point main.py')
×
122
            exit(1)
×
123
        if len(main_py_dirs) > 1:
×
124
            print('WARNING: Multiple main.py dirs found, using the shortest path')
×
125
        main_py_dirs = sorted(main_py_dirs, key=lambda j: len(split(j)))
×
126

127
        if not argv_contains('--launcher'):
×
128
            sys.argv.append('--private={}'.format(
×
129
                join(realpath(curdir), bdist_dir, dirname(main_py_dirs[0])))
130
            )
131

132

133
class BdistAPK(Bdist):
×
134
    """distutil command handler for 'apk'."""
135
    description = 'Create an APK with python-for-android'
×
136
    package_type = 'apk'
×
137

138

139
class BdistAAR(Bdist):
×
140
    """distutil command handler for 'aar'."""
141
    description = 'Create an AAR with python-for-android'
×
142
    package_type = 'aar'
×
143

144

145
class BdistAAB(Bdist):
×
146
    """distutil command handler for 'aab'."""
147
    description = 'Create an AAB with python-for-android'
×
148
    package_type = 'aab'
×
149

150

151
def _set_user_options():
×
152
    # This seems like a silly way to do things, but not sure if there's a
153
    # better way to pass arbitrary options onwards to p4a
154
    user_options = [('requirements=', None, None), ]
×
155
    for i, arg in enumerate(sys.argv):
×
156
        if arg.startswith('--'):
×
157
            if ('=' in arg or
×
158
                    (i < (len(sys.argv) - 1) and not sys.argv[i+1].startswith('-'))):
159
                user_options.append((arg[2:].split('=')[0] + '=', None, None))
×
160
            else:
161
                user_options.append((arg[2:], None, None))
×
162

163
    BdistAPK.user_options = user_options
×
164
    BdistAAB.user_options = user_options
×
165
    BdistAAR.user_options = user_options
×
166

167

168
_set_user_options()
×
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