_process_cli.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 IPython.utils import py3compat
  17. from ._process_common import arg_split
  18. def _find_cmd(cmd):
  19. """Find the full path to a command using which."""
  20. paths = System.Environment.GetEnvironmentVariable("PATH").Split(os.pathsep)
  21. for path in paths:
  22. filename = os.path.join(path, cmd)
  23. if System.IO.File.Exists(filename):
  24. return py3compat.bytes_to_str(filename)
  25. raise OSError("command %r not found" % cmd)
  26. def system(cmd):
  27. """
  28. system(cmd) should work in a cli environment on Mac OSX, Linux,
  29. and Windows
  30. """
  31. psi = System.Diagnostics.ProcessStartInfo(cmd)
  32. psi.RedirectStandardOutput = True
  33. psi.RedirectStandardError = True
  34. psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal
  35. psi.UseShellExecute = False
  36. # Start up process:
  37. reg = System.Diagnostics.Process.Start(psi)
  38. def getoutput(cmd):
  39. """
  40. getoutput(cmd) should work in a cli environment on Mac OSX, Linux,
  41. and Windows
  42. """
  43. psi = System.Diagnostics.ProcessStartInfo(cmd)
  44. psi.RedirectStandardOutput = True
  45. psi.RedirectStandardError = True
  46. psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal
  47. psi.UseShellExecute = False
  48. # Start up process:
  49. reg = System.Diagnostics.Process.Start(psi)
  50. myOutput = reg.StandardOutput
  51. output = myOutput.ReadToEnd()
  52. myError = reg.StandardError
  53. error = myError.ReadToEnd()
  54. return output
  55. def check_pid(pid):
  56. """
  57. Check if a process with the given PID (pid) exists
  58. """
  59. try:
  60. System.Diagnostics.Process.GetProcessById(pid)
  61. # process with given pid is running
  62. return True
  63. except System.InvalidOperationException:
  64. # process wasn't started by this object (but is running)
  65. return True
  66. except System.ArgumentException:
  67. # process with given pid isn't running
  68. return False