1"""
2Utilities for working with external processes.
3"""
4
5# Copyright (c) IPython Development Team.
6# Distributed under the terms of the Modified BSD License.
7
8
9import os
10import sys
11
12if sys.platform == 'win32':
13 from ._process_win32 import system, getoutput, arg_split, check_pid
14elif sys.platform == "emscripten":
15 from ._process_emscripten import system, getoutput, arg_split, check_pid
16else:
17 from ._process_posix import system, getoutput, arg_split, check_pid
18
19from ._process_common import (
20 arg_split_with_quotes,
21 getoutputerror,
22 get_output_error_code,
23 process_handler,
24)
25
26
27class FindCmdError(Exception):
28 pass
29
30
31def find_cmd(cmd):
32 """Find absolute path to executable cmd in a cross platform manner.
33
34 This function tries to determine the full path to a command line program
35 using `which` on Unix/Linux/OS X and `win32api` on Windows. Most of the
36 time it will use the version that is first on the users `PATH`.
37
38 Warning, don't use this to find IPython command line programs as there
39 is a risk you will find the wrong one. Instead find those using the
40 following code and looking for the application itself::
41
42 import sys
43 argv = [sys.executable, '-m', 'IPython']
44
45 Parameters
46 ----------
47 cmd : str
48 The command line program to look for.
49 """
50 import shutil
51 path = shutil.which(cmd)
52 if path is None:
53 raise FindCmdError('command could not be found: %s' % cmd)
54 return path
55
56
57def abbrev_cwd():
58 """ Return abbreviated version of cwd, e.g. d:mydir """
59 cwd = os.getcwd().replace('\\','/')
60 drivepart = ''
61 tail = cwd
62 if sys.platform == 'win32':
63 if len(cwd) < 4:
64 return cwd
65 drivepart,tail = os.path.splitdrive(cwd)
66
67
68 parts = tail.split('/')
69 if len(parts) > 2:
70 tail = '/'.join(parts[-2:])
71
72 return (drivepart + (
73 cwd == '/' and '/' or tail))