jlpmapp.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. # coding: utf-8
  2. """A Jupyter-aware wrapper for the yarn package manager"""
  3. # Copyright (c) Jupyter Development Team.
  4. # Distributed under the terms of the Modified BSD License.
  5. import sys
  6. import os
  7. from ipython_genutils.py3compat import which as _which
  8. try:
  9. import subprocess32 as subprocess
  10. except ImportError:
  11. import subprocess
  12. HERE = os.path.dirname(os.path.abspath(__file__))
  13. YARN_PATH = os.path.join(HERE, 'yarn.js')
  14. def execvp(cmd, argv):
  15. """Execvp, except on Windows where it uses Popen.
  16. The first argument, by convention, should point to the filename
  17. associated with the file being executed.
  18. Python provides execvp on Windows, but its behavior is problematic
  19. (Python bug#9148).
  20. """
  21. cmd = which(cmd)
  22. if os.name == 'nt':
  23. import signal
  24. import sys
  25. p = subprocess.Popen([cmd] + argv[1:])
  26. # Don't raise KeyboardInterrupt in the parent process.
  27. # Set this after spawning, to avoid subprocess inheriting handler.
  28. signal.signal(signal.SIGINT, signal.SIG_IGN)
  29. p.wait()
  30. sys.exit(p.returncode)
  31. else:
  32. os.execvp(cmd, argv)
  33. def which(command, env=None):
  34. """Get the full path to a command.
  35. Parameters
  36. ----------
  37. command: str
  38. The command name or path.
  39. env: dict, optional
  40. The environment variables, defaults to `os.environ`.
  41. """
  42. env = env or os.environ
  43. path = env.get('PATH') or os.defpath
  44. command_with_path = _which(command, path=path)
  45. if command_with_path is None:
  46. if command == 'node':
  47. msg = 'Please install nodejs using conda or the nodejs website'
  48. raise ValueError(msg)
  49. raise ValueError('The command was not found or was not ' +
  50. 'executable: %s.' % command)
  51. return command_with_path
  52. def main(argv=None):
  53. """Run node and return the result.
  54. """
  55. # Make sure node is available.
  56. argv = argv or sys.argv[1:]
  57. execvp('node', ['node', YARN_PATH] + argv)