forked from emscripten-core/emscripten
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_selector.py
More file actions
43 lines (37 loc) · 1.31 KB
/
python_selector.py
File metadata and controls
43 lines (37 loc) · 1.31 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
'''
Provides a way to run a script on the preferred Python version
'''
import os
import sys
'''
Imports the target script by filename and calls run()
'''
def run_by_import(filename, main):
import importlib
return getattr(importlib.import_module(os.path.basename(filename)), main)()
'''
Opens Python 2 subprocess to run target script
'''
def run_by_subprocess(filename):
# Python on Windows does not provide `python2` but instead `py` that receives version parameter
py2 = ['py', '-2'] if sys.platform.startswith('win') else ['python2']
import subprocess
return subprocess.run(py2 + [os.path.realpath(filename) + '.py'] + sys.argv[1:]).returncode
def on_allowed_version():
major = sys.version_info.major
if major == 2:
return True
if os.environ.get('EMSCRIPTEN_ALLOW_NEWER_PYTHON'):
import logging
from tools import colored_logger
logging.warning('Running on Python %s which is not officially supported yet', major)
return True
return False
'''
Runs filename+'.py' by opening Python 2 subprocess if required, or by importing.
'''
def run(filename, profile=False, main="run"):
if profile:
from tools.toolchain_profiler import ToolchainProfiler
ToolchainProfiler.record_process_start()
sys.exit(run_by_import(filename, main) if on_allowed_version() else run_by_subprocess(filename))