process.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. # encoding: utf-8
  2. """
  3. Utilities for working with external processes.
  4. """
  5. # Copyright (c) IPython Development Team.
  6. # Distributed under the terms of the Modified BSD License.
  7. from __future__ import print_function
  8. import os
  9. import sys
  10. if sys.platform == 'win32':
  11. from ._process_win32 import system, getoutput, arg_split, check_pid
  12. elif sys.platform == 'cli':
  13. from ._process_cli import system, getoutput, arg_split, check_pid
  14. else:
  15. from ._process_posix import system, getoutput, arg_split, check_pid
  16. from ._process_common import getoutputerror, get_output_error_code, process_handler
  17. from . import py3compat
  18. class FindCmdError(Exception):
  19. pass
  20. def find_cmd(cmd):
  21. """Find absolute path to executable cmd in a cross platform manner.
  22. This function tries to determine the full path to a command line program
  23. using `which` on Unix/Linux/OS X and `win32api` on Windows. Most of the
  24. time it will use the version that is first on the users `PATH`.
  25. Warning, don't use this to find IPython command line programs as there
  26. is a risk you will find the wrong one. Instead find those using the
  27. following code and looking for the application itself::
  28. from IPython.utils.path import get_ipython_module_path
  29. from IPython.utils.process import pycmd2argv
  30. argv = pycmd2argv(get_ipython_module_path('IPython.terminal.ipapp'))
  31. Parameters
  32. ----------
  33. cmd : str
  34. The command line program to look for.
  35. """
  36. path = py3compat.which(cmd)
  37. if path is None:
  38. raise FindCmdError('command could not be found: %s' % cmd)
  39. return path
  40. def is_cmd_found(cmd):
  41. """Check whether executable `cmd` exists or not and return a bool."""
  42. try:
  43. find_cmd(cmd)
  44. return True
  45. except FindCmdError:
  46. return False
  47. def pycmd2argv(cmd):
  48. r"""Take the path of a python command and return a list (argv-style).
  49. This only works on Python based command line programs and will find the
  50. location of the ``python`` executable using ``sys.executable`` to make
  51. sure the right version is used.
  52. For a given path ``cmd``, this returns [cmd] if cmd's extension is .exe,
  53. .com or .bat, and [, cmd] otherwise.
  54. Parameters
  55. ----------
  56. cmd : string
  57. The path of the command.
  58. Returns
  59. -------
  60. argv-style list.
  61. """
  62. ext = os.path.splitext(cmd)[1]
  63. if ext in ['.exe', '.com', '.bat']:
  64. return [cmd]
  65. else:
  66. return [sys.executable, cmd]
  67. def abbrev_cwd():
  68. """ Return abbreviated version of cwd, e.g. d:mydir """
  69. cwd = py3compat.getcwd().replace('\\','/')
  70. drivepart = ''
  71. tail = cwd
  72. if sys.platform == 'win32':
  73. if len(cwd) < 4:
  74. return cwd
  75. drivepart,tail = os.path.splitdrive(cwd)
  76. parts = tail.split('/')
  77. if len(parts) > 2:
  78. tail = '/'.join(parts[-2:])
  79. return (drivepart + (
  80. cwd == '/' and '/' or tail))