##// END OF EJS Templates
Create fallback with old arg_split, incase CommandLineToArgvW is missing.
Jörgen Stenarson -
Show More
@@ -1,176 +1,201 b''
1 """Windows-specific implementation of process utilities.
1 """Windows-specific implementation of process utilities.
2
2
3 This file is only meant to be imported by process.py, not by end-users.
3 This file is only meant to be imported by process.py, not by end-users.
4 """
4 """
5
5
6 #-----------------------------------------------------------------------------
6 #-----------------------------------------------------------------------------
7 # Copyright (C) 2010-2011 The IPython Development Team
7 # Copyright (C) 2010-2011 The IPython Development Team
8 #
8 #
9 # Distributed under the terms of the BSD License. The full license is in
9 # Distributed under the terms of the BSD License. The full license is in
10 # the file COPYING, distributed as part of this software.
10 # the file COPYING, distributed as part of this software.
11 #-----------------------------------------------------------------------------
11 #-----------------------------------------------------------------------------
12
12
13 #-----------------------------------------------------------------------------
13 #-----------------------------------------------------------------------------
14 # Imports
14 # Imports
15 #-----------------------------------------------------------------------------
15 #-----------------------------------------------------------------------------
16 from __future__ import print_function
16 from __future__ import print_function
17
17
18 # stdlib
18 # stdlib
19 import os
19 import os
20 import sys
20 import sys
21 import ctypes
21 import ctypes
22
22
23 from ctypes import c_int, POINTER
23 from ctypes import c_int, POINTER
24 from ctypes.wintypes import LPCWSTR, HLOCAL
24 from ctypes.wintypes import LPCWSTR, HLOCAL
25 from subprocess import STDOUT
25 from subprocess import STDOUT
26
26
27 # our own imports
27 # our own imports
28 from ._process_common import read_no_interrupt, process_handler
28 from ._process_common import read_no_interrupt, process_handler
29 from . import py3compat
29 from . import py3compat
30 from . import text
30 from . import text
31
31
32 #-----------------------------------------------------------------------------
32 #-----------------------------------------------------------------------------
33 # Function definitions
33 # Function definitions
34 #-----------------------------------------------------------------------------
34 #-----------------------------------------------------------------------------
35
35
36 class AvoidUNCPath(object):
36 class AvoidUNCPath(object):
37 """A context manager to protect command execution from UNC paths.
37 """A context manager to protect command execution from UNC paths.
38
38
39 In the Win32 API, commands can't be invoked with the cwd being a UNC path.
39 In the Win32 API, commands can't be invoked with the cwd being a UNC path.
40 This context manager temporarily changes directory to the 'C:' drive on
40 This context manager temporarily changes directory to the 'C:' drive on
41 entering, and restores the original working directory on exit.
41 entering, and restores the original working directory on exit.
42
42
43 The context manager returns the starting working directory *if* it made a
43 The context manager returns the starting working directory *if* it made a
44 change and None otherwise, so that users can apply the necessary adjustment
44 change and None otherwise, so that users can apply the necessary adjustment
45 to their system calls in the event of a change.
45 to their system calls in the event of a change.
46
46
47 Example
47 Example
48 -------
48 -------
49 ::
49 ::
50 cmd = 'dir'
50 cmd = 'dir'
51 with AvoidUNCPath() as path:
51 with AvoidUNCPath() as path:
52 if path is not None:
52 if path is not None:
53 cmd = '"pushd %s &&"%s' % (path, cmd)
53 cmd = '"pushd %s &&"%s' % (path, cmd)
54 os.system(cmd)
54 os.system(cmd)
55 """
55 """
56 def __enter__(self):
56 def __enter__(self):
57 self.path = os.getcwdu()
57 self.path = os.getcwdu()
58 self.is_unc_path = self.path.startswith(r"\\")
58 self.is_unc_path = self.path.startswith(r"\\")
59 if self.is_unc_path:
59 if self.is_unc_path:
60 # change to c drive (as cmd.exe cannot handle UNC addresses)
60 # change to c drive (as cmd.exe cannot handle UNC addresses)
61 os.chdir("C:")
61 os.chdir("C:")
62 return self.path
62 return self.path
63 else:
63 else:
64 # We return None to signal that there was no change in the working
64 # We return None to signal that there was no change in the working
65 # directory
65 # directory
66 return None
66 return None
67
67
68 def __exit__(self, exc_type, exc_value, traceback):
68 def __exit__(self, exc_type, exc_value, traceback):
69 if self.is_unc_path:
69 if self.is_unc_path:
70 os.chdir(self.path)
70 os.chdir(self.path)
71
71
72
72
73 def _find_cmd(cmd):
73 def _find_cmd(cmd):
74 """Find the full path to a .bat or .exe using the win32api module."""
74 """Find the full path to a .bat or .exe using the win32api module."""
75 try:
75 try:
76 from win32api import SearchPath
76 from win32api import SearchPath
77 except ImportError:
77 except ImportError:
78 raise ImportError('you need to have pywin32 installed for this to work')
78 raise ImportError('you need to have pywin32 installed for this to work')
79 else:
79 else:
80 PATH = os.environ['PATH']
80 PATH = os.environ['PATH']
81 extensions = ['.exe', '.com', '.bat', '.py']
81 extensions = ['.exe', '.com', '.bat', '.py']
82 path = None
82 path = None
83 for ext in extensions:
83 for ext in extensions:
84 try:
84 try:
85 path = SearchPath(PATH, cmd + ext)[0]
85 path = SearchPath(PATH, cmd + ext)[0]
86 except:
86 except:
87 pass
87 pass
88 if path is None:
88 if path is None:
89 raise OSError("command %r not found" % cmd)
89 raise OSError("command %r not found" % cmd)
90 else:
90 else:
91 return path
91 return path
92
92
93
93
94 def _system_body(p):
94 def _system_body(p):
95 """Callback for _system."""
95 """Callback for _system."""
96 enc = text.getdefaultencoding()
96 enc = text.getdefaultencoding()
97 for line in read_no_interrupt(p.stdout).splitlines():
97 for line in read_no_interrupt(p.stdout).splitlines():
98 line = line.decode(enc, 'replace')
98 line = line.decode(enc, 'replace')
99 print(line, file=sys.stdout)
99 print(line, file=sys.stdout)
100 for line in read_no_interrupt(p.stderr).splitlines():
100 for line in read_no_interrupt(p.stderr).splitlines():
101 line = line.decode(enc, 'replace')
101 line = line.decode(enc, 'replace')
102 print(line, file=sys.stderr)
102 print(line, file=sys.stderr)
103
103
104 # Wait to finish for returncode
104 # Wait to finish for returncode
105 return p.wait()
105 return p.wait()
106
106
107
107
108 def system(cmd):
108 def system(cmd):
109 """Win32 version of os.system() that works with network shares.
109 """Win32 version of os.system() that works with network shares.
110
110
111 Note that this implementation returns None, as meant for use in IPython.
111 Note that this implementation returns None, as meant for use in IPython.
112
112
113 Parameters
113 Parameters
114 ----------
114 ----------
115 cmd : str
115 cmd : str
116 A command to be executed in the system shell.
116 A command to be executed in the system shell.
117
117
118 Returns
118 Returns
119 -------
119 -------
120 None : we explicitly do NOT return the subprocess status code, as this
120 None : we explicitly do NOT return the subprocess status code, as this
121 utility is meant to be used extensively in IPython, where any return value
121 utility is meant to be used extensively in IPython, where any return value
122 would trigger :func:`sys.displayhook` calls.
122 would trigger :func:`sys.displayhook` calls.
123 """
123 """
124 with AvoidUNCPath() as path:
124 with AvoidUNCPath() as path:
125 if path is not None:
125 if path is not None:
126 cmd = '"pushd %s &&"%s' % (path, cmd)
126 cmd = '"pushd %s &&"%s' % (path, cmd)
127 return process_handler(cmd, _system_body)
127 return process_handler(cmd, _system_body)
128
128
129
129
130 def getoutput(cmd):
130 def getoutput(cmd):
131 """Return standard output of executing cmd in a shell.
131 """Return standard output of executing cmd in a shell.
132
132
133 Accepts the same arguments as os.system().
133 Accepts the same arguments as os.system().
134
134
135 Parameters
135 Parameters
136 ----------
136 ----------
137 cmd : str
137 cmd : str
138 A command to be executed in the system shell.
138 A command to be executed in the system shell.
139
139
140 Returns
140 Returns
141 -------
141 -------
142 stdout : str
142 stdout : str
143 """
143 """
144
144
145 with AvoidUNCPath() as path:
145 with AvoidUNCPath() as path:
146 if path is not None:
146 if path is not None:
147 cmd = '"pushd %s &&"%s' % (path, cmd)
147 cmd = '"pushd %s &&"%s' % (path, cmd)
148 out = process_handler(cmd, lambda p: p.communicate()[0], STDOUT)
148 out = process_handler(cmd, lambda p: p.communicate()[0], STDOUT)
149
149
150 if out is None:
150 if out is None:
151 out = ''
151 out = ''
152 return out
152 return out
153
153
154
154 try:
155 CommandLineToArgvW = ctypes.windll.shell32.CommandLineToArgvW
155 CommandLineToArgvW = ctypes.windll.shell32.CommandLineToArgvW
156 CommandLineToArgvW.arg_types = [LPCWSTR, POINTER(c_int)]
156 CommandLineToArgvW.arg_types = [LPCWSTR, POINTER(c_int)]
157 CommandLineToArgvW.res_types = [POINTER(LPCWSTR)]
157 CommandLineToArgvW.res_types = [POINTER(LPCWSTR)]
158 LocalFree = ctypes.windll.kernel32.LocalFree
158 LocalFree = ctypes.windll.kernel32.LocalFree
159 LocalFree.res_type = HLOCAL
159 LocalFree.res_type = HLOCAL
160 LocalFree.arg_types = [HLOCAL]
160 LocalFree.arg_types = [HLOCAL]
161
161
162 def arg_split(commandline, posix=False):
162 def arg_split(commandline, posix=False):
163 """Split a command line's arguments in a shell-like manner.
163 """Split a command line's arguments in a shell-like manner.
164
164
165 This is a special version for windows that use a ctypes call to CommandLineToArgvW
165 This is a special version for windows that use a ctypes call to CommandLineToArgvW
166 to do the argv splitting. The posix paramter is ignored.
166 to do the argv splitting. The posix paramter is ignored.
167 """
167 """
168 #CommandLineToArgvW returns path to executable if called with empty string.
168 #CommandLineToArgvW returns path to executable if called with empty string.
169 if commandline.strip() == "":
169 if commandline.strip() == "":
170 return []
170 return []
171 argvn = c_int()
171 argvn = c_int()
172 result_pointer = CommandLineToArgvW(py3compat.cast_unicode(commandline.lstrip()), ctypes.byref(argvn))
172 result_pointer = CommandLineToArgvW(py3compat.cast_unicode(commandline.lstrip()), ctypes.byref(argvn))
173 result_array_type = LPCWSTR * argvn.value
173 result_array_type = LPCWSTR * argvn.value
174 result = [arg for arg in result_array_type.from_address(result_pointer)]
174 result = [arg for arg in result_array_type.from_address(result_pointer)]
175 retval = LocalFree(result_pointer)
175 retval = LocalFree(result_pointer)
176 return result
176 return result
177 except AttributeError:
178 import shlex
179 #alternative if CommandLineToArgvW is not available
180 def arg_split(s, posix=False):
181 """Split a command line's arguments in a shell-like manner.
182
183 This is a modified version of the standard library's shlex.split()
184 function, but with a default of posix=False for splitting, so that quotes
185 in inputs are respected."""
186
187 # Unfortunately, python's shlex module is buggy with unicode input:
188 # http://bugs.python.org/issue1170
189 # At least encoding the input when it's unicode seems to help, but there
190 # may be more problems lurking. Apparently this is fixed in python3.
191 is_unicode = False
192 if (not py3compat.PY3) and isinstance(s, unicode):
193 is_unicode = True
194 s = s.encode('utf-8')
195 lex = shlex.shlex(s, posix=posix)
196 lex.whitespace_split = True
197 tokens = list(lex)
198 if is_unicode:
199 # Convert the tokens back to unicode.
200 tokens = [x.decode('utf-8') for x in tokens]
201 return tokens
General Comments 0
You need to be logged in to leave comments. Login now