platform_methods.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import os
  2. import sys
  3. import json
  4. import uuid
  5. import functools
  6. import subprocess
  7. # NOTE: The multiprocessing module is not compatible with SCons due to conflict on cPickle
  8. if sys.version_info[0] < 3:
  9. JSON_SERIALIZABLE_TYPES = (bool, int, long, float, basestring)
  10. else:
  11. JSON_SERIALIZABLE_TYPES = (bool, int, float, str)
  12. def run_in_subprocess(builder_function):
  13. @functools.wraps(builder_function)
  14. def wrapper(target, source, env):
  15. # Convert SCons Node instances to absolute paths
  16. target = [node.srcnode().abspath for node in target]
  17. source = [node.srcnode().abspath for node in source]
  18. # Short circuit on non-Windows platforms, no need to run in subprocess
  19. if sys.platform not in ("win32", "cygwin"):
  20. return builder_function(target, source, env)
  21. # Identify module
  22. module_name = builder_function.__module__
  23. function_name = builder_function.__name__
  24. module_path = sys.modules[module_name].__file__
  25. if module_path.endswith(".pyc") or module_path.endswith(".pyo"):
  26. module_path = module_path[:-1]
  27. # Subprocess environment
  28. subprocess_env = os.environ.copy()
  29. subprocess_env["PYTHONPATH"] = os.pathsep.join([os.getcwd()] + sys.path)
  30. # Keep only JSON serializable environment items
  31. filtered_env = dict((key, value) for key, value in env.items() if isinstance(value, JSON_SERIALIZABLE_TYPES))
  32. # Save parameters
  33. args = (target, source, filtered_env)
  34. data = dict(fn=function_name, args=args)
  35. json_path = os.path.join(os.environ["TMP"], uuid.uuid4().hex + ".json")
  36. with open(json_path, "wt") as json_file:
  37. json.dump(data, json_file, indent=2)
  38. json_file_size = os.stat(json_path).st_size
  39. print(
  40. "Executing builder function in subprocess: "
  41. "module_path=%r, parameter_file=%r, parameter_file_size=%r, target=%r, source=%r"
  42. % (module_path, json_path, json_file_size, target, source)
  43. )
  44. try:
  45. exit_code = subprocess.call([sys.executable, module_path, json_path], env=subprocess_env)
  46. finally:
  47. try:
  48. os.remove(json_path)
  49. except (OSError, IOError) as e:
  50. # Do not fail the entire build if it cannot delete a temporary file
  51. print(
  52. "WARNING: Could not delete temporary file: path=%r; [%s] %s" % (json_path, e.__class__.__name__, e)
  53. )
  54. # Must succeed
  55. if exit_code:
  56. raise RuntimeError(
  57. "Failed to run builder function in subprocess: module_path=%r; data=%r" % (module_path, data)
  58. )
  59. return wrapper
  60. def subprocess_main(namespace):
  61. with open(sys.argv[1]) as json_file:
  62. data = json.load(json_file)
  63. fn = namespace[data["fn"]]
  64. fn(*data["args"])