_process_cli.py 2.0 KB

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