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 shutil
11import sys
12
13if sys.platform == 'win32':
14 from ._process_win32 import system, getoutput, arg_split, check_pid
15elif sys.platform == "emscripten":
16 from ._process_emscripten import system, getoutput, arg_split, check_pid
17else:
18 from ._process_posix import system, getoutput, arg_split, check_pid
19
20from ._process_common import (
21 arg_split_with_quotes,
22 getoutputerror,
23 get_output_error_code,
24 process_handler,
25)
26
27
28class FindCmdError(Exception):
29 pass
30
31
32def find_cmd(cmd):
33 """Find absolute path to executable cmd in a cross platform manner.
34
35 This function tries to determine the full path to a command line program
36 using `which` on Unix/Linux/OS X and `win32api` on Windows. Most of the
37 time it will use the version that is first on the users `PATH`.
38
39 Warning, don't use this to find IPython command line programs as there
40 is a risk you will find the wrong one. Instead find those using the
41 following code and looking for the application itself::
42
43 import sys
44 argv = [sys.executable, '-m', 'IPython']
45
46 Parameters
47 ----------
48 cmd : str
49 The command line program to look for.
50 """
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))