-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlauncher_here.py
More file actions
46 lines (38 loc) · 1.24 KB
/
launcher_here.py
File metadata and controls
46 lines (38 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
"""Utilities for launching scripts."""
import os
import runpy
import sys
from pathlib import Path
from kivy import platform
from kivy.logger import Logger
def run_script(script: str):
"""Execute given script."""
Logger.info("PythonHere: Run script %s", script)
try:
path = Path(script).resolve(strict=True)
except FileNotFoundError:
Logger.error("Script not found: %s", script)
raise Exception(f"Script not found: {script}") from None
original_cwd = str(Path.cwd())
original_sys_path = sys.path[:]
try:
script_dir = path.parent
os.chdir(str(script_dir))
sys.path.insert(0, str(script_dir))
runpy.run_path(str(path), run_name="__main__")
finally:
os.chdir(original_cwd)
sys.path = original_sys_path
def try_startup_script():
"""Execute startup script, if it was passed to app."""
if platform != "android":
return
import android_here # pylint: disable=import-outside-toplevel
try:
android_here.bind_run_script_on_new_intent()
script = android_here.get_startup_script()
if script:
run_script(script)
except Exception:
Logger.exception("PythonHere: Error while starting script")
raise