| 1 |
# encoding: utf-8 |
| 2 |
""" |
| 3 |
Utilities for working with external processes. |
| 4 |
""" |
| 5 |
|
| 6 |
# Copyright (c) IPython Development Team. |
| 7 |
# Distributed under the terms of the Modified BSD License. |
| 8 |
|
| 9 |
|
| 10 |
import os |
| 11 |
import shutil |
| 12 |
import sys |
| 13 |
|
| 14 |
if sys.platform == 'win32': |
| 15 |
from ._process_win32 import system, getoutput, arg_split, check_pid |
| 16 |
elif sys.platform == 'cli': |
| 17 |
from ._process_cli import system, getoutput, arg_split, check_pid |
| 18 |
elif sys.platform == "emscripten": |
| 19 |
from ._process_emscripten import system, getoutput, arg_split, check_pid |
| 20 |
else: |
| 21 |
from ._process_posix import system, getoutput, arg_split, check_pid |
| 22 |
|
| 23 |
from ._process_common import getoutputerror, get_output_error_code, process_handler |
| 24 |
|
| 25 |
|
| 26 |
class FindCmdError(Exception): |
| 27 |
pass |
| 28 |
|
| 29 |
|
| 30 |
def find_cmd(cmd): |
| 31 |
"""Find absolute path to executable cmd in a cross platform manner. |
| 32 |
|
| 33 |
This function tries to determine the full path to a command line program |
| 34 |
using `which` on Unix/Linux/OS X and `win32api` on Windows. Most of the |
| 35 |
time it will use the version that is first on the users `PATH`. |
| 36 |
|
| 37 |
Warning, don't use this to find IPython command line programs as there |
| 38 |
is a risk you will find the wrong one. Instead find those using the |
| 39 |
following code and looking for the application itself:: |
| 40 |
|
| 41 |
import sys |
| 42 |
argv = [sys.executable, '-m', 'IPython'] |
| 43 |
|
| 44 |
Parameters |
| 45 |
---------- |
| 46 |
cmd : str |
| 47 |
The command line program to look for. |
| 48 |
""" |
| 49 |
path = shutil.which(cmd) |
| 50 |
if path is None: |
| 51 |
raise FindCmdError('command could not be found: %s' % cmd) |
| 52 |
return path |
| 53 |
|
| 54 |
|
| 55 |
def abbrev_cwd(): |
| 56 |
""" Return abbreviated version of cwd, e.g. d:mydir """ |
| 57 |
cwd = os.getcwd().replace('\\','/') |
| 58 |
drivepart = '' |
| 59 |
tail = cwd |
| 60 |
if sys.platform == 'win32': |
| 61 |
if len(cwd) < 4: |
| 62 |
return cwd |
| 63 |
drivepart,tail = os.path.splitdrive(cwd) |
| 64 |
|
| 65 |
|
| 66 |
parts = tail.split('/') |
| 67 |
if len(parts) > 2: |
| 68 |
tail = '/'.join(parts[-2:]) |
| 69 |
|
| 70 |
return (drivepart + ( |
| 71 |
cwd == '/' and '/' or tail)) |