Show More
@@ -0,0 +1,52 b'' | |||||
|
1 | """Test embedding of IPython""" | |||
|
2 | ||||
|
3 | #----------------------------------------------------------------------------- | |||
|
4 | # Copyright (C) 2013 The IPython Development Team | |||
|
5 | # | |||
|
6 | # Distributed under the terms of the BSD License. The full license is in | |||
|
7 | # the file COPYING, distributed as part of this software. | |||
|
8 | #----------------------------------------------------------------------------- | |||
|
9 | ||||
|
10 | #----------------------------------------------------------------------------- | |||
|
11 | # Imports | |||
|
12 | #----------------------------------------------------------------------------- | |||
|
13 | ||||
|
14 | import sys | |||
|
15 | import nose.tools as nt | |||
|
16 | from IPython.utils.process import process_handler | |||
|
17 | ||||
|
18 | #----------------------------------------------------------------------------- | |||
|
19 | # Tests | |||
|
20 | #----------------------------------------------------------------------------- | |||
|
21 | ||||
|
22 | _sample_embed = """ | |||
|
23 | from __future__ import print_function | |||
|
24 | import IPython | |||
|
25 | ||||
|
26 | a = 3 | |||
|
27 | b = 14 | |||
|
28 | print(a, '.', b) | |||
|
29 | ||||
|
30 | IPython.embed() | |||
|
31 | ||||
|
32 | print 'bye!' | |||
|
33 | """ | |||
|
34 | ||||
|
35 | def test_ipython_embed(): | |||
|
36 | """test that `ipython [subcommand] --help-all` works""" | |||
|
37 | #with TempFile as f: | |||
|
38 | # f.write("some embed case goes here") | |||
|
39 | ||||
|
40 | # run `python file_with_embed.py` | |||
|
41 | fname = '/home/pi/code/workspace/rambles/test_embed.py' | |||
|
42 | cmd = [sys.executable, fname] | |||
|
43 | ||||
|
44 | #p = Popen(cmd, stdout=PIPE, stderr=PIPE, stdin=PIPE) | |||
|
45 | print "\nhere's the command:", cmd | |||
|
46 | _, out, p = process_handler(cmd, | |||
|
47 | lambda p: (p.stdin.write("exit\r"), p.communicate()[:], p)) | |||
|
48 | print out[1] | |||
|
49 | nt.assert_equal(p.returncode, 0) | |||
|
50 | nt.assert_equal(p.returncode, 0) | |||
|
51 | #out, err, rc = tt.get_output_error_code(cmd) | |||
|
52 |
@@ -1,123 +1,123 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, get_output_error_code |
|
30 | from ._process_common import getoutputerror, get_output_error_code, process_handler | |
31 | from . import py3compat |
|
31 | from . 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`. |
|
47 | time it will use the version that is first on the users `PATH`. | |
48 |
|
48 | |||
49 | Warning, don't use this to find IPython command line programs as there |
|
49 | Warning, don't use this to find IPython command line programs as there | |
50 | is a risk you will find the wrong one. Instead find those using the |
|
50 | is a risk you will find the wrong one. Instead find those using the | |
51 | following code and looking for the application itself:: |
|
51 | following code and looking for the application itself:: | |
52 |
|
52 | |||
53 | from IPython.utils.path import get_ipython_module_path |
|
53 | from IPython.utils.path import get_ipython_module_path | |
54 | from IPython.utils.process import pycmd2argv |
|
54 | from IPython.utils.process import pycmd2argv | |
55 | argv = pycmd2argv(get_ipython_module_path('IPython.terminal.ipapp')) |
|
55 | argv = pycmd2argv(get_ipython_module_path('IPython.terminal.ipapp')) | |
56 |
|
56 | |||
57 | Parameters |
|
57 | Parameters | |
58 | ---------- |
|
58 | ---------- | |
59 | cmd : str |
|
59 | cmd : str | |
60 | The command line program to look for. |
|
60 | The command line program to look for. | |
61 | """ |
|
61 | """ | |
62 | try: |
|
62 | try: | |
63 | path = _find_cmd(cmd).rstrip() |
|
63 | path = _find_cmd(cmd).rstrip() | |
64 | except OSError: |
|
64 | except OSError: | |
65 | raise FindCmdError('command could not be found: %s' % cmd) |
|
65 | raise FindCmdError('command could not be found: %s' % cmd) | |
66 | # which returns empty if not found |
|
66 | # which returns empty if not found | |
67 | if path == '': |
|
67 | if path == '': | |
68 | raise FindCmdError('command could not be found: %s' % cmd) |
|
68 | raise FindCmdError('command could not be found: %s' % cmd) | |
69 | return os.path.abspath(path) |
|
69 | return os.path.abspath(path) | |
70 |
|
70 | |||
71 |
|
71 | |||
72 | def is_cmd_found(cmd): |
|
72 | def is_cmd_found(cmd): | |
73 | """Check whether executable `cmd` exists or not and return a bool.""" |
|
73 | """Check whether executable `cmd` exists or not and return a bool.""" | |
74 | try: |
|
74 | try: | |
75 | find_cmd(cmd) |
|
75 | find_cmd(cmd) | |
76 | return True |
|
76 | return True | |
77 | except FindCmdError: |
|
77 | except FindCmdError: | |
78 | return False |
|
78 | return False | |
79 |
|
79 | |||
80 |
|
80 | |||
81 | def pycmd2argv(cmd): |
|
81 | def pycmd2argv(cmd): | |
82 | r"""Take the path of a python command and return a list (argv-style). |
|
82 | r"""Take the path of a python command and return a list (argv-style). | |
83 |
|
83 | |||
84 | This only works on Python based command line programs and will find the |
|
84 | This only works on Python based command line programs and will find the | |
85 | location of the ``python`` executable using ``sys.executable`` to make |
|
85 | location of the ``python`` executable using ``sys.executable`` to make | |
86 | sure the right version is used. |
|
86 | sure the right version is used. | |
87 |
|
87 | |||
88 | For a given path ``cmd``, this returns [cmd] if cmd's extension is .exe, |
|
88 | For a given path ``cmd``, this returns [cmd] if cmd's extension is .exe, | |
89 | .com or .bat, and [, cmd] otherwise. |
|
89 | .com or .bat, and [, cmd] otherwise. | |
90 |
|
90 | |||
91 | Parameters |
|
91 | Parameters | |
92 | ---------- |
|
92 | ---------- | |
93 | cmd : string |
|
93 | cmd : string | |
94 | The path of the command. |
|
94 | The path of the command. | |
95 |
|
95 | |||
96 | Returns |
|
96 | Returns | |
97 | ------- |
|
97 | ------- | |
98 | argv-style list. |
|
98 | argv-style list. | |
99 | """ |
|
99 | """ | |
100 | ext = os.path.splitext(cmd)[1] |
|
100 | ext = os.path.splitext(cmd)[1] | |
101 | if ext in ['.exe', '.com', '.bat']: |
|
101 | if ext in ['.exe', '.com', '.bat']: | |
102 | return [cmd] |
|
102 | return [cmd] | |
103 | else: |
|
103 | else: | |
104 | return [sys.executable, cmd] |
|
104 | return [sys.executable, cmd] | |
105 |
|
105 | |||
106 |
|
106 | |||
107 | def abbrev_cwd(): |
|
107 | def abbrev_cwd(): | |
108 | """ Return abbreviated version of cwd, e.g. d:mydir """ |
|
108 | """ Return abbreviated version of cwd, e.g. d:mydir """ | |
109 | cwd = py3compat.getcwd().replace('\\','/') |
|
109 | cwd = py3compat.getcwd().replace('\\','/') | |
110 | drivepart = '' |
|
110 | drivepart = '' | |
111 | tail = cwd |
|
111 | tail = cwd | |
112 | if sys.platform == 'win32': |
|
112 | if sys.platform == 'win32': | |
113 | if len(cwd) < 4: |
|
113 | if len(cwd) < 4: | |
114 | return cwd |
|
114 | return cwd | |
115 | drivepart,tail = os.path.splitdrive(cwd) |
|
115 | drivepart,tail = os.path.splitdrive(cwd) | |
116 |
|
116 | |||
117 |
|
117 | |||
118 | parts = tail.split('/') |
|
118 | parts = tail.split('/') | |
119 | if len(parts) > 2: |
|
119 | if len(parts) > 2: | |
120 | tail = '/'.join(parts[-2:]) |
|
120 | tail = '/'.join(parts[-2:]) | |
121 |
|
121 | |||
122 | return (drivepart + ( |
|
122 | return (drivepart + ( | |
123 | cwd == '/' and '/' or tail)) |
|
123 | cwd == '/' and '/' or tail)) |
General Comments 0
You need to be logged in to leave comments.
Login now