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