__init__.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #!/usr/bin/env python3
  2. # Contest Management System - http://cms-dev.github.io/
  3. # Copyright © 2012 Bernard Blackham <bernard@largestprime.net>
  4. # Copyright © 2013-2018 Stefano Maggiolo <s.maggiolo@gmail.com>
  5. # Copyright © 2013-2016 Luca Wehrstedt <luca.wehrstedt@gmail.com>
  6. # Copyright © 2014 Luca Versari <veluca93@gmail.com>
  7. # Copyright © 2014 William Di Luigi <williamdiluigi@gmail.com>
  8. # Copyright © 2016 Peyman Jabbarzade Ganje <peyman.jabarzade@gmail.com>
  9. # Copyright © 2017 Luca Chiodini <luca@chiodini.org>
  10. #
  11. # This program is free software: you can redistribute it and/or modify
  12. # it under the terms of the GNU Affero General Public License as
  13. # published by the Free Software Foundation, either version 3 of the
  14. # License, or (at your option) any later version.
  15. #
  16. # This program is distributed in the hope that it will be useful,
  17. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. # GNU Affero General Public License for more details.
  20. #
  21. # You should have received a copy of the GNU Affero General Public License
  22. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. import logging
  24. import subprocess
  25. from cmscommon.commands import pretty_print_cmdline
  26. logger = logging.getLogger(__name__)
  27. # CONFIG is populated by our test script.
  28. CONFIG = {
  29. 'VERBOSITY': 0,
  30. }
  31. class TestException(Exception):
  32. pass
  33. def sh(cmdline, ignore_failure=False):
  34. """Execute a simple command.
  35. cmd ([str]): the (unescaped) command to execute.
  36. ignore_failure (bool): whether to suppress failures.
  37. raise (TestException): if the command failed and ignore_failure was False.
  38. """
  39. if CONFIG["VERBOSITY"] >= 1:
  40. logger.info('$ %s', pretty_print_cmdline(cmdline))
  41. kwargs = dict()
  42. if CONFIG["VERBOSITY"] >= 3:
  43. kwargs["stdout"] = subprocess.DEVNULL
  44. kwargs["stderr"] = subprocess.STDOUT
  45. kwargs["check"] = not ignore_failure
  46. try:
  47. subprocess.run(cmdline, **kwargs)
  48. except subprocess.CalledProcessError as e:
  49. raise TestException("Execution failed") from e