shell.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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 sys
  16. from subprocess import (
  17. PIPE,
  18. Popen,
  19. CalledProcessError
  20. )
  21. from ..common.error import Error
  22. #TODO: Find a better way since which is tempramental.
  23. CMD = {
  24. "eclcc": "/usr/bin/eclcc",
  25. "ecl": "/usr/bin/ecl",
  26. "configgen": "/opt/HPCCSystems/sbin/configgen"
  27. }
  28. class Shell:
  29. def command(self, *command_args):
  30. def __command(*args):
  31. all_args = command_args + args
  32. return self.__run(*all_args)
  33. return __command
  34. def __run(self, *args, **kwargs):
  35. args = [i for i in args if i is not None]
  36. process = Popen(
  37. args, stdout=kwargs.pop('stdout', PIPE),
  38. stderr=kwargs.pop('stderr', PIPE),
  39. close_fds=kwargs.pop('close_fds', True), **kwargs)
  40. stdout, stderr = process.communicate()
  41. if process.returncode:
  42. exception = CalledProcessError(
  43. process.returncode, repr(args))
  44. exception.output = ''.join(filter(None, [stdout, stderr]))
  45. raise Error('1001', err=exception.output)
  46. return stdout
  47. # Currently hacked to use the CMD dict as which can be tempramental.
  48. # - What other methods can be used?
  49. # - Support multiple versions of eclcc?
  50. def which(self, command):
  51. if command in CMD:
  52. return CMD[command]
  53. return self.__run("which", command).rstrip('\n')