shell.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. '''
  2. /*#############################################################################
  3. HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. ############################################################################ */
  14. '''
  15. import logging
  16. import sys
  17. from subprocess import (
  18. PIPE,
  19. Popen,
  20. CalledProcessError
  21. )
  22. from ..common.error import Error
  23. #TODO: Find a better way since which is tempramental.
  24. CMD = {
  25. "eclcc": "/usr/bin/eclcc",
  26. "ecl": "/usr/bin/ecl",
  27. "configgen": "/opt/HPCCSystems/sbin/configgen"
  28. }
  29. class Shell:
  30. def command(self, *command_args):
  31. def __command(*args):
  32. all_args = command_args + args
  33. return self.__run(*all_args)
  34. return __command
  35. def __hidePassw(self, item):
  36. if '--password' in item:
  37. return '--password=********'
  38. else:
  39. return item
  40. def __run(self, *args, **kwargs):
  41. _args = [i for i in args if i is not None]
  42. argsLog = [self.__hidePassw(i) for i in args if i is not None ]
  43. logging.debug("Shell _run CMD: " + " ". join(argsLog))
  44. process = Popen(
  45. _args, stdout = PIPE, stderr = PIPE, close_fds = True, **kwargs)
  46. stdout, stderr = process.communicate()
  47. retCode = process.returncode
  48. logging.debug("Shell _run retCode: %d", retCode)
  49. logging.debug(" stdout:'%s'", stdout)
  50. logging.debug(" stderr:'%s'", stderr)
  51. if retCode or ((len(stderr) > 0) and ('Error' in stderr)):
  52. exception = CalledProcessError(
  53. process.returncode, repr(args))
  54. exception.output = ''.join(filter(None, [stdout, stderr]))
  55. logging.debug("exception.output:'%s'", exception.output)
  56. raise Error('1001', err=repr(exception.output))
  57. return stdout
  58. # Currently hacked to use the CMD dict as which can be tempramental.
  59. # - What other methods can be used?
  60. # - Support multiple versions of eclcc?
  61. def which(self, command):
  62. if command in CMD:
  63. return CMD[command]
  64. return self.__run("which", command).rstrip('\n')