Skip to main content

A python module for sourcing variables from shell scripts

Project description

python-shell-source

A python module for sourcing variables from shell scripts.

Installation

$ pip install shell-source

Documentation

The full documentation is available here

Usage

This module provides a function source which attempts to mimic the shell's source command.

The purpose of this function is to allow you to run a shell script which sets either environment variables or local variables, and then give you access to those variables. Normally this is not a straght-forward task, but this function achieves it by running the script in its intended shell then injecting commands to the shell to print its local variables and its environment variables. Finally it collects the shell's stdout and parses it to return to you with exactly the data you asked for.

Basic Usage

If you just pass a script and an interpreter you'll get back all the environment variables and local variables visible to and set by the script.

>>> from shell_source import source
>>> variables = source("path/to/script.sh", "bash")
>>> # It returns a dictionary of local and environment variables known by the script.
>>> variables
{"USER": "abraham", "PATH": "/bin:/usr/bin", ..., "foo": "bar"}

Requesting Specific Variables

If you specify the argument variables, then only those variables you passed will be present as keys in the returned dictionary.

>>> source("path/to/script.sh", "csh", variables=("foo", "bar", "biz"))
{"foo": ..., "bar": ..., "biz", ...}

Ignoring Local Variables

If you don't want to obtain any local variables set by the script, but only want the environment variables, you can pass ignore_locals=True.

Supporting Different Shells

This module has been tested to work with bash, zsh, tcsh, and ksh. You can use any other shell that's somewhat posix compliant and supports the keyword "source", but it it doesn't work, you may use the ShellConfig class to indicate to source how to interact with your shell.

The class ShellConfig contains several string templates which are used to run the necessary commands with the shell. If the shell you want to use doesn't support any of the commands set by default in that class, you can pass an instance of ShellConfig to source to override the default templates.

For example, csh and fish are not supported by default, (specifically because they don't have the variable $? to get the exit status of the last command,) but we can source a script for one of these shells anyways by passing a ShellConfig instance which will declare how to get the exit code of the previous command.

source(
	"path/to/script.csh",
	"csh",
	shell_config=ShellConfig(prev_exit_code="$status")
)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

shell-source-1.0.1.tar.gz (5.4 kB view details)

Uploaded Source

Built Distribution

shell_source-1.0.1-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

Details for the file shell-source-1.0.1.tar.gz.

File metadata

  • Download URL: shell-source-1.0.1.tar.gz
  • Upload date:
  • Size: 5.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.8 CPython/3.7.13 Linux/5.13.0-1031-azure

File hashes

Hashes for shell-source-1.0.1.tar.gz
Algorithm Hash digest
SHA256 9fde980703ec882432ba3dfff2741d50571b73e55c1ad7cda2d71185dc0f70e9
MD5 d1ad20d31194af34229bb1b14501d0b1
BLAKE2b-256 24b1bc7305ab628493b9eb2fc66d276058b6e46774cdf2126538f115e15cda24

See more details on using hashes here.

File details

Details for the file shell_source-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: shell_source-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 5.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.8 CPython/3.7.13 Linux/5.13.0-1031-azure

File hashes

Hashes for shell_source-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 9e3c82c20bf972733ce4c3cffae27d2b8c66b3c969e43f139a9751b6f8406d98
MD5 d8713475b6797b7c9e1f1ab7035ad78e
BLAKE2b-256 c7c6a055e1985ed6758ebff2466be0c2c377c8a1459e187c71b36965fe47b936

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page