js2c.py 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #!/usr/bin/env python
  2. import contextlib
  3. import glob
  4. import os
  5. import subprocess
  6. import sys
  7. SOURCE_ROOT = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
  8. TEMPLATE = """
  9. #ifndef ATOM_NATIVES_H_
  10. #define ATOM_NATIVES_H_
  11. namespace node {{
  12. {definitions}
  13. }} // namespace node
  14. #endif // ATOM_NATIVES_H_
  15. """
  16. def main():
  17. node_path = os.path.abspath(sys.argv[1])
  18. natives = os.path.abspath(sys.argv[2])
  19. js_source_files = glob.glob('{0}/*.js'.format(sys.argv[3]))
  20. call_js2c(node_path, natives, js_source_files)
  21. def call_js2c(node_path, natives, js_source_files):
  22. js2c = os.path.join(node_path, 'tools', 'js2c.py')
  23. src_dir = os.path.dirname(js_source_files[0])
  24. with scoped_cwd(src_dir):
  25. subprocess.check_call(
  26. [sys.executable, js2c, natives] +
  27. [os.path.basename(source) for source in js_source_files] +
  28. ['-t', TEMPLATE])
  29. @contextlib.contextmanager
  30. def scoped_cwd(path):
  31. cwd = os.getcwd()
  32. os.chdir(path)
  33. try:
  34. yield
  35. finally:
  36. os.chdir(cwd)
  37. if __name__ == '__main__':
  38. sys.exit(main())