A python module for sourcing variables from shell scripts
Project description
python-shell-source
A python module for sourcing variables from shell scripts.
Installation
You can install this package with pip or conda.
$ pip install shell-source
$ conda install -c abrahammurciano 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 write its local variables and its environment variables to a temporary file. Finally it reads the temporary file 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
Built Distribution
File details
Details for the file shell_source-2.0.0.tar.gz
.
File metadata
- Download URL: shell_source-2.0.0.tar.gz
- Upload date:
- Size: 5.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.5.0 CPython/3.7.16 Linux/5.15.0-1037-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d72a41b313ce9c160340366648e041aade67e0e8f7976af6f9c727bdf780d685 |
|
MD5 | 323c3e86f777d5b8fb0e7a96430397b3 |
|
BLAKE2b-256 | b9f26e73eb236edeb4bbec201c8663c92b525e442b8aec21f820728ec36d6488 |
File details
Details for the file shell_source-2.0.0-py3-none-any.whl
.
File metadata
- Download URL: shell_source-2.0.0-py3-none-any.whl
- Upload date:
- Size: 6.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.5.0 CPython/3.7.16 Linux/5.15.0-1037-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f6f619713558140a8f6aa470c869d9d2d77075a3fb5d314eaf2d0bbc87d3a930 |
|
MD5 | 8bad1d36864730a70ba3a75ed281334d |
|
BLAKE2b-256 | 85b9b6ac08044204293ec5c61d4933c9d0146d01ce0a74768b2ac9b57cf9cf93 |