tool_executor_helper.rb 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. require 'constants' # for Verbosity enumeration & $stderr redirect enumeration
  2. class ToolExecutorHelper
  3. constructor :streaminator, :system_utils, :system_wrapper
  4. def stderr_redirection(tool_config, logging)
  5. # if there's no logging enabled, return :stderr_redirect unmodified
  6. return tool_config[:stderr_redirect] if (not logging)
  7. # if there is logging enabled but the redirect is a custom value (not enum), return the custom string
  8. return tool_config[:stderr_redirect] if (tool_config[:stderr_redirect].class == String)
  9. # if logging is enabled but there's no custom string, return the AUTO enumeration so $stderr goes into the log
  10. return StdErrRedirect::AUTO
  11. end
  12. def background_exec_cmdline_prepend(tool_config)
  13. return nil if (tool_config[:background_exec].nil?)
  14. config_exec = tool_config[:background_exec]
  15. if ((config_exec == BackgroundExec::AUTO) and (@system_wrapper.windows?))
  16. return 'start'
  17. end
  18. if (config_exec == BackgroundExec::WIN)
  19. return 'start'
  20. end
  21. return nil
  22. end
  23. def osify_path_separators(executable)
  24. return executable.gsub(/\//, '\\') if (@system_wrapper.windows?)
  25. return executable
  26. end
  27. def stderr_redirect_cmdline_append(tool_config)
  28. return nil if (tool_config[:stderr_redirect].nil?)
  29. config_redirect = tool_config[:stderr_redirect]
  30. redirect = StdErrRedirect::NONE
  31. if (config_redirect == StdErrRedirect::AUTO)
  32. if (@system_wrapper.windows?)
  33. redirect = StdErrRedirect::WIN
  34. else
  35. if (@system_utils.tcsh_shell?)
  36. redirect = StdErrRedirect::TCSH
  37. else
  38. redirect = StdErrRedirect::UNIX
  39. end
  40. end
  41. end
  42. case redirect
  43. # we may need more complicated processing after some learning with various environments
  44. when StdErrRedirect::NONE then nil
  45. when StdErrRedirect::WIN then '2>&1'
  46. when StdErrRedirect::UNIX then '2>&1'
  47. when StdErrRedirect::TCSH then '|&'
  48. else redirect.to_s
  49. end
  50. end
  51. def background_exec_cmdline_append(tool_config)
  52. return nil if (tool_config[:background_exec].nil?)
  53. config_exec = tool_config[:background_exec]
  54. # if :auto & windows, then we already prepended 'start' and should append nothing
  55. return nil if ((config_exec == BackgroundExec::AUTO) and (@system_wrapper.windows?))
  56. # if :auto & not windows, then we append standard '&'
  57. return '&' if ((config_exec == BackgroundExec::AUTO) and (not @system_wrapper.windows?))
  58. # if explicitly Unix, then append '&'
  59. return '&' if (config_exec == BackgroundExec::UNIX)
  60. # all other cases, including :none, :win, & anything unrecognized, append nothing
  61. return nil
  62. end
  63. # if command succeeded and we have verbosity cranked up, spill our guts
  64. def print_happy_results(command_str, shell_result, boom=true)
  65. if ((shell_result[:exit_code] == 0) or ((shell_result[:exit_code] != 0) and not boom))
  66. output = "> Shell executed command:\n"
  67. output += "#{command_str}\n"
  68. output += "> Produced output:\n" if (not shell_result[:output].empty?)
  69. output += "#{shell_result[:output].strip}\n" if (not shell_result[:output].empty?)
  70. output += "> And exited with status: [#{shell_result[:exit_code]}].\n" if (shell_result[:exit_code] != 0)
  71. output += "\n"
  72. @streaminator.stdout_puts(output, Verbosity::OBNOXIOUS)
  73. end
  74. end
  75. # if command failed and we have verbosity set to minimum error level, spill our guts
  76. def print_error_results(command_str, shell_result, boom=true)
  77. if ((shell_result[:exit_code] != 0) and boom)
  78. output = "ERROR: Shell command failed.\n"
  79. output += "> Shell executed command:\n"
  80. output += "'#{command_str}'\n"
  81. output += "> Produced output:\n" if (not shell_result[:output].empty?)
  82. output += "#{shell_result[:output].strip}\n" if (not shell_result[:output].empty?)
  83. output += "> And exited with status: [#{shell_result[:exit_code]}].\n" if (shell_result[:exit_code] != nil)
  84. output += "> And then likely crashed.\n" if (shell_result[:exit_code] == nil)
  85. output += "\n"
  86. @streaminator.stderr_puts(output, Verbosity::ERRORS)
  87. end
  88. end
  89. end