### Install shell Source: https://github.com/toastdriven/shell/blob/master/docs/tutorial.rst Installs the 'shell' library using pip. This is the standard way to install Python packages. ```bash $ pip install shell ``` -------------------------------- ### Python 2 Test Setup Source: https://github.com/toastdriven/shell/blob/master/docs/testing.rst Instructions for setting up the testing environment for Python 2. This includes cloning the repository, creating a virtual environment, activating it, and installing necessary packages like mock and nose. ```shell git clone https://github.com/toastdriven/shell cd shell virtualenv env . env/bin/activate pip install mock==1.0.1 pip install nose==1.3.0 ``` -------------------------------- ### Python 3 Test Setup Source: https://github.com/toastdriven/shell/blob/master/docs/testing.rst Instructions for setting up the testing environment for Python 3. This involves creating a separate virtual environment for Python 3 and installing the same dependencies as for Python 2. ```shell virtualenv -p python3 env3 . env3/bin/activate pip install mock==1.0.1 pip install nose==1.3.0 ``` -------------------------------- ### Chaining commands (functional) Source: https://github.com/toastdriven/shell/blob/master/docs/tutorial.rst Demonstrates chaining multiple operations, like sending input and getting output, in a single line using the functional interface. ```python from shell import shell print(shell('cat -u', has_input=True).write('Hello, world!').output()) ``` -------------------------------- ### Chaining commands (class-based) Source: https://github.com/toastdriven/shell/blob/master/docs/tutorial.rst Demonstrates chaining multiple operations, like sending input and getting output, in a single line using the class-based interface. ```python from shell import Shell print(Shell(has_input=True).run('cat -u').write('Hello, world!').output()) ``` -------------------------------- ### Create a file Source: https://github.com/toastdriven/shell/blob/master/docs/tutorial.rst Shows how to create a file using the 'touch' command with the functional interface of the 'shell' library. ```python from shell import shell shell('touch hello_world.txt') ``` -------------------------------- ### Create a file (Class-based) Source: https://github.com/toastdriven/shell/blob/master/docs/tutorial.rst Shows how to create a file using the 'touch' command with the class-based interface of the 'shell' library. ```python from shell import Shell sh = Shell() sh.run('touch hello_world.txt') ``` -------------------------------- ### Basic ls command Source: https://github.com/toastdriven/shell/blob/master/docs/tutorial.rst Demonstrates running the 'ls' command and iterating over its output using the functional interface of the 'shell' library. ```python from shell import shell ls = shell('ls') for file in ls.output(): print file ``` -------------------------------- ### Basic Shell Command Execution Source: https://github.com/toastdriven/shell/blob/master/README.rst Demonstrates how to execute a simple shell command like 'ls' and iterate over its output. This is the most straightforward way to use the library. ```python from shell import shell ls = shell('ls') for file in ls.output(): print file ``` -------------------------------- ### Basic ls command (Class-based) Source: https://github.com/toastdriven/shell/blob/master/docs/tutorial.rst Demonstrates running the 'ls' command and iterating over its output using the class-based interface of the 'shell' library. ```python from shell import Shell sh = Shell() sh.run('ls') for file in sh.output(): print file ``` -------------------------------- ### Shell Project Overview Source: https://github.com/toastdriven/shell/blob/master/docs/index.rst This documentation master file outlines the structure and purpose of the shell project. It highlights the project's philosophy of simplifying shell command execution in Python, making it more intuitive and user-friendly compared to the standard `subprocess` module. ```documentation .. shell documentation master file, created by sphinx-quickstart on Sun Jun 2 12:36:25 2013. You can adapt this file completely to your liking, but it should at least contain the root `toctree` directive. shell's Documentation ===================== """A better way to run shell commands in Python.""" Built because every time I go to use `subprocess`_, I spend more time in the docs & futzing around than actually implementing what I'm trying to get done. .. _`subprocess`: http://docs.python.org/2.7/library/subprocess.html Philosophy ---------- * Makes running commands more natural * Assumes you care about the output/errors by default * Covers the 80% case of running commands * A nicer API * Works on Linux/OS X (untested on Windows but might work?) Contents: .. toctree:: :maxdepth: 2 tutorial shell_api testing contributing Indices and tables ================== * :ref:`genindex` * :ref:`modindex` * :ref:`search` ``` -------------------------------- ### Send input to an interactive command Source: https://github.com/toastdriven/shell/blob/master/docs/tutorial.rst Demonstrates sending input to an interactive command like 'cat -u' using the 'write' method with the functional interface. Note the limitation of one input shot. ```python from shell import shell sh = shell('cat -u', has_input=True) sh.write('Hello, world!') print(sh.output()) ``` -------------------------------- ### Access process information Source: https://github.com/toastdriven/shell/blob/master/docs/tutorial.rst Shows how to access the process ID (PID) and return code of a completed command. ```python from shell import Shell sh = Shell() sh.run('ls /tmp') print(sh.pid) print(sh.code) ``` -------------------------------- ### Send input to an interactive command (Class-based) Source: https://github.com/toastdriven/shell/blob/master/docs/tutorial.rst Demonstrates sending input to an interactive command like 'cat -u' using the 'write' method with the class-based interface. Note the limitation of one input shot. ```python from shell import Shell sh = Shell(has_input=True) sh.run('cat -u') sh.write('Hello, world!') print(sh.output()) ``` -------------------------------- ### Handle CommandError and access stderr Source: https://github.com/toastdriven/shell/blob/master/docs/tutorial.rst Shows how to catch a CommandError, access its stderr attribute, and use the exit code. ```python import sys from shell import CommandError, shell try: shell('ls /also/definitely/fake', die=True) except CommandError as e: print(e.stderr) sys.exit(e.code) ``` -------------------------------- ### Chaining Shell Commands Source: https://github.com/toastdriven/shell/blob/master/README.rst Illustrates the ability to chain multiple shell operations, including writing input and retrieving output, in a single, fluent call. This enhances code readability for sequential operations. ```python from shell import shell shell('cat -u', has_input=True).write('Hello, world!').output() ``` -------------------------------- ### Read command output and errors Source: https://github.com/toastdriven/shell/blob/master/docs/tutorial.rst Demonstrates capturing and accessing both standard output and standard error streams from a command using the functional interface. ```python from shell import shell sh = shell('ls /tmp') print(sh.output()) print(sh.errors()) ``` -------------------------------- ### Read command output and errors (Class-based) Source: https://github.com/toastdriven/shell/blob/master/docs/tutorial.rst Demonstrates capturing and accessing both standard output and standard error streams from a command using the class-based interface. ```python from shell import Shell sh = Shell() sh.run('ls /tmp') print(sh.output()) print(sh.errors()) ``` -------------------------------- ### Running Tests Source: https://github.com/toastdriven/shell/blob/master/docs/testing.rst Command to execute the tests for the shell project using the nosetests runner. Assumes the testing environment is already set up. ```shell nosetests -s tests.py ``` -------------------------------- ### Shell API Reference Source: https://github.com/toastdriven/shell/blob/master/docs/shell_api.rst This section provides a comprehensive reference for the Shell API, detailing its modules, functions, and their usage. It covers functionalities for executing shell commands, managing subprocesses, and interacting with the operating system's command-line interface. ```APIDOC shell ----- .. automodule:: shell :members: :undoc-members: ``` -------------------------------- ### Propagate non-zero exit codes as exceptions Source: https://github.com/toastdriven/shell/blob/master/docs/tutorial.rst Illustrates how to configure the 'shell' function to raise a CommandError exception when a command returns a non-zero exit code, using the 'die=True' argument. ```python from shell import shell try: shell('ls /not/a/real/place', die=True) except Exception as e: print(e) ``` -------------------------------- ### Shell Object for Extended Behavior Source: https://github.com/toastdriven/shell/blob/master/README.rst Shows how to use the Shell object for more complex interactions, such as providing input to a command. The 'has_input=True' argument enables writing to the command's stdin. ```python from shell import Shell sh = Shell(has_input=True) cat = sh.run('cat -u') cat.write('Hello, world!') cat.output() ``` -------------------------------- ### Ignore large output (Class-based) Source: https://github.com/toastdriven/shell/blob/master/docs/tutorial.rst Shows how to prevent the 'shell' library from capturing output and errors for commands that produce large volumes of data, using 'record_output=False' and 'record_errors=False' with the class-based interface. ```python from shell import Shell sh = Shell(record_output=False, record_errors=False) sh.run('run_intensive_command -v') print(sh.code) ``` -------------------------------- ### Ignore large output Source: https://github.com/toastdriven/shell/blob/master/docs/tutorial.rst Shows how to prevent the 'shell' library from capturing output and errors for commands that produce large volumes of data, using 'record_output=False' and 'record_errors=False'. ```python from shell import shell sh = shell('run_intensive_command -v', record_output=False, record_errors=False) print(sh.code) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.