aboutsummaryrefslogtreecommitdiffstats
path: root/python/scripts
diff options
context:
space:
mode:
authorNan Zhang <nanzhang@google.com>2017-02-27 10:12:13 -0800
committerNan Zhang <nanzhang@google.com>2017-05-05 13:27:56 -0700
commitdb0b9a3cf3c9965929c988f1292f892bfc5deec5 (patch)
treefe145e5c2d864983915eaf98e1f8e4648e40c355 /python/scripts
parent7c34c4c8eba1ec0d5b8cf50926e40aee3291604e (diff)
downloadbuild_soong-db0b9a3cf3c9965929c988f1292f892bfc5deec5.tar.gz
build_soong-db0b9a3cf3c9965929c988f1292f892bfc5deec5.tar.bz2
build_soong-db0b9a3cf3c9965929c988f1292f892bfc5deec5.zip
Supported python build in host side.
The base module handles all the common functionalites, such as version compatibilty check, version variations split, source file format check, source/data file duplicate check. The library/binary module focuses on how to generate binary build actions, such as setting up stub script, zipping, filling in __init__.py in runfiles dir tree. Bug: b/31676493 Test: go test under python package Change-Id: I06608369f350f7195873d459e1c8d1bdb811e77e
Diffstat (limited to 'python/scripts')
-rw-r--r--python/scripts/stub_template_host.txt91
1 files changed, 91 insertions, 0 deletions
diff --git a/python/scripts/stub_template_host.txt b/python/scripts/stub_template_host.txt
new file mode 100644
index 00000000..b90a28b5
--- /dev/null
+++ b/python/scripts/stub_template_host.txt
@@ -0,0 +1,91 @@
+#!/usr/bin/env python
+
+import os
+import re
+import tempfile
+import shutil
+import sys
+import subprocess
+import zipfile
+
+PYTHON_BINARY = '%interpreter%'
+MAIN_FILE = '%main%'
+PYTHON_PATH = 'PYTHONPATH'
+ZIP_RUNFILES_DIRECTORY_NAME = 'runfiles'
+
+def SearchPathEnv(name):
+ search_path = os.getenv('PATH', os.defpath).split(os.pathsep)
+ for directory in search_path:
+ if directory == '': continue
+ path = os.path.join(directory, name)
+ if os.path.islink(path):
+ path = os.path.realpath(path)
+ # Check if path is actual executable file.
+ if os.path.isfile(path) and os.access(path, os.X_OK):
+ return path
+ return None
+
+def FindPythonBinary():
+ if PYTHON_BINARY.startswith('/'):
+ # Case 1: Python interpreter is directly provided with absolute path.
+ return PYTHON_BINARY
+ else:
+ # Case 2: Find Python interpreter through environment variable: PATH.
+ return SearchPathEnv(PYTHON_BINARY)
+
+# Create the runfiles tree by extracting the zip file
+def ExtractRunfiles():
+ temp_dir = tempfile.mkdtemp("", "Soong.python_")
+ zf = zipfile.ZipFile(os.path.dirname(__file__))
+ zf.extractall(temp_dir)
+ return os.path.join(temp_dir, ZIP_RUNFILES_DIRECTORY_NAME)
+
+def Main():
+ args = sys.argv[1:]
+
+ new_env = {}
+
+ try:
+ runfiles_path = ExtractRunfiles()
+
+ # Add runfiles path to PYTHONPATH.
+ python_path_entries = [runfiles_path]
+
+ # Add top dirs within runfiles path to PYTHONPATH.
+ top_entries = [os.path.join(runfiles_path, i) for i in os.listdir(runfiles_path)]
+ top_pkg_dirs = [i for i in top_entries if os.path.isdir(i)]
+ python_path_entries += top_pkg_dirs
+
+ old_python_path = os.environ.get(PYTHON_PATH)
+ separator = ':'
+ new_python_path = separator.join(python_path_entries)
+
+ # Copy old PYTHONPATH.
+ if old_python_path:
+ new_python_path += separator + old_python_path
+ new_env[PYTHON_PATH] = new_python_path
+
+ # Now look for main python source file.
+ main_filepath = os.path.join(runfiles_path, MAIN_FILE)
+ assert os.path.exists(main_filepath), \
+ 'Cannot exec() %r: file not found.' % main_filepath
+ assert os.access(main_filepath, os.R_OK), \
+ 'Cannot exec() %r: file not readable.' % main_filepath
+
+ python_program = FindPythonBinary()
+ if python_program is None:
+ raise AssertionError('Could not find python binary: ' + PYTHON_BINARY)
+ args = [python_program, main_filepath] + args
+
+ os.environ.update(new_env)
+
+ sys.stdout.flush()
+ retCode = subprocess.call(args)
+ exit(retCode)
+ except:
+ raise
+ finally:
+ shutil.rmtree(os.path.dirname(runfiles_path), True)
+
+if __name__ == '__main__':
+ Main()