platform_methods.py 2.7 KB

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