| 1 |
"""cli-specific implementation of process utilities. |
| 2 |
|
| 3 |
cli - Common Language Infrastructure for IronPython. Code |
| 4 |
can run on any operating system. Check os.name for os- |
| 5 |
specific settings. |
| 6 |
|
| 7 |
This file is only meant to be imported by process.py, not by end-users. |
| 8 |
|
| 9 |
This file is largely untested. To become a full drop-in process |
| 10 |
interface for IronPython will probably require you to help fill |
| 11 |
in the details. |
| 12 |
""" |
| 13 |
|
| 14 |
# Import cli libraries: |
| 15 |
import clr |
| 16 |
import System |
| 17 |
|
| 18 |
# Import Python libraries: |
| 19 |
import os |
| 20 |
|
| 21 |
# Import IPython libraries: |
| 22 |
from ._process_common import arg_split |
| 23 |
|
| 24 |
|
| 25 |
def system(cmd): |
| 26 |
""" |
| 27 |
system(cmd) should work in a cli environment on Mac OSX, Linux, |
| 28 |
and Windows |
| 29 |
""" |
| 30 |
psi = System.Diagnostics.ProcessStartInfo(cmd) |
| 31 |
psi.RedirectStandardOutput = True |
| 32 |
psi.RedirectStandardError = True |
| 33 |
psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal |
| 34 |
psi.UseShellExecute = False |
| 35 |
# Start up process: |
| 36 |
reg = System.Diagnostics.Process.Start(psi) |
| 37 |
|
| 38 |
|
| 39 |
def getoutput(cmd): |
| 40 |
""" |
| 41 |
getoutput(cmd) should work in a cli environment on Mac OSX, Linux, |
| 42 |
and Windows |
| 43 |
""" |
| 44 |
psi = System.Diagnostics.ProcessStartInfo(cmd) |
| 45 |
psi.RedirectStandardOutput = True |
| 46 |
psi.RedirectStandardError = True |
| 47 |
psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal |
| 48 |
psi.UseShellExecute = False |
| 49 |
# Start up process: |
| 50 |
reg = System.Diagnostics.Process.Start(psi) |
| 51 |
myOutput = reg.StandardOutput |
| 52 |
output = myOutput.ReadToEnd() |
| 53 |
myError = reg.StandardError |
| 54 |
error = myError.ReadToEnd() |
| 55 |
return output |
| 56 |
|
| 57 |
|
| 58 |
def check_pid(pid): |
| 59 |
""" |
| 60 |
Check if a process with the given PID (pid) exists |
| 61 |
""" |
| 62 |
try: |
| 63 |
System.Diagnostics.Process.GetProcessById(pid) |
| 64 |
# process with given pid is running |
| 65 |
return True |
| 66 |
except System.InvalidOperationException: |
| 67 |
# process wasn't started by this object (but is running) |
| 68 |
return True |
| 69 |
except System.ArgumentException: |
| 70 |
# process with given pid isn't running |
| 71 |
return False |