Coder Social home page Coder Social logo

shellingham's Introduction

Shellingham: Tool to Detect Surrounding Shell

image

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

Usage

>>> 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:

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 guarenteed 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:

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')

shellingham's People

Contributors

congee avatar davidlatwe avatar dependabot[bot] avatar glehmann avatar gunungpw avatar ids1024 avatar jeppeklitgaard avatar r4lv avatar thorstenkampe avatar uranusjr avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.