##// END OF EJS Templates
Merge pull request #6338 from aminb/cli-check_pid...
Min RK -
r17691:d1365d77 merge
parent child Browse files
Show More
@@ -1,63 +1,78 b''
1 """cli-specific implementation of process utilities.
1 """cli-specific implementation of process utilities.
2
2
3 cli - Common Language Infrastructure for IronPython. Code
3 cli - Common Language Infrastructure for IronPython. Code
4 can run on any operating system. Check os.name for os-
4 can run on any operating system. Check os.name for os-
5 specific settings.
5 specific settings.
6
6
7 This file is only meant to be imported by process.py, not by end-users.
7 This file is only meant to be imported by process.py, not by end-users.
8
8
9 This file is largely untested. To become a full drop-in process
9 This file is largely untested. To become a full drop-in process
10 interface for IronPython will probably require you to help fill
10 interface for IronPython will probably require you to help fill
11 in the details.
11 in the details.
12 """
12 """
13
13
14 # Import cli libraries:
14 # Import cli libraries:
15 import clr
15 import clr
16 import System
16 import System
17
17
18 # Import Python libraries:
18 # Import Python libraries:
19 import os
19 import os
20
20
21 # Import IPython libraries:
21 # Import IPython libraries:
22 from IPython.utils import py3compat
22 from IPython.utils import py3compat
23 from ._process_common import arg_split
23 from ._process_common import arg_split
24
24
25 def _find_cmd(cmd):
25 def _find_cmd(cmd):
26 """Find the full path to a command using which."""
26 """Find the full path to a command using which."""
27 paths = System.Environment.GetEnvironmentVariable("PATH").Split(os.pathsep)
27 paths = System.Environment.GetEnvironmentVariable("PATH").Split(os.pathsep)
28 for path in paths:
28 for path in paths:
29 filename = os.path.join(path, cmd)
29 filename = os.path.join(path, cmd)
30 if System.IO.File.Exists(filename):
30 if System.IO.File.Exists(filename):
31 return py3compat.bytes_to_str(filename)
31 return py3compat.bytes_to_str(filename)
32 raise OSError("command %r not found" % cmd)
32 raise OSError("command %r not found" % cmd)
33
33
34 def system(cmd):
34 def system(cmd):
35 """
35 """
36 system(cmd) should work in a cli environment on Mac OSX, Linux,
36 system(cmd) should work in a cli environment on Mac OSX, Linux,
37 and Windows
37 and Windows
38 """
38 """
39 psi = System.Diagnostics.ProcessStartInfo(cmd)
39 psi = System.Diagnostics.ProcessStartInfo(cmd)
40 psi.RedirectStandardOutput = True
40 psi.RedirectStandardOutput = True
41 psi.RedirectStandardError = True
41 psi.RedirectStandardError = True
42 psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal
42 psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal
43 psi.UseShellExecute = False
43 psi.UseShellExecute = False
44 # Start up process:
44 # Start up process:
45 reg = System.Diagnostics.Process.Start(psi)
45 reg = System.Diagnostics.Process.Start(psi)
46
46
47 def getoutput(cmd):
47 def getoutput(cmd):
48 """
48 """
49 getoutput(cmd) should work in a cli environment on Mac OSX, Linux,
49 getoutput(cmd) should work in a cli environment on Mac OSX, Linux,
50 and Windows
50 and Windows
51 """
51 """
52 psi = System.Diagnostics.ProcessStartInfo(cmd)
52 psi = System.Diagnostics.ProcessStartInfo(cmd)
53 psi.RedirectStandardOutput = True
53 psi.RedirectStandardOutput = True
54 psi.RedirectStandardError = True
54 psi.RedirectStandardError = True
55 psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal
55 psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal
56 psi.UseShellExecute = False
56 psi.UseShellExecute = False
57 # Start up process:
57 # Start up process:
58 reg = System.Diagnostics.Process.Start(psi)
58 reg = System.Diagnostics.Process.Start(psi)
59 myOutput = reg.StandardOutput
59 myOutput = reg.StandardOutput
60 output = myOutput.ReadToEnd()
60 output = myOutput.ReadToEnd()
61 myError = reg.StandardError
61 myError = reg.StandardError
62 error = myError.ReadToEnd()
62 error = myError.ReadToEnd()
63 return output
63 return output
64
65 def check_pid(pid):
66 """
67 Check if a process with the given PID (pid) exists
68 """
69 try:
70 System.Diagnostics.Process.GetProcessById(pid)
71 # process with given pid is running
72 return True
73 except System.InvalidOperationException:
74 # process wasn't started by this object (but is running)
75 return True
76 except System.ArgumentException:
77 # process with given pid isn't running
78 return False
@@ -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
21
22 # Our own
22 # Our own
23 if sys.platform == 'win32':
23 if sys.platform == 'win32':
24 from ._process_win32 import _find_cmd, system, getoutput, arg_split, check_pid
24 from ._process_win32 import _find_cmd, system, getoutput, arg_split, check_pid
25 elif sys.platform == 'cli':
25 elif sys.platform == 'cli':
26 from ._process_cli import _find_cmd, system, getoutput, arg_split
26 from ._process_cli import _find_cmd, system, getoutput, arg_split, check_pid
27 else:
27 else:
28 from ._process_posix import _find_cmd, system, getoutput, arg_split, check_pid
28 from ._process_posix import _find_cmd, system, getoutput, arg_split, check_pid
29
29
30 from ._process_common import getoutputerror, get_output_error_code, process_handler
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