blob: a48a86f51fae588ab074043b8b1426b3fd05d523 [file] [log] [blame]
Nan Zhangdb0b9a32017-02-27 10:12:13 -08001#!/usr/bin/env python
2
3import os
4import re
5import tempfile
6import shutil
7import sys
8import subprocess
9import zipfile
10
11PYTHON_BINARY = '%interpreter%'
12MAIN_FILE = '%main%'
13PYTHON_PATH = 'PYTHONPATH'
Nan Zhangdb0b9a32017-02-27 10:12:13 -080014
yangbill7265e5f2019-01-04 18:10:32 +080015# Don't imply 'import site' on initialization
16PYTHON_ARG = '-S'
17
Nan Zhangdb0b9a32017-02-27 10:12:13 -080018def SearchPathEnv(name):
19 search_path = os.getenv('PATH', os.defpath).split(os.pathsep)
20 for directory in search_path:
21 if directory == '': continue
22 path = os.path.join(directory, name)
Nan Zhangdb0b9a32017-02-27 10:12:13 -080023 # Check if path is actual executable file.
24 if os.path.isfile(path) and os.access(path, os.X_OK):
25 return path
26 return None
27
28def FindPythonBinary():
29 if PYTHON_BINARY.startswith('/'):
30 # Case 1: Python interpreter is directly provided with absolute path.
31 return PYTHON_BINARY
32 else:
33 # Case 2: Find Python interpreter through environment variable: PATH.
34 return SearchPathEnv(PYTHON_BINARY)
35
36# Create the runfiles tree by extracting the zip file
37def ExtractRunfiles():
38 temp_dir = tempfile.mkdtemp("", "Soong.python_")
39 zf = zipfile.ZipFile(os.path.dirname(__file__))
40 zf.extractall(temp_dir)
Nan Zhangbea09752018-05-31 12:49:33 -070041 return temp_dir
Nan Zhangdb0b9a32017-02-27 10:12:13 -080042
43def Main():
44 args = sys.argv[1:]
45
46 new_env = {}
Nicolas Geoffray6e1bf2b2018-10-11 15:18:42 +010047 runfiles_path = None
Nan Zhangdb0b9a32017-02-27 10:12:13 -080048
49 try:
50 runfiles_path = ExtractRunfiles()
51
52 # Add runfiles path to PYTHONPATH.
53 python_path_entries = [runfiles_path]
54
55 # Add top dirs within runfiles path to PYTHONPATH.
56 top_entries = [os.path.join(runfiles_path, i) for i in os.listdir(runfiles_path)]
57 top_pkg_dirs = [i for i in top_entries if os.path.isdir(i)]
58 python_path_entries += top_pkg_dirs
59
60 old_python_path = os.environ.get(PYTHON_PATH)
61 separator = ':'
62 new_python_path = separator.join(python_path_entries)
63
64 # Copy old PYTHONPATH.
65 if old_python_path:
66 new_python_path += separator + old_python_path
67 new_env[PYTHON_PATH] = new_python_path
68
69 # Now look for main python source file.
70 main_filepath = os.path.join(runfiles_path, MAIN_FILE)
71 assert os.path.exists(main_filepath), \
72 'Cannot exec() %r: file not found.' % main_filepath
73 assert os.access(main_filepath, os.R_OK), \
74 'Cannot exec() %r: file not readable.' % main_filepath
75
76 python_program = FindPythonBinary()
77 if python_program is None:
78 raise AssertionError('Could not find python binary: ' + PYTHON_BINARY)
yangbill7265e5f2019-01-04 18:10:32 +080079 args = [python_program, PYTHON_ARG, main_filepath] + args
Nan Zhangdb0b9a32017-02-27 10:12:13 -080080
81 os.environ.update(new_env)
82
83 sys.stdout.flush()
84 retCode = subprocess.call(args)
85 exit(retCode)
86 except:
87 raise
88 finally:
Nicolas Geoffray6e1bf2b2018-10-11 15:18:42 +010089 if runfiles_path is not None:
90 shutil.rmtree(runfiles_path, True)
Nan Zhangdb0b9a32017-02-27 10:12:13 -080091
92if __name__ == '__main__':
93 Main()