Home | History | Annotate | Download | only in scripts
      1 #!/usr/bin/env python
      2 
      3 import os
      4 import re
      5 import tempfile
      6 import shutil
      7 import sys
      8 import subprocess
      9 import zipfile
     10 
     11 PYTHON_BINARY = '%interpreter%'
     12 MAIN_FILE = '%main%'
     13 PYTHON_PATH = 'PYTHONPATH'
     14 ZIP_RUNFILES_DIRECTORY_NAME = 'runfiles'
     15 
     16 def SearchPathEnv(name):
     17   search_path = os.getenv('PATH', os.defpath).split(os.pathsep)
     18   for directory in search_path:
     19     if directory == '': continue
     20     path = os.path.join(directory, name)
     21     if os.path.islink(path):
     22       path = os.path.realpath(path)
     23     # 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 
     28 def 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
     37 def ExtractRunfiles():
     38   temp_dir = tempfile.mkdtemp("", "Soong.python_")
     39   zf = zipfile.ZipFile(os.path.dirname(__file__))
     40   zf.extractall(temp_dir)
     41   return os.path.join(temp_dir, ZIP_RUNFILES_DIRECTORY_NAME)
     42 
     43 def Main():
     44   args = sys.argv[1:]
     45 
     46   new_env = {}
     47 
     48   try:
     49     runfiles_path = ExtractRunfiles()
     50 
     51     # Add runfiles path to PYTHONPATH.
     52     python_path_entries = [runfiles_path]
     53 
     54     # Add top dirs within runfiles path to PYTHONPATH.
     55     top_entries = [os.path.join(runfiles_path, i) for i in os.listdir(runfiles_path)]
     56     top_pkg_dirs = [i for i in top_entries if os.path.isdir(i)]
     57     python_path_entries += top_pkg_dirs
     58 
     59     old_python_path = os.environ.get(PYTHON_PATH)
     60     separator = ':'
     61     new_python_path = separator.join(python_path_entries)
     62 
     63     # Copy old PYTHONPATH.
     64     if old_python_path:
     65       new_python_path += separator + old_python_path
     66     new_env[PYTHON_PATH] = new_python_path
     67 
     68     # Now look for main python source file.
     69     main_filepath = os.path.join(runfiles_path, MAIN_FILE)
     70     assert os.path.exists(main_filepath), \
     71            'Cannot exec() %r: file not found.' % main_filepath
     72     assert os.access(main_filepath, os.R_OK), \
     73            'Cannot exec() %r: file not readable.' % main_filepath
     74 
     75     python_program = FindPythonBinary()
     76     if python_program is None:
     77       raise AssertionError('Could not find python binary: ' + PYTHON_BINARY)
     78     args = [python_program, main_filepath] + args
     79 
     80     os.environ.update(new_env)
     81 
     82     sys.stdout.flush()
     83     retCode = subprocess.call(args)
     84     exit(retCode)
     85   except:
     86     raise
     87   finally:
     88     shutil.rmtree(os.path.dirname(runfiles_path), True)
     89 
     90 if __name__ == '__main__':
     91   Main()
     92