commands.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. #!/usr/bin/env python3
  2. # Contest Management System - http://cms-dev.github.io/
  3. # Copyright © 2013 Giovanni Mascellani <mascellani@poisson.phc.unipi.it>
  4. # Copyright © 2014 Luca Wehrstedt <luca.wehrstedt@gmail.com>
  5. # Copyright © 2018 Stefano Maggiolo <s.maggiolo@gmail.com>
  6. #
  7. # This program is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU Affero General Public License as
  9. # published by the Free Software Foundation, either version 3 of the
  10. # License, or (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU Affero General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Affero General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. from shlex import quote
  20. __all__ = [
  21. "pretty_print_cmdline",
  22. ]
  23. def pretty_print_cmdline(cmdline):
  24. """Pretty print a command line.
  25. Take a command line suitable to be passed to a Popen-like call and
  26. returns a string that represents it in a way that preserves the
  27. structure of arguments and can be passed to bash as is.
  28. More precisely, delimitate every item of the command line with
  29. single apstrophes and join all the arguments separating them with
  30. spaces.
  31. """
  32. return " ".join(quote(x) for x in cmdline)