/DLR_Main/Languages/IronPython/IronPython/Lib/iptest/process_util.py

https://bitbucket.org/mdavid/dlr · Python · 147 lines · 106 code · 26 blank · 15 comment · 30 complexity · 08394b7569e6061130d8812dc8171ef2 MD5 · raw file

  1. #####################################################################################
  2. #
  3. # Copyright (c) Microsoft Corporation. All rights reserved.
  4. #
  5. # This source code is subject to terms and conditions of the Apache License, Version 2.0. A
  6. # copy of the license can be found in the License.html file at the root of this distribution. If
  7. # you cannot locate the Apache License, Version 2.0, please send an email to
  8. # ironpy@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
  9. # by the terms of the Apache License, Version 2.0.
  10. #
  11. # You must not remove this notice, or any other, from this software.
  12. #
  13. #
  14. #####################################################################################
  15. ## BE PLATFORM NETURAL
  16. import nt
  17. import sys
  18. from assert_util import testpath, is_cli
  19. one_arg_params = ("-X:Optimize", "-W", "-c", "-X:MaxRecursion", "-X:AssembliesDir")
  20. def launch(executable, *params):
  21. l = [ executable ] + list(params)
  22. return nt.spawnv(0, executable, l)
  23. def launch_ironpython(pyfile, *args):
  24. t = (pyfile, )
  25. for arg in args: t += (arg, )
  26. return launch(testpath.ipython_executable, *t)
  27. def launch_cpython(pyfile, *args):
  28. t = (pyfile, )
  29. for arg in args: t += (arg, )
  30. return launch(testpath.cpython_executable, *t)
  31. def launch_ironpython_with_extensions(pyfile, extensions, args):
  32. t = tuple(extensions)
  33. t += (pyfile, )
  34. for arg in args: t += (arg, )
  35. return launch(testpath.ipython_executable, *t)
  36. def _get_ip_testmode():
  37. import System
  38. lastConsumesNext = False
  39. switches = []
  40. for param in System.Environment.GetCommandLineArgs():
  41. if param.startswith('-T:') or param.startswith('-O:'):
  42. continue
  43. if param.startswith("-"):
  44. switches.append(param)
  45. if param in one_arg_params:
  46. lastConsumesNext = True
  47. else:
  48. if lastConsumesNext:
  49. switches.append(param)
  50. lastConsumesNext = False
  51. return switches
  52. def launch_ironpython_changing_extensions(test, add=[], remove=[], additionalScriptParams=()):
  53. final = _get_ip_testmode()
  54. for param in add:
  55. if param not in final: final.append(param)
  56. for param in remove:
  57. if param in final:
  58. pos = final.index(param)
  59. if pos != -1:
  60. if param in one_arg_params:
  61. del final[pos:pos+2]
  62. else :
  63. del final[pos]
  64. params = [sys.executable]
  65. params.extend(final)
  66. params.append(test)
  67. params.extend(additionalScriptParams)
  68. print "Starting process: %s" % params
  69. return nt.spawnv(0, sys.executable, params)
  70. def run_tool(cmd, args=""):
  71. import System
  72. process = System.Diagnostics.Process()
  73. process.StartInfo.FileName = cmd
  74. process.StartInfo.Arguments = args
  75. process.StartInfo.CreateNoWindow = True
  76. process.StartInfo.UseShellExecute = False
  77. process.StartInfo.RedirectStandardInput = False
  78. process.StartInfo.RedirectStandardOutput = False
  79. process.StartInfo.RedirectStandardError = False
  80. process.Start()
  81. process.WaitForExit()
  82. return process.ExitCode
  83. def has_csc():
  84. try: run_csc("/?")
  85. except WindowsError: return False
  86. else: return True
  87. def has_vbc():
  88. try: run_vbc("/?")
  89. except WindowsError: return False
  90. else: return True
  91. def has_ilasm():
  92. try: run_ilasm("/?")
  93. except WindowsError: return False
  94. else: return True
  95. def run_tlbimp(pathToTypeLib, outputName=None):
  96. if outputName:
  97. return run_tool("tlbimp.exe", pathToTypeLib+" /out:"+outputName)
  98. else:
  99. return run_tool("tlbimp.exe", pathToTypeLib)
  100. def run_register_com_component(pathToDll):
  101. return run_tool("regsvr32.exe", "/s "+pathToDll)
  102. def run_unregister_com_component(pathToDll):
  103. return run_tool("regsvr32.exe", "/s /u "+pathToDll)
  104. def run_csc(args):
  105. import file_util
  106. return run_tool(file_util.path_combine(get_clr_dir(),"csc.exe"), args)
  107. def run_vbc(args):
  108. import file_util
  109. return run_tool(file_util.path_combine(get_clr_dir(),"vbc.exe"), args)
  110. def run_ilasm(args):
  111. import file_util
  112. return run_tool(file_util.path_combine(get_clr_dir(),"ilasm.exe"), args)
  113. def number_of_process(arg):
  114. return len([x for x in nt.popen('tasklist.exe').readlines() if x.lower().startswith(arg.lower()) ])
  115. def kill_process(arg):
  116. return run_tool("taskkill.exe", '/F /IM %s' % arg)
  117. def get_clr_dir():
  118. import clr
  119. from System import Type
  120. from System.IO import Path
  121. return Path.GetDirectoryName(Type.GetType('System.Int32').Assembly.Location)