Skip to content

Commit 93fe511

Browse files
misl6ShyamQt
authored andcommitted
Fixes an issue regarding blacklist and bytecode compile + some cleanup (kivy#2693)
1 parent 501f330 commit 93fe511

File tree

10 files changed

+44
-72
lines changed

10 files changed

+44
-72
lines changed

doc/source/buildoptions.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ options (this list may not be exhaustive):
8989
- ``--service``: A service name and the Python script it should
9090
run. See :ref:`arbitrary_scripts_services`.
9191
- ``--add-source``: Add a source directory to the app's Java code.
92-
- ``--no-compile-pyo``: Do not optimise .py files to .pyo.
92+
- ``--no-byte-compile-python``: Skip byte compile for .py files.
9393
- ``--enable-androidx``: Enable AndroidX support library.
9494

9595

doc/source/launcher.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ grab an old (cached) package instead of a fresh one.
4848
.. warning::
4949

5050
Do not use any of `--private`, `--public`, `--dir` or other arguments for
51-
adding `main.py` or `main.pyo` to the app. The argument `--launcher` is
51+
adding `main.py` or `main.pyc` to the app. The argument `--launcher` is
5252
above them and tells the p4a to build the launcher version of the APK.
5353

5454
Usage

pythonforandroid/bdistapk.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ def prepare_build_dir(self):
108108
makedirs(new_dir)
109109
print('Including {}'.format(filen))
110110
copyfile(filen, join(bdist_dir, filen))
111-
if basename(filen) in ('main.py', 'main.pyo'):
111+
if basename(filen) in ('main.py', 'main.pyc'):
112112
main_py_dirs.append(filen)
113113

114114
# This feels ridiculous, but how else to define the main.py dir?

pythonforandroid/bootstraps/common/build/build.py

Lines changed: 33 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,6 @@ def get_hostpython():
4040
return get_dist_info_for('hostpython')
4141

4242

43-
def get_python_version():
44-
return get_dist_info_for('python_version')
45-
46-
4743
def get_bootstrap_name():
4844
return get_dist_info_for('bootstrap')
4945

@@ -58,7 +54,6 @@ def get_bootstrap_name():
5854
curdir = dirname(__file__)
5955

6056
PYTHON = get_hostpython()
61-
PYTHON_VERSION = get_python_version()
6257
if PYTHON is not None and not exists(PYTHON):
6358
PYTHON = None
6459

@@ -73,17 +68,16 @@ def get_bootstrap_name():
7368
'~',
7469
'*.bak',
7570
'*.swp',
71+
72+
# Android artifacts
73+
'*.apk',
74+
'*.aab',
7675
]
77-
# pyc/py
78-
if PYTHON is not None:
79-
BLACKLIST_PATTERNS.append('*.py')
8076

8177
WHITELIST_PATTERNS = []
8278
if get_bootstrap_name() in ('sdl2', 'webview', 'service_only'):
8379
WHITELIST_PATTERNS.append('pyconfig.h')
8480

85-
python_files = []
86-
8781

8882
environment = jinja2.Environment(loader=jinja2.FileSystemLoader(
8983
join(curdir, 'templates')))
@@ -150,23 +144,11 @@ def listfiles(d):
150144
yield fn
151145

152146

153-
def make_tar(tfn, source_dirs, ignore_path=[], optimize_python=True):
147+
def make_tar(tfn, source_dirs, byte_compile_python=False, optimize_python=True):
154148
'''
155149
Make a zip file `fn` from the contents of source_dis.
156150
'''
157151

158-
# selector function
159-
def select(fn):
160-
rfn = realpath(fn)
161-
for p in ignore_path:
162-
if p.endswith('/'):
163-
p = p[:-1]
164-
if rfn.startswith(p):
165-
return False
166-
if rfn in python_files:
167-
return False
168-
return not is_blacklist(fn)
169-
170152
def clean(tinfo):
171153
"""cleaning function (for reproducible builds)"""
172154
tinfo.uid = tinfo.gid = 0
@@ -178,9 +160,12 @@ def clean(tinfo):
178160
files = []
179161
for sd in source_dirs:
180162
sd = realpath(sd)
181-
compile_dir(sd, optimize_python=optimize_python)
182-
files += [(x, relpath(realpath(x), sd)) for x in listfiles(sd)
183-
if select(x)]
163+
for fn in listfiles(sd):
164+
if is_blacklist(fn):
165+
continue
166+
if fn.endswith('.py') and byte_compile_python:
167+
fn = compile_py_file(fn, optimize_python=optimize_python)
168+
files.append((fn, relpath(realpath(fn), sd)))
184169
files.sort() # deterministic
185170

186171
# create tar.gz of thoses files
@@ -210,18 +195,15 @@ def clean(tinfo):
210195
gf.close()
211196

212197

213-
def compile_dir(dfn, optimize_python=True):
198+
def compile_py_file(python_file, optimize_python=True):
214199
'''
215-
Compile *.py in directory `dfn` to *.pyo
200+
Compile python_file to *.pyc and return the filename of the *.pyc file.
216201
'''
217202

218203
if PYTHON is None:
219204
return
220205

221-
if int(PYTHON_VERSION[0]) >= 3:
222-
args = [PYTHON, '-m', 'compileall', '-b', '-f', dfn]
223-
else:
224-
args = [PYTHON, '-m', 'compileall', '-f', dfn]
206+
args = [PYTHON, '-m', 'compileall', '-b', '-f', python_file]
225207
if optimize_python:
226208
# -OO = strip docstrings
227209
args.insert(1, '-OO')
@@ -233,16 +215,18 @@ def compile_dir(dfn, optimize_python=True):
233215
'error, see logs above')
234216
exit(1)
235217

218+
return ".".join([os.path.splitext(python_file)[0], "pyc"])
219+
236220

237221
def make_package(args):
238-
# If no launcher is specified, require a main.py/main.pyo:
222+
# If no launcher is specified, require a main.py/main.pyc:
239223
if (get_bootstrap_name() != "sdl" or args.launcher is None) and \
240224
get_bootstrap_name() not in ["webview", "service_library"]:
241225
# (webview doesn't need an entrypoint, apparently)
242226
if args.private is None or (
243227
not exists(join(realpath(args.private), 'main.py')) and
244-
not exists(join(realpath(args.private), 'main.pyo'))):
245-
print('''BUILD FAILURE: No main.py(o) found in your app directory. This
228+
not exists(join(realpath(args.private), 'main.pyc'))):
229+
print('''BUILD FAILURE: No main.py(c) found in your app directory. This
246230
file must exist to act as the entry point for you app. If your app is
247231
started by a file with a different name, rename it to main.py or add a
248232
main.py that loads it.''')
@@ -290,7 +274,6 @@ def make_package(args):
290274
variants = [
291275
copy_path,
292276
copy_path.partition(".")[0] + ".pyc",
293-
copy_path.partition(".")[0] + ".pyo",
294277
]
295278
# Check in all variants with all possible endings:
296279
for variant in variants:
@@ -326,11 +309,17 @@ def make_package(args):
326309
for arch in get_dist_info_for("archs"):
327310
libs_dir = f"libs/{arch}"
328311
make_tar(
329-
join(libs_dir, 'libpybundle.so'), [f'_python_bundle__{arch}'], args.ignore_path,
330-
optimize_python=args.optimize_python)
312+
join(libs_dir, "libpybundle.so"),
313+
[f"_python_bundle__{arch}"],
314+
byte_compile_python=args.byte_compile_python,
315+
optimize_python=args.optimize_python,
316+
)
331317
make_tar(
332-
join(assets_dir, 'private.tar'), private_tar_dirs, args.ignore_path,
333-
optimize_python=args.optimize_python)
318+
join(assets_dir, "private.tar"),
319+
private_tar_dirs,
320+
byte_compile_python=args.byte_compile_python,
321+
optimize_python=args.optimize_python,
322+
)
334323
finally:
335324
for directory in _temp_dirs_to_clean:
336325
shutil.rmtree(directory)
@@ -824,8 +813,6 @@ def parse_args_and_make_package(args=None):
824813
ap.add_argument('--try-system-python-compile', dest='try_system_python_compile',
825814
action='store_true',
826815
help='Use the system python during compileall if possible.')
827-
ap.add_argument('--no-compile-pyo', dest='no_compile_pyo', action='store_true',
828-
help='Do not optimise .py files to .pyo.')
829816
ap.add_argument('--sign', action='store_true',
830817
help=('Try to sign the APK with your credentials. You must set '
831818
'the appropriate environment variables.'))
@@ -844,9 +831,12 @@ def parse_args_and_make_package(args=None):
844831
'files (containing your main.py entrypoint). '
845832
'See https://developer.android.com/guide/topics/data/'
846833
'autobackup#IncludingFiles for more information'))
834+
ap.add_argument('--no-byte-compile-python', dest='byte_compile_python',
835+
action='store_false', default=True,
836+
help='Skip byte compile for .py files.')
847837
ap.add_argument('--no-optimize-python', dest='optimize_python',
848838
action='store_false', default=True,
849-
help=('Whether to compile to optimised .pyo files, using -OO '
839+
help=('Whether to compile to optimised .pyc files, using -OO '
850840
'(strips docstrings and asserts)'))
851841
ap.add_argument('--extra-manifest-xml', default='',
852842
help=('Extra xml to write directly inside the <manifest> element of'
@@ -881,8 +871,6 @@ def _read_configuration():
881871

882872
args = ap.parse_args(args)
883873

884-
args.ignore_path = []
885-
886874
if args.name and args.name[0] == '"' and args.name[-1] == '"':
887875
args.name = args.name[1:-1]
888876

@@ -925,10 +913,6 @@ def _read_configuration():
925913
else:
926914
PYTHON = python_executable
927915

928-
if args.no_compile_pyo:
929-
PYTHON = None
930-
BLACKLIST_PATTERNS.remove('*.py')
931-
932916
if args.blacklist:
933917
with open(args.blacklist) as fd:
934918
patterns = [x.strip() for x in fd.read().splitlines()

pythonforandroid/bootstraps/common/build/jni/application/src/start.c

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -251,14 +251,10 @@ int main(int argc, char *argv[]) {
251251
*/
252252
LOGP("Run user program, change dir and execute entrypoint");
253253

254-
/* Get the entrypoint, search the .pyo then .py
254+
/* Get the entrypoint, search the .pyc then .py
255255
*/
256256
char *dot = strrchr(env_entrypoint, '.');
257-
#if PY_MAJOR_VERSION > 2
258257
char *ext = ".pyc";
259-
#else
260-
char *ext = ".pyo";
261-
#endif
262258
if (dot <= 0) {
263259
LOGP("Invalid entrypoint, abort.");
264260
return -1;
@@ -281,14 +277,10 @@ int main(int argc, char *argv[]) {
281277
strcpy(entrypoint, env_entrypoint);
282278
}
283279
} else if (!strcmp(dot, ".py")) {
284-
/* if .py is passed, check the pyo version first */
280+
/* if .py is passed, check the pyc version first */
285281
strcpy(entrypoint, env_entrypoint);
286282
entrypoint[strlen(env_entrypoint) + 1] = '\0';
287-
#if PY_MAJOR_VERSION > 2
288283
entrypoint[strlen(env_entrypoint)] = 'c';
289-
#else
290-
entrypoint[strlen(env_entrypoint)] = 'o';
291-
#endif
292284
if (!file_exists(entrypoint)) {
293285
/* fallback on pure python version */
294286
if (!file_exists(env_entrypoint)) {

pythonforandroid/bootstraps/sdl2/build/src/main/java/org/kivy/android/PythonActivity.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -419,11 +419,10 @@ public void run() {
419419
}
420420

421421
public String getEntryPoint(String search_dir) {
422-
/* Get the main file (.pyc|.pyo|.py) depending on if we
422+
/* Get the main file (.pyc|.py) depending on if we
423423
* have a compiled version or not.
424424
*/
425425
List<String> entryPoints = new ArrayList<String>();
426-
entryPoints.add("main.pyo"); // python 2 compiled files
427426
entryPoints.add("main.pyc"); // python 3 compiled files
428427
for (String value : entryPoints) {
429428
File mainFile = new File(search_dir + "/" + value);

pythonforandroid/bootstraps/service_only/build/src/main/java/org/kivy/android/PythonActivity.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,10 @@ public String getAppRoot() {
4646
}
4747

4848
public String getEntryPoint(String search_dir) {
49-
/* Get the main file (.pyc|.pyo|.py) depending on if we
49+
/* Get the main file (.pyc|.py) depending on if we
5050
* have a compiled version or not.
5151
*/
5252
List<String> entryPoints = new ArrayList<String>();
53-
entryPoints.add("main.pyo"); // python 2 compiled files
5453
entryPoints.add("main.pyc"); // python 3 compiled files
5554
for (String value : entryPoints) {
5655
File mainFile = new File(search_dir + "/" + value);

pythonforandroid/bootstraps/webview/build/src/main/java/org/kivy/android/PythonActivity.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,10 @@ public String getAppRoot() {
6868
}
6969

7070
public String getEntryPoint(String search_dir) {
71-
/* Get the main file (.pyc|.pyo|.py) depending on if we
71+
/* Get the main file (.pyc|.py) depending on if we
7272
* have a compiled version or not.
7373
*/
7474
List<String> entryPoints = new ArrayList<String>();
75-
entryPoints.add("main.pyo"); // python 2 compiled files
7675
entryPoints.add("main.pyc"); // python 3 compiled files
7776
for (String value : entryPoints) {
7877
File mainFile = new File(search_dir + "/" + value);

pythonforandroid/build.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,6 @@ def has_package(self, name, arch=None):
456456
return (exists(join(site_packages_dir, name)) or
457457
exists(join(site_packages_dir, name + '.py')) or
458458
exists(join(site_packages_dir, name + '.pyc')) or
459-
exists(join(site_packages_dir, name + '.pyo')) or
460459
exists(join(site_packages_dir, name + '.so')) or
461460
glob.glob(join(site_packages_dir, name + '-*.egg')))
462461

pythonforandroid/recipes/python3/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -364,11 +364,11 @@ def create_python_bundle(self, dirn, arch):
364364
self.major_minor_version_string
365365
))
366366

367-
# Compile to *.pyc/*.pyo the python modules
367+
# Compile to *.pyc the python modules
368368
self.compile_python_files(modules_build_dir)
369-
# Compile to *.pyc/*.pyo the standard python library
369+
# Compile to *.pyc the standard python library
370370
self.compile_python_files(join(self.get_build_dir(arch.arch), 'Lib'))
371-
# Compile to *.pyc/*.pyo the other python packages (site-packages)
371+
# Compile to *.pyc the other python packages (site-packages)
372372
self.compile_python_files(self.ctx.get_python_install_dir(arch.arch))
373373

374374
# Bundle compiled python modules to a folder

0 commit comments

Comments
 (0)