process.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. import os
  8. import shutil
  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. elif sys.platform == "emscripten":
  15. from ._process_emscripten import system, getoutput, arg_split, check_pid
  16. else:
  17. from ._process_posix import system, getoutput, arg_split, check_pid
  18. from ._process_common import getoutputerror, get_output_error_code, process_handler
  19. class FindCmdError(Exception):
  20. pass
  21. def find_cmd(cmd):
  22. """Find absolute path to executable cmd in a cross platform manner.
  23. This function tries to determine the full path to a command line program
  24. using `which` on Unix/Linux/OS X and `win32api` on Windows. Most of the
  25. time it will use the version that is first on the users `PATH`.
  26. Warning, don't use this to find IPython command line programs as there
  27. is a risk you will find the wrong one. Instead find those using the
  28. following code and looking for the application itself::
  29. import sys
  30. argv = [sys.executable, '-m', 'IPython']
  31. Parameters
  32. ----------
  33. cmd : str
  34. The command line program to look for.
  35. """
  36. path = shutil.which(cmd)
  37. if path is None:
  38. raise FindCmdError('command could not be found: %s' % cmd)
  39. return path
  40. def abbrev_cwd():
  41. """ Return abbreviated version of cwd, e.g. d:mydir """
  42. cwd = os.getcwd().replace('\\','/')
  43. drivepart = ''
  44. tail = cwd
  45. if sys.platform == 'win32':
  46. if len(cwd) < 4:
  47. return cwd
  48. drivepart,tail = os.path.splitdrive(cwd)
  49. parts = tail.split('/')
  50. if len(parts) > 2:
  51. tail = '/'.join(parts[-2:])
  52. return (drivepart + (
  53. cwd == '/' and '/' or tail))