A Python utility belt containing simple tools, a stdlib like feel, and extra batteries
Execute shell commands with enhanced features, platform detection, and system integration utilities for cross-platform compatibility.
Enhanced subprocess execution with features like verbose output, result capturing, and flexible parameter handling.
def cmd(command, shell=False, detach=False, verbose=0, tee=None, cwd=None, env=None, tee_backend='auto', check=False, system=False, timeout=None, capture=True):
"""
Execute subprocess with enhanced features.
Args:
command (str|list): Command to execute
shell (bool): Use shell for execution
detach (bool): Detach process (don't wait)
verbose (int): Verbosity level (0-3)
tee (bool|None): Print output while capturing
cwd (str): Working directory
env (dict): Environment variables
tee_backend (str): Tee backend ('auto', 'thread', etc.)
check (bool): Raise exception on non-zero exit
system (bool): Use os.system instead of subprocess
timeout (float): Command timeout in seconds
capture (bool): Capture output
Returns:
CmdOutput: Enhanced result with stdout, stderr, ret attributes
"""
class CmdOutput(dict):
"""
Container for command output with dict-like and subprocess.CompletedProcess compatibility.
Attributes:
out (str): Standard output
err (str): Standard error
ret (int): Return code
proc: Subprocess object
"""
@property
def stdout(self): ...
@property
def stderr(self): ...
@property
def returncode(self): ...Constants and utilities for detecting the current operating system and platform-specific behavior.
# Platform detection constants
WIN32: bool # True if running on Windows
LINUX: bool # True if running on Linux
DARWIN: bool # True if running on macOS
POSIX: bool # True if running on POSIX system
def find_exe(name, **kwargs):
"""
Find executable in PATH.
Args:
name (str): Executable name
**kwargs: Additional search options
Returns:
str|None: Path to executable or None if not found
"""
def find_path(name, **kwargs):
"""
Find path to executable or library.
Args:
name (str): Name to search for
**kwargs: Additional search options
Returns:
str|None: Path to item or None if not found
"""Cross-platform utilities for accessing standard system directories like cache, config, and data directories.
def platform_cache_dir():
"""
Get platform-specific cache directory.
Returns:
str: Cache directory path
Examples:
- Linux: ~/.cache
- macOS: ~/Library/Caches
- Windows: %LOCALAPPDATA%
"""
def platform_config_dir():
"""
Get platform-specific config directory.
Returns:
str: Config directory path
Examples:
- Linux: ~/.config
- macOS: ~/Library/Application Support
- Windows: %APPDATA%
"""
def platform_data_dir():
"""
Get platform-specific data directory.
Returns:
str: Data directory path
Examples:
- Linux: ~/.local/share
- macOS: ~/Library/Application Support
- Windows: %APPDATA%
"""# These functions are deprecated - use platform_*_dir() instead
def ensure_app_cache_dir(*args, **kwargs):
"""DEPRECATED: Use platform_cache_dir() instead"""
def ensure_app_config_dir(*args, **kwargs):
"""DEPRECATED: Use platform_config_dir() instead"""
def ensure_app_data_dir(*args, **kwargs):
"""DEPRECATED: Use platform_data_dir() instead"""
def get_app_cache_dir(*args, **kwargs):
"""DEPRECATED: Use platform_cache_dir() instead"""
def get_app_config_dir(*args, **kwargs):
"""DEPRECATED: Use platform_config_dir() instead"""
def get_app_data_dir(*args, **kwargs):
"""DEPRECATED: Use platform_data_dir() instead"""import ubelt as ub
# Basic command execution
result = ub.cmd('echo "Hello World"')
print(result['out']) # "Hello World\n"
print(result.ret) # 0
# Verbose execution with real-time output
result = ub.cmd('ls -la', verbose=2)
# Command with custom environment
env = {'MY_VAR': 'custom_value'}
result = ub.cmd('echo $MY_VAR', shell=True, env=env)
# Capture both stdout and stderr
result = ub.cmd('python -c "import sys; print(\\"out\\"); print(\\"err\\", file=sys.stderr)"')
print("STDOUT:", result['out'])
print("STDERR:", result['err'])
# Check if command succeeded
if result.ret == 0:
print("Command succeeded")
else:
print(f"Command failed with code {result.ret}")import ubelt as ub
# Platform-specific logic
if ub.WIN32:
# Windows-specific code
cmd = 'dir'
elif ub.LINUX:
# Linux-specific code
cmd = 'ls'
elif ub.DARWIN:
# macOS-specific code
cmd = 'ls -G'
else:
# Generic POSIX
cmd = 'ls'
result = ub.cmd(cmd)
# Find executables
python_path = ub.find_exe('python')
if python_path:
print(f"Python found at: {python_path}")
else:
print("Python not found in PATH")import ubelt as ub
# Get platform-appropriate directories
cache_dir = ub.platform_cache_dir()
config_dir = ub.platform_config_dir()
data_dir = ub.platform_data_dir()
print(f"Cache: {cache_dir}")
print(f"Config: {config_dir}")
print(f"Data: {data_dir}")
# Use for application-specific paths
app_cache = ub.ensuredir(cache_dir, 'myapp')
app_config = ub.ensuredir(config_dir, 'myapp')import ubelt as ub
# Cross-platform file listing
if ub.WIN32:
result = ub.cmd('dir /B', shell=True)
else:
result = ub.cmd('ls -1')
files = result['out'].strip().split('\n')
# Cross-platform path operations
if ub.WIN32:
# Windows uses different path separators
separator = '\\'
else:
separator = '/'
# Better to use ubelt's path utilities instead
from pathlib import Path
path = ub.Path('/some/path') # Works cross-platformInstall with Tessl CLI
npx tessl i tessl/pypi-ubelt