mozshellutil

raw JSON →
1.0.0 verified Mon Apr 27 auth: no python

MozShellUtil is a Python library providing shell command line parsing utilities for Mozilla testing. It offers functionality to parse and execute shell commands in a cross-platform manner, handling quotes, variables, and subprocess launching. The current version is 1.0.0, with a requirement of Python >=3.8. The library is maintained by the Mozilla community and released as needed.

pip install mozshellutil
error AttributeError: module 'mozshellutil' has no attribute 'Shell'
cause Incorrect import path; likely imported the package but the symbol is not directly under the module.
fix
Use 'from mozshellutil import Shell'.
error TypeError: Command() argument after * must be an iterable, not str
cause Passed a string instead of a list to Command.
fix
Change Command('ls -la') to Command(['ls', '-la']).
error MozShellResult object has no attribute 'stdout'
cause Trying to access stdout on a result that failed or library version mismatch.
fix
Ensure the command ran successfully. Use result.returncode to check first. If using an older version, upgrade to 1.0.0.
breaking Command line parsing is minimal. Shell.run does not expand environment variables in arguments by default; you must pass them explicitly in the env dict.
fix Use shell-specific expansion (e.g., os.path.expandvars) before creating Command.
gotcha Shell.run returns a MozShellResult object, not subprocess.CompletedProcess. Its fields are stdout, stderr, returncode but as bytes. Decode manually if needed.
fix Access result.stdout.decode('utf-8') for string output.
gotcha Do not pass a list of strings as a single string to Command; it expects a sequence. Passing a string will treat each character as a separate argument.
fix Always provide a list of strings, e.g., Command(['ls', '-la']).

Creates a Shell instance to run Command objects. Each Command specifies the executable and arguments, and optionally an environment. The run method returns a CompletedProcess-like object.

from mozshellutil import Shell, Command

shell = Shell()
# Run a simple command
result = shell.run(Command(['echo', 'hello']))
print(result.stdout)  # b'hello\n'

# With arguments and environment
env = {'MY_VAR': 'value'} if os.environ.get('TEST') else {}
result = shell.run(Command(['printenv', 'MY_VAR'], env=env))
print(result.returncode)