6d68e26a811dced94e37a38226087da5095a47fb
[platform/upstream/mic.git] / mic / utils / runner.py
1 #!/usr/bin/python -tt
2 #
3 # Copyright (c) 2011 Intel, Inc.
4 #
5 # This program is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by the Free
7 # Software Foundation; version 2 of the License
8 #
9 # This program is distributed in the hope that it will be useful, but
10 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 # or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 # for more details.
13 #
14 # You should have received a copy of the GNU General Public License along
15 # with this program; if not, write to the Free Software Foundation, Inc., 59
16 # Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17
18 import os
19 import subprocess
20
21 from mic import msger
22 from mic.utils import errors
23
24 def runtool(cmdln_or_args, catch=1):
25     """ wrapper for most of the subprocess calls
26     input:
27         cmdln_or_args: can be both args and cmdln str (shell=True)
28         catch: 0, quitely run
29                1, only STDOUT
30                2, only STDERR
31                3, both STDOUT and STDERR
32     return:
33         (rc, output)
34         if catch==0: the output will always None
35     """
36
37     if catch not in (0, 1, 2, 3):
38         # invalid catch selection, will cause exception, that's good
39         return None
40
41     if isinstance(cmdln_or_args, list):
42         cmd = cmdln_or_args[0]
43         shell = False
44     else:
45         import shlex
46         cmd = shlex.split(cmdln_or_args)[0]
47         shell = True
48
49     if catch != 3:
50         dev_null = os.open("/dev/null", os.O_WRONLY)
51
52     if catch == 0:
53         sout = dev_null
54         serr = dev_null
55     elif catch == 1:
56         sout = subprocess.PIPE
57         serr = dev_null
58     elif catch == 2:
59         sout = dev_null
60         serr = subprocess.PIPE
61     elif catch == 3:
62         sout = subprocess.PIPE
63         serr = subprocess.STDOUT
64
65     try:
66         p = subprocess.Popen(cmdln_or_args, stdout=sout,
67                              stderr=serr, shell=shell)
68         (sout, serr) = p.communicate()
69         # combine stdout and stderr, filter None out
70         out = ''.join(filter(None, [sout, serr]))
71     except OSError, e:
72         if e.errno == 2:
73             # [Errno 2] No such file or directory
74             raise errors.CreatorError('Cannot run command: %s, lost dependency?' % cmd)
75         else:
76             raise # relay
77     finally:
78         if catch != 3:
79             os.close(dev_null)
80
81     return (p.returncode, out)
82
83 def show(cmdln_or_args):
84     # show all the message using msger.verbose
85
86     rc, out = runtool(cmdln_or_args, catch=3)
87
88     if isinstance(cmdln_or_args, list):
89         cmd = ' '.join(cmdln_or_args)
90     else:
91         cmd = cmdln_or_args
92
93     msg =  'running command: "%s"' % cmd
94     if out: out = out.strip()
95     if out:
96         msg += ', with output::'
97         msg += '\n  +----------------'
98         for line in out.splitlines():
99             msg += '\n  | %s' % line
100         msg += '\n  +----------------'
101
102     msger.verbose(msg)
103     return rc
104
105 def outs(cmdln_or_args, catch=1):
106     # get the outputs of tools
107     return runtool(cmdln_or_args, catch)[1].strip()
108
109 def quiet(cmdln_or_args):
110     return runtool(cmdln_or_args, catch=0)[0]