process.py 1.8 KB

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