aboutsummaryrefslogtreecommitdiffstats
path: root/tools
diff options
context:
space:
mode:
authorSimon Glass2012-12-15 04:42:04 -0600
committerSimon Glass2013-04-04 16:04:33 -0500
commita10fd93cbc2ddf1da1f8b6d915f4bc3276ff7731 (patch)
tree738c8809d2a150bd507a8c0b0b27474e854042ee /tools
parent71162e3cae29832192497d4a1b966336b638df01 (diff)
downloadu-boot-a10fd93cbc2ddf1da1f8b6d915f4bc3276ff7731.tar.gz
u-boot-a10fd93cbc2ddf1da1f8b6d915f4bc3276ff7731.tar.xz
u-boot-a10fd93cbc2ddf1da1f8b6d915f4bc3276ff7731.zip
patman: Make command methods return a CommandResult
Rather than returning a list of things, return an object. That makes it easier to access the returned items, and easier to extend the return value later. Signed-off-by: Simon Glass <sjg@chromium.org>
Diffstat (limited to 'tools')
-rw-r--r--tools/patman/command.py84
-rw-r--r--tools/patman/gitutil.py2
-rw-r--r--tools/patman/patchstream.py2
3 files changed, 64 insertions, 24 deletions
diff --git a/tools/patman/command.py b/tools/patman/command.py
index 4b00250c02..fc085f256d 100644
--- a/tools/patman/command.py
+++ b/tools/patman/command.py
@@ -20,53 +20,93 @@
20# 20#
21 21
22import os 22import os
23import subprocess 23import cros_subprocess
24 24
25"""Shell command ease-ups for Python.""" 25"""Shell command ease-ups for Python."""
26 26
27def RunPipe(pipeline, infile=None, outfile=None, 27class CommandResult:
28 capture=False, oneline=False, hide_stderr=False): 28 """A class which captures the result of executing a command.
29
30 Members:
31 stdout: stdout obtained from command, as a string
32 stderr: stderr obtained from command, as a string
33 return_code: Return code from command
34 exception: Exception received, or None if all ok
35 """
36 def __init__(self):
37 self.stdout = None
38 self.stderr = None
39 self.return_code = None
40 self.exception = None
41
42
43def RunPipe(pipe_list, infile=None, outfile=None,
44 capture=False, capture_stderr=False, oneline=False,
45 cwd=None, **kwargs):
29 """ 46 """
30 Perform a command pipeline, with optional input/output filenames. 47 Perform a command pipeline, with optional input/output filenames.
31 48
32 hide_stderr Don't allow output of stderr (default False) 49 Args:
50 pipe_list: List of command lines to execute. Each command line is
51 piped into the next, and is itself a list of strings. For
52 example [ ['ls', '.git'] ['wc'] ] will pipe the output of
53 'ls .git' into 'wc'.
54 infile: File to provide stdin to the pipeline
55 outfile: File to store stdout
56 capture: True to capture output
57 capture_stderr: True to capture stderr
58 oneline: True to strip newline chars from output
59 kwargs: Additional keyword arguments to cros_subprocess.Popen()
60 Returns:
61 CommandResult object
33 """ 62 """
63 result = CommandResult()
34 last_pipe = None 64 last_pipe = None
65 pipeline = list(pipe_list)
35 while pipeline: 66 while pipeline:
36 cmd = pipeline.pop(0) 67 cmd = pipeline.pop(0)
37 kwargs = {}
38 if last_pipe is not None: 68 if last_pipe is not None:
39 kwargs['stdin'] = last_pipe.stdout 69 kwargs['stdin'] = last_pipe.stdout
40 elif infile: 70 elif infile:
41 kwargs['stdin'] = open(infile, 'rb') 71 kwargs['stdin'] = open(infile, 'rb')
42 if pipeline or capture: 72 if pipeline or capture:
43 kwargs['stdout'] = subprocess.PIPE 73 kwargs['stdout'] = cros_subprocess.PIPE
44 elif outfile: 74 elif outfile:
45 kwargs['stdout'] = open(outfile, 'wb') 75 kwargs['stdout'] = open(outfile, 'wb')
46 if hide_stderr: 76 if capture_stderr:
47 kwargs['stderr'] = open('/dev/null', 'wb') 77 kwargs['stderr'] = cros_subprocess.PIPE
48 78
49 last_pipe = subprocess.Popen(cmd, **kwargs) 79 try:
80 last_pipe = cros_subprocess.Popen(cmd, cwd=cwd, **kwargs)
81 except Exception, err:
82 result.exception = err
83 print 'exception', pipe_list, err
84 raise Exception("Error running '%s': %s" % (pipe_list, str))
50 85
51 if capture: 86 if capture:
52 ret = last_pipe.communicate()[0] 87 result.stdout, result.stderr, result.combined = (
53 if not ret: 88 last_pipe.CommunicateFilter(None))
54 return None 89 if result.stdout and oneline:
55 elif oneline: 90 result.output = result.stdout.rstrip('\r\n')
56 return ret.rstrip('\r\n') 91 result.return_code = last_pipe.wait()
57 else:
58 return ret
59 else: 92 else:
60 return os.waitpid(last_pipe.pid, 0)[1] == 0 93 result.return_code = os.waitpid(last_pipe.pid, 0)[1]
94 if result.return_code:
95 raise Exception("Error running '%s'" % pipe_list)
96 return result
61 97
62def Output(*cmd): 98def Output(*cmd):
63 return RunPipe([cmd], capture=True) 99 return RunPipe([cmd], capture=True).stdout
64 100
65def OutputOneLine(*cmd): 101def OutputOneLine(*cmd, **kwargs):
66 return RunPipe([cmd], capture=True, oneline=True) 102 return (RunPipe([cmd], capture=True, oneline=True,
103 **kwargs).stdout.strip())
67 104
68def Run(*cmd, **kwargs): 105def Run(*cmd, **kwargs):
69 return RunPipe([cmd], **kwargs) 106 return RunPipe([cmd], **kwargs).stdout
70 107
71def RunList(cmd): 108def RunList(cmd):
72 return RunPipe([cmd], capture=True) 109 return RunPipe([cmd], capture=True).stdout
110
111def StopAll():
112 cros_subprocess.stay_alive = False
diff --git a/tools/patman/gitutil.py b/tools/patman/gitutil.py
index ca3ba4a03e..77907c15c8 100644
--- a/tools/patman/gitutil.py
+++ b/tools/patman/gitutil.py
@@ -40,7 +40,7 @@ def CountCommitsToBranch():
40 """ 40 """
41 pipe = [['git', 'log', '--no-color', '--oneline', '@{upstream}..'], 41 pipe = [['git', 'log', '--no-color', '--oneline', '@{upstream}..'],
42 ['wc', '-l']] 42 ['wc', '-l']]
43 stdout = command.RunPipe(pipe, capture=True, oneline=True) 43 stdout = command.RunPipe(pipe, capture=True, oneline=True).stdout
44 patch_count = int(stdout) 44 patch_count = int(stdout)
45 return patch_count 45 return patch_count
46 46
diff --git a/tools/patman/patchstream.py b/tools/patman/patchstream.py
index f7ee75a25f..1e4a36f1dd 100644
--- a/tools/patman/patchstream.py
+++ b/tools/patman/patchstream.py
@@ -346,7 +346,7 @@ def GetMetaData(start, count):
346 """ 346 """
347 pipe = [['git', 'log', '--no-color', '--reverse', 'HEAD~%d' % start, 347 pipe = [['git', 'log', '--no-color', '--reverse', 'HEAD~%d' % start,
348 '-n%d' % count]] 348 '-n%d' % count]]
349 stdout = command.RunPipe(pipe, capture=True) 349 stdout = command.RunPipe(pipe, capture=True).stdout
350 series = Series() 350 series = Series()
351 ps = PatchStream(series, is_log=True) 351 ps = PatchStream(series, is_log=True)
352 for line in stdout.splitlines(): 352 for line in stdout.splitlines():