##// END OF EJS Templates
initial test for the embedding
Paul Ivanov -
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 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 from ._process_common import getoutputerror, get_output_error_code
30 from ._process_common import getoutputerror, get_output_error_code, process_handler
31 31 from . 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`.
48 48
49 49 Warning, don't use this to find IPython command line programs as there
50 50 is a risk you will find the wrong one. Instead find those using the
51 51 following code and looking for the application itself::
52 52
53 53 from IPython.utils.path import get_ipython_module_path
54 54 from IPython.utils.process import pycmd2argv
55 55 argv = pycmd2argv(get_ipython_module_path('IPython.terminal.ipapp'))
56 56
57 57 Parameters
58 58 ----------
59 59 cmd : str
60 60 The command line program to look for.
61 61 """
62 62 try:
63 63 path = _find_cmd(cmd).rstrip()
64 64 except OSError:
65 65 raise FindCmdError('command could not be found: %s' % cmd)
66 66 # which returns empty if not found
67 67 if path == '':
68 68 raise FindCmdError('command could not be found: %s' % cmd)
69 69 return os.path.abspath(path)
70 70
71 71
72 72 def is_cmd_found(cmd):
73 73 """Check whether executable `cmd` exists or not and return a bool."""
74 74 try:
75 75 find_cmd(cmd)
76 76 return True
77 77 except FindCmdError:
78 78 return False
79 79
80 80
81 81 def pycmd2argv(cmd):
82 82 r"""Take the path of a python command and return a list (argv-style).
83 83
84 84 This only works on Python based command line programs and will find the
85 85 location of the ``python`` executable using ``sys.executable`` to make
86 86 sure the right version is used.
87 87
88 88 For a given path ``cmd``, this returns [cmd] if cmd's extension is .exe,
89 89 .com or .bat, and [, cmd] otherwise.
90 90
91 91 Parameters
92 92 ----------
93 93 cmd : string
94 94 The path of the command.
95 95
96 96 Returns
97 97 -------
98 98 argv-style list.
99 99 """
100 100 ext = os.path.splitext(cmd)[1]
101 101 if ext in ['.exe', '.com', '.bat']:
102 102 return [cmd]
103 103 else:
104 104 return [sys.executable, cmd]
105 105
106 106
107 107 def abbrev_cwd():
108 108 """ Return abbreviated version of cwd, e.g. d:mydir """
109 109 cwd = py3compat.getcwd().replace('\\','/')
110 110 drivepart = ''
111 111 tail = cwd
112 112 if sys.platform == 'win32':
113 113 if len(cwd) < 4:
114 114 return cwd
115 115 drivepart,tail = os.path.splitdrive(cwd)
116 116
117 117
118 118 parts = tail.split('/')
119 119 if len(parts) > 2:
120 120 tail = '/'.join(parts[-2:])
121 121
122 122 return (drivepart + (
123 123 cwd == '/' and '/' or tail))
General Comments 0
You need to be logged in to leave comments. Login now