10a8c90248264a8b26970b4473770bcc3df8515fJosh Gao"""Execute shell commands via os.popen() and return status, output. 20a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 30a8c90248264a8b26970b4473770bcc3df8515fJosh GaoInterface summary: 40a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 50a8c90248264a8b26970b4473770bcc3df8515fJosh Gao import commands 60a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 70a8c90248264a8b26970b4473770bcc3df8515fJosh Gao outtext = commands.getoutput(cmd) 80a8c90248264a8b26970b4473770bcc3df8515fJosh Gao (exitstatus, outtext) = commands.getstatusoutput(cmd) 90a8c90248264a8b26970b4473770bcc3df8515fJosh Gao outtext = commands.getstatus(file) # returns output of "ls -ld file" 100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 110a8c90248264a8b26970b4473770bcc3df8515fJosh GaoA trailing newline is removed from the output string. 120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 130a8c90248264a8b26970b4473770bcc3df8515fJosh GaoEncapsulates the basic operation: 140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao pipe = os.popen('{ ' + cmd + '; } 2>&1', 'r') 160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao text = pipe.read() 170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao sts = pipe.close() 180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao [Note: it would be nice to add functions to interpret the exit status.] 200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao""" 210a8c90248264a8b26970b4473770bcc3df8515fJosh Gaofrom warnings import warnpy3k 220a8c90248264a8b26970b4473770bcc3df8515fJosh Gaowarnpy3k("the commands module has been removed in Python 3.0; " 230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao "use the subprocess module instead", stacklevel=2) 240a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodel warnpy3k 250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao__all__ = ["getstatusoutput","getoutput","getstatus"] 270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Module 'commands' 290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# 300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Various tools for executing commands and looking at their output and status. 310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# 320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# NB This only works (and is only relevant) for UNIX. 330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Get 'ls -l' status for an object into a string 360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# 370a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef getstatus(file): 380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Return output of "ls -ld <file>" in a string.""" 390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao import warnings 400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao warnings.warn("commands.getstatus() is deprecated", DeprecationWarning, 2) 410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return getoutput('ls -ld' + mkarg(file)) 420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Get the output from a shell command into a string. 450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# The exit status is ignored; a trailing newline is stripped. 460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Assume the command will work with '{ ... ; } 2>&1' around it.. 470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# 480a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef getoutput(cmd): 490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Return output (stdout or stderr) of executing cmd in a shell.""" 500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return getstatusoutput(cmd)[1] 510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Ditto but preserving the exit status. 540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Returns a pair (sts, output) 550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# 560a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef getstatusoutput(cmd): 570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Return (status, output) of executing cmd in a shell.""" 580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao import os 590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao pipe = os.popen('{ ' + cmd + '; } 2>&1', 'r') 600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao text = pipe.read() 610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao sts = pipe.close() 620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if sts is None: sts = 0 630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if text[-1:] == '\n': text = text[:-1] 640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return sts, text 650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Make command argument from directory and pathname (prefix space, add quotes). 680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# 690a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef mk2arg(head, x): 700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao import os 710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return mkarg(os.path.join(head, x)) 720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Make a shell command argument from a string. 750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Return a string beginning with a space followed by a shell-quoted 760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# version of the argument. 770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Two strategies: enclose in single quotes if it contains none; 780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# otherwise, enclose in double quotes and prefix quotable characters 790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# with backslash. 800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# 810a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef mkarg(x): 820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if '\'' not in x: 830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return ' \'' + x + '\'' 840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao s = ' "' 850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for c in x: 860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if c in '\\$"`': 870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao s = s + '\\' 880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao s = s + c 890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao s = s + '"' 900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return s 91