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