stub_template_host.txt 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #!/usr/bin/env '%interpreter%'
  2. import os
  3. import tempfile
  4. import shutil
  5. import sys
  6. import subprocess
  7. import zipfile
  8. PYTHON_BINARY = '%interpreter%'
  9. MAIN_FILE = '%main%'
  10. PYTHON_PATH = 'PYTHONPATH'
  11. # Don't imply 'import site' on initialization
  12. PYTHON_ARG = '-S'
  13. def Main():
  14. args = sys.argv[1:]
  15. runfiles_path = tempfile.mkdtemp(prefix="Soong.python_")
  16. try:
  17. zf = zipfile.ZipFile(os.path.dirname(__file__))
  18. zf.extractall(runfiles_path)
  19. zf.close()
  20. new_python_path = runfiles_path
  21. old_python_path = os.environ.get(PYTHON_PATH)
  22. if old_python_path:
  23. os.environ.update({PYTHON_PATH: new_python_path + ":" + old_python_path})
  24. else:
  25. os.environ.update({PYTHON_PATH: new_python_path})
  26. # Now look for main python source file.
  27. main_filepath = os.path.join(runfiles_path, MAIN_FILE)
  28. assert os.path.exists(main_filepath), \
  29. 'Cannot exec() %r: file not found.' % main_filepath
  30. assert os.access(main_filepath, os.R_OK), \
  31. 'Cannot exec() %r: file not readable.' % main_filepath
  32. args = [PYTHON_BINARY, PYTHON_ARG, main_filepath] + args
  33. sys.stdout.flush()
  34. # close_fds=False so that you can run binaries with files provided on the command line:
  35. # my_python_app --file <(echo foo)
  36. sys.exit(subprocess.call(args, close_fds=False))
  37. finally:
  38. shutil.rmtree(runfiles_path, ignore_errors=True)
  39. if __name__ == '__main__':
  40. Main()