~ubuntu-branches/debian/experimental/ipython/experimental

« back to all changes in this revision

Viewing changes to IPython/utils/process.py

  • Committer: Bazaar Package Importer
  • Author(s): Julian Taylor
  • Date: 2011-05-10 16:18:45 UTC
  • mfrom: (1.2.14 upstream)
  • Revision ID: james.westby@ubuntu.com-20110510161845-0nrh795uiyp1a20r
Tags: 0.11~rc1-1
* New upstream release candidate
* change to source format 3.0 (quilt)
* run testsuite during build
* add clean target
* build-depends:
  - matplotlib, pymongo, xauth, xvfb and qt4 for tests
  - graphviz, sphinx for docs
* add dependencies and don't install embedded copy:
  - python-argparse(Closes: #555348)
  - python-configobj (Closes: #555339)
  - python-simplegeneric
  - python-decorator
  - python-pyparsing
  - pexpect
* update patches
  - disabled obsolete patches 03, 04 and 06
  - refresh default background color patch 07
  - add patch to fix testsuite
  - add patch to improve error message on missing ipython-qtconsole
* change to compat 7
* split out -parallel package
* split out -qtconsole package
* split out -doc package
* improve dependencies for each package
* add doc-base control file
* fix the ipythonX.Y startup scripts

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# encoding: utf-8
 
2
"""
 
3
Utilities for working with external processes.
 
4
"""
 
5
 
 
6
#-----------------------------------------------------------------------------
 
7
#  Copyright (C) 2008-2009  The IPython Development Team
 
8
#
 
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
#-----------------------------------------------------------------------------
 
12
 
 
13
#-----------------------------------------------------------------------------
 
14
# Imports
 
15
#-----------------------------------------------------------------------------
 
16
from __future__ import print_function
 
17
 
 
18
# Stdlib
 
19
import os
 
20
import sys
 
21
import shlex
 
22
 
 
23
# Our own
 
24
if sys.platform == 'win32':
 
25
    from ._process_win32 import _find_cmd, system, getoutput, AvoidUNCPath
 
26
else:
 
27
    from ._process_posix import _find_cmd, system, getoutput
 
28
 
 
29
from ._process_common import getoutputerror
 
30
 
 
31
#-----------------------------------------------------------------------------
 
32
# Code
 
33
#-----------------------------------------------------------------------------
 
34
 
 
35
 
 
36
class FindCmdError(Exception):
 
37
    pass
 
38
 
 
39
 
 
40
def find_cmd(cmd):
 
41
    """Find absolute path to executable cmd in a cross platform manner.
 
42
    
 
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`.
 
47
 
 
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::
 
51
    
 
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'))
 
55
 
 
56
    Parameters
 
57
    ----------
 
58
    cmd : str
 
59
        The command line program to look for.
 
60
    """
 
61
    if cmd == 'python':
 
62
        return os.path.abspath(sys.executable)
 
63
    try:
 
64
        path = _find_cmd(cmd).rstrip()
 
65
    except OSError:
 
66
        raise FindCmdError('command could not be found: %s' % cmd)
 
67
    # which returns empty if not found
 
68
    if path == '':
 
69
        raise FindCmdError('command could not be found: %s' % cmd)
 
70
    return os.path.abspath(path)
 
71
 
 
72
 
 
73
def pycmd2argv(cmd):
 
74
    r"""Take the path of a python command and return a list (argv-style).
 
75
 
 
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.
 
79
 
 
80
    For a given path ``cmd``, this returns [cmd] if cmd's extension is .exe,
 
81
    .com or .bat, and [, cmd] otherwise.  
 
82
 
 
83
    Parameters
 
84
    ----------
 
85
    cmd : string
 
86
      The path of the command.
 
87
 
 
88
    Returns
 
89
    -------
 
90
    argv-style list.
 
91
    """
 
92
    ext = os.path.splitext(cmd)[1]
 
93
    if ext in ['.exe', '.com', '.bat']:
 
94
        return [cmd]
 
95
    else:
 
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 
 
100
            # right python exe.
 
101
            return [sys.executable, '-u', cmd]
 
102
        else:
 
103
            return [sys.executable, cmd]
 
104
 
 
105
 
 
106
def arg_split(s, posix=False):
 
107
    """Split a command line's arguments in a shell-like manner.
 
108
 
 
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."""
 
112
 
 
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.
 
117
    is_unicode = False
 
118
    if isinstance(s, unicode):
 
119
        is_unicode = True
 
120
        s = s.encode('utf-8')
 
121
    lex = shlex.shlex(s, posix=posix)
 
122
    lex.whitespace_split = True
 
123
    tokens = list(lex)
 
124
    if is_unicode:
 
125
        # Convert the tokens back to unicode.
 
126
        tokens = [x.decode('utf-8') for x in tokens]
 
127
    return tokens
 
128
 
 
129
 
 
130
def abbrev_cwd():
 
131
    """ Return abbreviated version of cwd, e.g. d:mydir """
 
132
    cwd = os.getcwd().replace('\\','/')
 
133
    drivepart = ''
 
134
    tail = cwd
 
135
    if sys.platform == 'win32':
 
136
        if len(cwd) < 4:
 
137
            return cwd
 
138
        drivepart,tail = os.path.splitdrive(cwd)
 
139
 
 
140
 
 
141
    parts = tail.split('/')
 
142
    if len(parts) > 2:
 
143
        tail = '/'.join(parts[-2:])
 
144
 
 
145
    return (drivepart + (
 
146
        cwd == '/' and '/' or tail))