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