METADATA 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. Metadata-Version: 2.0
  2. Name: shellescape
  3. Version: 3.4.1
  4. Summary: Shell escape a string to safely use it as a token in a shell command (backport of Python shlex.quote for Python versions 2.x & < 3.3)
  5. Home-page: https://github.com/chrissimpkins/shellescape
  6. Author: Christopher Simpkins
  7. Author-email: git.simpkins@gmail.com
  8. License: MIT license
  9. Keywords: shell,quote,escape,backport,command line,command,subprocess
  10. Platform: any
  11. Classifier: Development Status :: 5 - Production/Stable
  12. Classifier: Intended Audience :: Developers
  13. Classifier: Natural Language :: English
  14. Classifier: License :: OSI Approved :: MIT License
  15. Classifier: Programming Language :: Python
  16. Classifier: Programming Language :: Python :: 2
  17. Classifier: Programming Language :: Python :: 3
  18. Classifier: Operating System :: MacOS :: MacOS X
  19. Classifier: Operating System :: POSIX
  20. Classifier: Operating System :: Unix
  21. Classifier: Operating System :: Microsoft :: Windows
  22. Source Repository: https://github.com/chrissimpkins/shellescape
  23. Description
  24. -----------
  25. The shellescape Python module defines the ``shellescape.quote()`` function that returns a shell-escaped version of a Python string. This is a backport of the ``shlex.quote()`` function from Python 3.4.3 that makes it accessible to users of Python 3 versions < 3.3 and all Python 2.x versions.
  26. quote(s)
  27. --------
  28. >From the Python documentation:
  29. Return a shell-escaped version of the string s. The returned value is a string that can safely be used as one token in a shell command line, for cases where you cannot use a list.
  30. This idiom would be unsafe:
  31. .. code-block:: python
  32. >>> filename = 'somefile; rm -rf ~'
  33. >>> command = 'ls -l {}'.format(filename)
  34. >>> print(command) # executed by a shell: boom!
  35. ls -l somefile; rm -rf ~
  36. ``quote()`` lets you plug the security hole:
  37. .. code-block:: python
  38. >>> command = 'ls -l {}'.format(quote(filename))
  39. >>> print(command)
  40. ls -l 'somefile; rm -rf ~'
  41. >>> remote_command = 'ssh home {}'.format(quote(command))
  42. >>> print(remote_command)
  43. ssh home 'ls -l '"'"'somefile; rm -rf ~'"'"''
  44. The quoting is compatible with UNIX shells and with ``shlex.split()``:
  45. .. code-block:: python
  46. >>> remote_command = split(remote_command)
  47. >>> remote_command
  48. ['ssh', 'home', "ls -l 'somefile; rm -rf ~'"]
  49. >>> command = split(remote_command[-1])
  50. >>> command
  51. ['ls', '-l', 'somefile; rm -rf ~']
  52. Usage
  53. -----
  54. Include ``shellescape`` in your project setup.py file ``install_requires`` dependency definition list:
  55. .. code-block:: python
  56. setup(
  57. ...
  58. install_requires=['shellescape'],
  59. ...
  60. )
  61. Then import the ``quote`` function into your module(s) and use it as needed:
  62. .. code-block:: python
  63. #!/usr/bin/env python
  64. # -*- coding: utf-8 -*-
  65. from shellescape import quote
  66. filename = "somefile; rm -rf ~"
  67. escaped_shell_command = 'ls -l {}'.format(quote(filename))
  68. Issue Reporting
  69. ---------------
  70. Issue reporting is available on the `GitHub repository <https://github.com/chrissimpkins/shellescape/issues>`_