shellingham


Nameshellingham JSON
Version 1.5.4 PyPI version JSON
download
home_pagehttps://github.com/sarugaku/shellingham
SummaryTool to Detect Surrounding Shell
upload_time2023-10-24 04:13:40
maintainer
docs_urlNone
authorTzu-ping Chung
requires_python>=3.7
licenseISC License
keywords shell
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            =============================================
Shellingham: Tool to Detect Surrounding Shell
=============================================

.. image:: https://img.shields.io/pypi/v/shellingham.svg
    :target: https://pypi.org/project/shellingham/

Shellingham detects what shell the current Python executable is running in.


Usage
=====

.. code-block:: python

    >>> import shellingham
    >>> shellingham.detect_shell()
    ('bash', '/bin/bash')

``detect_shell`` pokes around the process's running environment to determine
what shell it is run in. It returns a 2-tuple:

* The shell name, always lowercased.
* The command used to run the shell.

``ShellDetectionFailure`` is raised if ``detect_shell`` fails to detect the
surrounding shell.


Notes
=====

* The shell name is always lowercased.
* On Windows, the shell name is the name of the executable, minus the file
  extension.


Notes for Application Developers
================================

Remember, your application's user is not necessarily using a shell.
Shellingham raises ``ShellDetectionFailure`` if there is no shell to detect,
but *your application should almost never do this to your user*.

A practical approach to this is to wrap ``detect_shell`` in a try block, and
provide a sane default on failure

.. code-block:: python

    try:
        shell = shellingham.detect_shell()
    except shellingham.ShellDetectionFailure:
        shell = provide_default()


There are a few choices for you to choose from.

* The POSIX standard mandates the environment variable ``SHELL`` to refer to
  "the user's preferred command language interpreter". This is always available
  (even if the user is not in an interactive session), and likely the correct
  choice to launch an interactive sub-shell with.
* A command ``sh`` is almost guaranteed to exist, likely at ``/bin/sh``, since
  several POSIX tools rely on it. This should be suitable if you want to run a
  (possibly non-interactive) script.
* All versions of DOS and Windows have an environment variable ``COMSPEC``.
  This can always be used to launch a usable command prompt (e.g. `cmd.exe` on
  Windows).

Here's a simple implementation to provide a default shell

.. code-block:: python

    import os

    def provide_default():
        if os.name == 'posix':
            return os.environ['SHELL']
        elif os.name == 'nt':
            return os.environ['COMSPEC']
        raise NotImplementedError(f'OS {os.name!r} support not available')

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/sarugaku/shellingham",
    "name": "shellingham",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.7",
    "maintainer_email": "",
    "keywords": "shell",
    "author": "Tzu-ping Chung",
    "author_email": "uranusjr@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/58/15/8b3609fd3830ef7b27b655beb4b4e9c62313a4e8da8c676e142cc210d58e/shellingham-1.5.4.tar.gz",
    "platform": null,
    "description": "=============================================\nShellingham: Tool to Detect Surrounding Shell\n=============================================\n\n.. image:: https://img.shields.io/pypi/v/shellingham.svg\n    :target: https://pypi.org/project/shellingham/\n\nShellingham detects what shell the current Python executable is running in.\n\n\nUsage\n=====\n\n.. code-block:: python\n\n    >>> import shellingham\n    >>> shellingham.detect_shell()\n    ('bash', '/bin/bash')\n\n``detect_shell`` pokes around the process's running environment to determine\nwhat shell it is run in. It returns a 2-tuple:\n\n* The shell name, always lowercased.\n* The command used to run the shell.\n\n``ShellDetectionFailure`` is raised if ``detect_shell`` fails to detect the\nsurrounding shell.\n\n\nNotes\n=====\n\n* The shell name is always lowercased.\n* On Windows, the shell name is the name of the executable, minus the file\n  extension.\n\n\nNotes for Application Developers\n================================\n\nRemember, your application's user is not necessarily using a shell.\nShellingham raises ``ShellDetectionFailure`` if there is no shell to detect,\nbut *your application should almost never do this to your user*.\n\nA practical approach to this is to wrap ``detect_shell`` in a try block, and\nprovide a sane default on failure\n\n.. code-block:: python\n\n    try:\n        shell = shellingham.detect_shell()\n    except shellingham.ShellDetectionFailure:\n        shell = provide_default()\n\n\nThere are a few choices for you to choose from.\n\n* The POSIX standard mandates the environment variable ``SHELL`` to refer to\n  \"the user's preferred command language interpreter\". This is always available\n  (even if the user is not in an interactive session), and likely the correct\n  choice to launch an interactive sub-shell with.\n* A command ``sh`` is almost guaranteed to exist, likely at ``/bin/sh``, since\n  several POSIX tools rely on it. This should be suitable if you want to run a\n  (possibly non-interactive) script.\n* All versions of DOS and Windows have an environment variable ``COMSPEC``.\n  This can always be used to launch a usable command prompt (e.g. `cmd.exe` on\n  Windows).\n\nHere's a simple implementation to provide a default shell\n\n.. code-block:: python\n\n    import os\n\n    def provide_default():\n        if os.name == 'posix':\n            return os.environ['SHELL']\n        elif os.name == 'nt':\n            return os.environ['COMSPEC']\n        raise NotImplementedError(f'OS {os.name!r} support not available')\n",
    "bugtrack_url": null,
    "license": "ISC License",
    "summary": "Tool to Detect Surrounding Shell",
    "version": "1.5.4",
    "project_urls": {
        "Homepage": "https://github.com/sarugaku/shellingham"
    },
    "split_keywords": [
        "shell"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "e0f90595336914c5619e5f28a1fb793285925a8cd4b432c9da0a987836c7f822",
                "md5": "c7dfcd723fcc27c548ad3a7b06c976dc",
                "sha256": "7ecfff8f2fd72616f7481040475a65b2bf8af90a56c89140852d1120324e8686"
            },
            "downloads": -1,
            "filename": "shellingham-1.5.4-py2.py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "c7dfcd723fcc27c548ad3a7b06c976dc",
            "packagetype": "bdist_wheel",
            "python_version": "py2.py3",
            "requires_python": ">=3.7",
            "size": 9755,
            "upload_time": "2023-10-24T04:13:38",
            "upload_time_iso_8601": "2023-10-24T04:13:38.866125Z",
            "url": "https://files.pythonhosted.org/packages/e0/f9/0595336914c5619e5f28a1fb793285925a8cd4b432c9da0a987836c7f822/shellingham-1.5.4-py2.py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "58158b3609fd3830ef7b27b655beb4b4e9c62313a4e8da8c676e142cc210d58e",
                "md5": "883d381da8fe788fb24428591e61eb20",
                "sha256": "8dbca0739d487e5bd35ab3ca4b36e11c4078f3a234bfce294b0a0291363404de"
            },
            "downloads": -1,
            "filename": "shellingham-1.5.4.tar.gz",
            "has_sig": false,
            "md5_digest": "883d381da8fe788fb24428591e61eb20",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.7",
            "size": 10310,
            "upload_time": "2023-10-24T04:13:40",
            "upload_time_iso_8601": "2023-10-24T04:13:40.426335Z",
            "url": "https://files.pythonhosted.org/packages/58/15/8b3609fd3830ef7b27b655beb4b4e9c62313a4e8da8c676e142cc210d58e/shellingham-1.5.4.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-10-24 04:13:40",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "sarugaku",
    "github_project": "shellingham",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "tox": true,
    "lcname": "shellingham"
}
        
Elapsed time: 0.19573s