You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

107 line
3.4 KiB

  1. Metadata-Version: 2.1
  2. Name: shellingham
  3. Version: 1.5.4
  4. Summary: Tool to Detect Surrounding Shell
  5. Home-page: https://github.com/sarugaku/shellingham
  6. Author: Tzu-ping Chung
  7. Author-email: uranusjr@gmail.com
  8. License: ISC License
  9. Keywords: shell
  10. Classifier: Development Status :: 3 - Alpha
  11. Classifier: Environment :: Console
  12. Classifier: Intended Audience :: Developers
  13. Classifier: License :: OSI Approved :: ISC License (ISCL)
  14. Classifier: Operating System :: OS Independent
  15. Classifier: Programming Language :: Python :: 3 :: Only
  16. Classifier: Programming Language :: Python :: 3.7
  17. Classifier: Programming Language :: Python :: 3.8
  18. Classifier: Programming Language :: Python :: 3.9
  19. Classifier: Programming Language :: Python :: 3.10
  20. Classifier: Programming Language :: Python :: 3.11
  21. Classifier: Programming Language :: Python :: 3.12
  22. Classifier: Topic :: Software Development :: Libraries :: Python Modules
  23. Requires-Python: >=3.7
  24. Description-Content-Type: text/x-rst
  25. License-File: LICENSE
  26. =============================================
  27. Shellingham: Tool to Detect Surrounding Shell
  28. =============================================
  29. .. image:: https://img.shields.io/pypi/v/shellingham.svg
  30. :target: https://pypi.org/project/shellingham/
  31. Shellingham detects what shell the current Python executable is running in.
  32. Usage
  33. =====
  34. .. code-block:: python
  35. >>> import shellingham
  36. >>> shellingham.detect_shell()
  37. ('bash', '/bin/bash')
  38. ``detect_shell`` pokes around the process's running environment to determine
  39. what shell it is run in. It returns a 2-tuple:
  40. * The shell name, always lowercased.
  41. * The command used to run the shell.
  42. ``ShellDetectionFailure`` is raised if ``detect_shell`` fails to detect the
  43. surrounding shell.
  44. Notes
  45. =====
  46. * The shell name is always lowercased.
  47. * On Windows, the shell name is the name of the executable, minus the file
  48. extension.
  49. Notes for Application Developers
  50. ================================
  51. Remember, your application's user is not necessarily using a shell.
  52. Shellingham raises ``ShellDetectionFailure`` if there is no shell to detect,
  53. but *your application should almost never do this to your user*.
  54. A practical approach to this is to wrap ``detect_shell`` in a try block, and
  55. provide a sane default on failure
  56. .. code-block:: python
  57. try:
  58. shell = shellingham.detect_shell()
  59. except shellingham.ShellDetectionFailure:
  60. shell = provide_default()
  61. There are a few choices for you to choose from.
  62. * The POSIX standard mandates the environment variable ``SHELL`` to refer to
  63. "the user's preferred command language interpreter". This is always available
  64. (even if the user is not in an interactive session), and likely the correct
  65. choice to launch an interactive sub-shell with.
  66. * A command ``sh`` is almost guaranteed to exist, likely at ``/bin/sh``, since
  67. several POSIX tools rely on it. This should be suitable if you want to run a
  68. (possibly non-interactive) script.
  69. * All versions of DOS and Windows have an environment variable ``COMSPEC``.
  70. This can always be used to launch a usable command prompt (e.g. `cmd.exe` on
  71. Windows).
  72. Here's a simple implementation to provide a default shell
  73. .. code-block:: python
  74. import os
  75. def provide_default():
  76. if os.name == 'posix':
  77. return os.environ['SHELL']
  78. elif os.name == 'nt':
  79. return os.environ['COMSPEC']
  80. raise NotImplementedError(f'OS {os.name!r} support not available')