@@ -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-
4743def get_bootstrap_name ():
4844 return get_dist_info_for ('bootstrap' )
4945
@@ -58,7 +54,6 @@ def get_bootstrap_name():
5854curdir = dirname (__file__ )
5955
6056PYTHON = get_hostpython ()
61- PYTHON_VERSION = get_python_version ()
6257if 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
8177WHITELIST_PATTERNS = []
8278if get_bootstrap_name () in ('sdl2' , 'webview' , 'service_only' ):
8379 WHITELIST_PATTERNS .append ('pyconfig.h' )
8480
85- python_files = []
86-
8781
8882environment = 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
237221def 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
246230file must exist to act as the entry point for you app. If your app is
247231started by a file with a different name, rename it to main.py or add a
248232main.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 ()
0 commit comments