Show More
@@ -1,123 +1,119 b'' | |||||
1 | # encoding: utf-8 |
|
1 | # encoding: utf-8 | |
2 | """ |
|
2 | """ | |
3 | Utilities for working with external processes. |
|
3 | Utilities for working with external processes. | |
4 | """ |
|
4 | """ | |
5 |
|
5 | |||
6 | #----------------------------------------------------------------------------- |
|
6 | #----------------------------------------------------------------------------- | |
7 | # Copyright (C) 2008-2011 The IPython Development Team |
|
7 | # Copyright (C) 2008-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 shlex |
|
21 | import shlex | |
22 |
|
22 | |||
23 | # Our own |
|
23 | # Our own | |
24 | if sys.platform == 'win32': |
|
24 | if sys.platform == 'win32': | |
25 | from ._process_win32 import _find_cmd, system, getoutput, AvoidUNCPath, arg_split |
|
25 | from ._process_win32 import _find_cmd, system, getoutput, AvoidUNCPath, arg_split | |
26 | else: |
|
26 | else: | |
27 | from ._process_posix import _find_cmd, system, getoutput, arg_split |
|
27 | from ._process_posix import _find_cmd, system, getoutput, arg_split | |
28 |
|
28 | |||
29 |
|
29 | |||
30 | from ._process_common import getoutputerror |
|
30 | from ._process_common import getoutputerror | |
31 | from IPython.utils import py3compat |
|
31 | from IPython.utils import py3compat | |
32 |
|
32 | |||
33 | #----------------------------------------------------------------------------- |
|
33 | #----------------------------------------------------------------------------- | |
34 | # Code |
|
34 | # Code | |
35 | #----------------------------------------------------------------------------- |
|
35 | #----------------------------------------------------------------------------- | |
36 |
|
36 | |||
37 |
|
37 | |||
38 | class FindCmdError(Exception): |
|
38 | class FindCmdError(Exception): | |
39 | pass |
|
39 | pass | |
40 |
|
40 | |||
41 |
|
41 | |||
42 | def find_cmd(cmd): |
|
42 | def find_cmd(cmd): | |
43 | """Find absolute path to executable cmd in a cross platform manner. |
|
43 | """Find absolute path to executable cmd in a cross platform manner. | |
44 |
|
44 | |||
45 | This function tries to determine the full path to a command line program |
|
45 | This function tries to determine the full path to a command line program | |
46 | using `which` on Unix/Linux/OS X and `win32api` on Windows. Most of the |
|
46 | using `which` on Unix/Linux/OS X and `win32api` on Windows. Most of the | |
47 | time it will use the version that is first on the users `PATH`. If |
|
47 | time it will use the version that is first on the users `PATH`. If | |
48 | cmd is `python` return `sys.executable`. |
|
48 | cmd is `python` return `sys.executable`. | |
49 |
|
49 | |||
50 | Warning, don't use this to find IPython command line programs as there |
|
50 | Warning, don't use this to find IPython command line programs as there | |
51 | is a risk you will find the wrong one. Instead find those using the |
|
51 | is a risk you will find the wrong one. Instead find those using the | |
52 | following code and looking for the application itself:: |
|
52 | following code and looking for the application itself:: | |
53 |
|
53 | |||
54 | from IPython.utils.path import get_ipython_module_path |
|
54 | from IPython.utils.path import get_ipython_module_path | |
55 | from IPython.utils.process import pycmd2argv |
|
55 | from IPython.utils.process import pycmd2argv | |
56 | argv = pycmd2argv(get_ipython_module_path('IPython.frontend.terminal.ipapp')) |
|
56 | argv = pycmd2argv(get_ipython_module_path('IPython.frontend.terminal.ipapp')) | |
57 |
|
57 | |||
58 | Parameters |
|
58 | Parameters | |
59 | ---------- |
|
59 | ---------- | |
60 | cmd : str |
|
60 | cmd : str | |
61 | The command line program to look for. |
|
61 | The command line program to look for. | |
62 | """ |
|
62 | """ | |
63 | if cmd == 'python': |
|
63 | if cmd == 'python': | |
64 | return os.path.abspath(sys.executable) |
|
64 | return os.path.abspath(sys.executable) | |
65 | try: |
|
65 | try: | |
66 | path = _find_cmd(cmd).rstrip() |
|
66 | path = _find_cmd(cmd).rstrip() | |
67 | except OSError: |
|
67 | except OSError: | |
68 | raise FindCmdError('command could not be found: %s' % cmd) |
|
68 | raise FindCmdError('command could not be found: %s' % cmd) | |
69 | # which returns empty if not found |
|
69 | # which returns empty if not found | |
70 | if path == '': |
|
70 | if path == '': | |
71 | raise FindCmdError('command could not be found: %s' % cmd) |
|
71 | raise FindCmdError('command could not be found: %s' % cmd) | |
72 | return os.path.abspath(path) |
|
72 | return os.path.abspath(path) | |
73 |
|
73 | |||
74 |
|
74 | |||
75 | def pycmd2argv(cmd): |
|
75 | def pycmd2argv(cmd): | |
76 | r"""Take the path of a python command and return a list (argv-style). |
|
76 | r"""Take the path of a python command and return a list (argv-style). | |
77 |
|
77 | |||
78 | This only works on Python based command line programs and will find the |
|
78 | This only works on Python based command line programs and will find the | |
79 | location of the ``python`` executable using ``sys.executable`` to make |
|
79 | location of the ``python`` executable using ``sys.executable`` to make | |
80 | sure the right version is used. |
|
80 | sure the right version is used. | |
81 |
|
81 | |||
82 | For a given path ``cmd``, this returns [cmd] if cmd's extension is .exe, |
|
82 | For a given path ``cmd``, this returns [cmd] if cmd's extension is .exe, | |
83 | .com or .bat, and [, cmd] otherwise. |
|
83 | .com or .bat, and [, cmd] otherwise. | |
84 |
|
84 | |||
85 | Parameters |
|
85 | Parameters | |
86 | ---------- |
|
86 | ---------- | |
87 | cmd : string |
|
87 | cmd : string | |
88 | The path of the command. |
|
88 | The path of the command. | |
89 |
|
89 | |||
90 | Returns |
|
90 | Returns | |
91 | ------- |
|
91 | ------- | |
92 | argv-style list. |
|
92 | argv-style list. | |
93 | """ |
|
93 | """ | |
94 | ext = os.path.splitext(cmd)[1] |
|
94 | ext = os.path.splitext(cmd)[1] | |
95 | if ext in ['.exe', '.com', '.bat']: |
|
95 | if ext in ['.exe', '.com', '.bat']: | |
96 | return [cmd] |
|
96 | return [cmd] | |
97 | else: |
|
97 | else: | |
98 | if sys.platform == 'win32': |
|
98 | if sys.platform == 'win32': | |
99 | # The -u option here turns on unbuffered output, which is required |
|
99 | return [sys.executable, cmd] | |
100 | # on Win32 to prevent wierd conflict and problems with Twisted. |
|
|||
101 | # Also, use sys.executable to make sure we are picking up the |
|
|||
102 | # right python exe. |
|
|||
103 | return [sys.executable, '-u', cmd] |
|
|||
104 | else: |
|
100 | else: | |
105 | return [sys.executable, cmd] |
|
101 | return [sys.executable, cmd] | |
106 |
|
102 | |||
107 | def abbrev_cwd(): |
|
103 | def abbrev_cwd(): | |
108 | """ Return abbreviated version of cwd, e.g. d:mydir """ |
|
104 | """ Return abbreviated version of cwd, e.g. d:mydir """ | |
109 | cwd = os.getcwdu().replace('\\','/') |
|
105 | cwd = os.getcwdu().replace('\\','/') | |
110 | drivepart = '' |
|
106 | drivepart = '' | |
111 | tail = cwd |
|
107 | tail = cwd | |
112 | if sys.platform == 'win32': |
|
108 | if sys.platform == 'win32': | |
113 | if len(cwd) < 4: |
|
109 | if len(cwd) < 4: | |
114 | return cwd |
|
110 | return cwd | |
115 | drivepart,tail = os.path.splitdrive(cwd) |
|
111 | drivepart,tail = os.path.splitdrive(cwd) | |
116 |
|
112 | |||
117 |
|
113 | |||
118 | parts = tail.split('/') |
|
114 | parts = tail.split('/') | |
119 | if len(parts) > 2: |
|
115 | if len(parts) > 2: | |
120 | tail = '/'.join(parts[-2:]) |
|
116 | tail = '/'.join(parts[-2:]) | |
121 |
|
117 | |||
122 | return (drivepart + ( |
|
118 | return (drivepart + ( | |
123 | cwd == '/' and '/' or tail)) |
|
119 | cwd == '/' and '/' or tail)) |
General Comments 0
You need to be logged in to leave comments.
Login now