Skip to content

Commit 75bbbc7

Browse files
AppManager: try .mpy before .py
1 parent 40ca3da commit 75bbbc7

1 file changed

Lines changed: 45 additions & 13 deletions

File tree

internal_filesystem/lib/mpos/content/app_manager.py

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -315,32 +315,64 @@ def is_installed_by_name(app_fullname):
315315
def execute_script(script_source, is_file, classname, cwd=None, app_fullname=None):
316316
"""Run the script in the current thread. Returns True if successful."""
317317
import utime # for timing read and compile
318-
import lvgl as lv
319318
import mpos.ui
320319
import _thread
320+
import sys
321321
thread_id = _thread.get_ident()
322322
compile_name = 'script' if not is_file else script_source
323+
executed_name = compile_name
323324
print(f"Thread {thread_id}: executing script with cwd: {cwd}")
324325
try:
325-
if is_file:
326-
print(f"Thread {thread_id}: reading script from file {script_source}")
327-
with open(script_source, 'r') as f: # No need to check if it exists as exceptions are caught
328-
start_time = utime.ticks_ms()
329-
script_source = f.read()
330-
read_time = utime.ticks_diff(utime.ticks_ms(), start_time)
331-
print(f"execute_script: reading script_source took {read_time}ms")
332326
script_globals = {
333-
'lv': lv,
334-
'mpos': mpos,
335327
'__name__': "__main__", # in case the script wants this
336-
'__file__': compile_name
328+
'__file__': compile_name # useful for logger
337329
}
338330
print(f"Thread {thread_id}: starting script")
339-
import sys
340331
path_before = sys.path[:] # Make a copy, not a reference
341332
if cwd:
342333
sys.path.append(cwd)
343334
try:
335+
if is_file and script_source.endswith(".py"):
336+
mpy_source = script_source[:-3] + ".mpy"
337+
try:
338+
if os.stat(mpy_source)[0] & 0x8000:
339+
print(f"Thread {thread_id}: found precompiled script {mpy_source}")
340+
executed_name = mpy_source
341+
module_name = mpy_source.rsplit("/", 1)[-1][:-4]
342+
import builtins
343+
if module_name in sys.modules:
344+
del sys.modules[module_name]
345+
try:
346+
print(f"app_manager.py trying to __import__({module_name})")
347+
module = __import__(module_name)
348+
except Exception as e:
349+
print(f"app_manager.py failed to __import__({module_name}")
350+
module.__file__ = mpy_source
351+
module.__name__ = "__main__"
352+
main_activity = getattr(module, classname, None)
353+
if main_activity:
354+
from mpos.activity_navigator import ActivityNavigator
355+
from .intent import Intent
356+
start_time = utime.ticks_ms()
357+
ActivityNavigator.startActivity(Intent(activity_class=main_activity, app_fullname=app_fullname))
358+
end_time = utime.ticks_diff(utime.ticks_ms(), start_time)
359+
print(f"execute_script: ActivityNavigator.startActivity took {end_time}ms")
360+
return True
361+
raise Exception("could not find app's main_activity {} in {}".format(classname, mpy_source))
362+
except OSError:
363+
pass
364+
except Exception as e:
365+
print(f"WARNING: failed running precompiled script {mpy_source}: {e}")
366+
sys.print_exception(e)
367+
368+
if is_file:
369+
print(f"Thread {thread_id}: reading script from file {script_source}")
370+
with open(script_source, 'r') as f: # No need to check if it exists as exceptions are caught
371+
start_time = utime.ticks_ms()
372+
script_source = f.read()
373+
read_time = utime.ticks_diff(utime.ticks_ms(), start_time)
374+
print(f"execute_script: reading script_source took {read_time}ms")
375+
344376
start_time = utime.ticks_ms()
345377
compiled_script = compile(script_source, compile_name, 'exec')
346378
compile_time = utime.ticks_diff(utime.ticks_ms(), start_time)
@@ -375,7 +407,7 @@ def execute_script(script_source, is_file, classname, cwd=None, app_fullname=Non
375407
return False
376408
finally:
377409
# Always restore sys.path, even if we return early or raise an exception
378-
print(f"Thread {thread_id}: script {compile_name} finished, restoring sys.path from {sys.path} to {path_before}")
410+
print(f"Thread {thread_id}: script {executed_name} finished, restoring sys.path from {sys.path} to {path_before}")
379411
sys.path = path_before
380412
return True
381413
except Exception as e:

0 commit comments

Comments
 (0)