3
Utilities for working with external processes.
6
#-----------------------------------------------------------------------------
7
# Copyright (C) 2008-2009 The IPython Development Team
9
# Distributed under the terms of the BSD License. The full license is in
10
# the file COPYING, distributed as part of this software.
11
#-----------------------------------------------------------------------------
13
#-----------------------------------------------------------------------------
15
#-----------------------------------------------------------------------------
16
from __future__ import print_function
24
if sys.platform == 'win32':
25
from ._process_win32 import _find_cmd, system, getoutput, AvoidUNCPath
27
from ._process_posix import _find_cmd, system, getoutput
29
from ._process_common import getoutputerror
31
#-----------------------------------------------------------------------------
33
#-----------------------------------------------------------------------------
36
class FindCmdError(Exception):
41
"""Find absolute path to executable cmd in a cross platform manner.
43
This function tries to determine the full path to a command line program
44
using `which` on Unix/Linux/OS X and `win32api` on Windows. Most of the
45
time it will use the version that is first on the users `PATH`. If
46
cmd is `python` return `sys.executable`.
48
Warning, don't use this to find IPython command line programs as there
49
is a risk you will find the wrong one. Instead find those using the
50
following code and looking for the application itself::
52
from IPython.utils.path import get_ipython_module_path
53
from IPython.utils.process import pycmd2argv
54
argv = pycmd2argv(get_ipython_module_path('IPython.frontend.terminal.ipapp'))
59
The command line program to look for.
62
return os.path.abspath(sys.executable)
64
path = _find_cmd(cmd).rstrip()
66
raise FindCmdError('command could not be found: %s' % cmd)
67
# which returns empty if not found
69
raise FindCmdError('command could not be found: %s' % cmd)
70
return os.path.abspath(path)
74
r"""Take the path of a python command and return a list (argv-style).
76
This only works on Python based command line programs and will find the
77
location of the ``python`` executable using ``sys.executable`` to make
78
sure the right version is used.
80
For a given path ``cmd``, this returns [cmd] if cmd's extension is .exe,
81
.com or .bat, and [, cmd] otherwise.
86
The path of the command.
92
ext = os.path.splitext(cmd)[1]
93
if ext in ['.exe', '.com', '.bat']:
96
if sys.platform == 'win32':
97
# The -u option here turns on unbuffered output, which is required
98
# on Win32 to prevent wierd conflict and problems with Twisted.
99
# Also, use sys.executable to make sure we are picking up the
101
return [sys.executable, '-u', cmd]
103
return [sys.executable, cmd]
106
def arg_split(s, posix=False):
107
"""Split a command line's arguments in a shell-like manner.
109
This is a modified version of the standard library's shlex.split()
110
function, but with a default of posix=False for splitting, so that quotes
111
in inputs are respected."""
113
# Unfortunately, python's shlex module is buggy with unicode input:
114
# http://bugs.python.org/issue1170
115
# At least encoding the input when it's unicode seems to help, but there
116
# may be more problems lurking. Apparently this is fixed in python3.
118
if isinstance(s, unicode):
120
s = s.encode('utf-8')
121
lex = shlex.shlex(s, posix=posix)
122
lex.whitespace_split = True
125
# Convert the tokens back to unicode.
126
tokens = [x.decode('utf-8') for x in tokens]
131
""" Return abbreviated version of cwd, e.g. d:mydir """
132
cwd = os.getcwd().replace('\\','/')
135
if sys.platform == 'win32':
138
drivepart,tail = os.path.splitdrive(cwd)
141
parts = tail.split('/')
143
tail = '/'.join(parts[-2:])
145
return (drivepart + (
146
cwd == '/' and '/' or tail))