platform_methods.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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(
  32. (key, value)
  33. for key, value in env.items()
  34. if isinstance(value, JSON_SERIALIZABLE_TYPES)
  35. )
  36. # Save parameters
  37. args = (target, source, filtered_env)
  38. data = dict(fn=function_name, args=args)
  39. json_path = os.path.join(os.environ['TMP'], uuid.uuid4().hex + '.json')
  40. with open(json_path, 'wt') as json_file:
  41. json.dump(data, json_file, indent=2)
  42. json_file_size = os.stat(json_path).st_size
  43. print('Executing builder function in subprocess: '
  44. 'module_path=%r, parameter_file=%r, parameter_file_size=%r, target=%r, source=%r' % (
  45. module_path, json_path, json_file_size, target, source))
  46. try:
  47. exit_code = subprocess.call([sys.executable, module_path, json_path], env=subprocess_env)
  48. finally:
  49. try:
  50. os.remove(json_path)
  51. except (OSError, IOError) as e:
  52. # Do not fail the entire build if it cannot delete a temporary file
  53. print('WARNING: Could not delete temporary file: path=%r; [%s] %s' %
  54. (json_path, e.__class__.__name__, e))
  55. # Must succeed
  56. if exit_code:
  57. raise RuntimeError(
  58. 'Failed to run builder function in subprocess: module_path=%r; data=%r' % (module_path, data))
  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'])