remove unsued variable
[tools/mic.git] / mic / utils / runner.py
1 #!/usr/bin/python3 -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 == 0:
50         sout = subprocess.DEVNULL
51         serr = subprocess.DEVNULL
52     elif catch == 1:
53         sout = subprocess.PIPE
54         serr = subprocess.DEVNULL
55     elif catch == 2:
56         sout = subprocess.DEVNULL
57         serr = subprocess.PIPE
58     elif catch == 3:
59         sout = subprocess.PIPE
60         serr = subprocess.STDOUT
61
62     try:
63         p = subprocess.Popen(cmdln_or_args, stdout=sout,
64                              stderr=serr, shell=shell, universal_newlines=True)
65         (sout, serr) = p.communicate()
66         # combine stdout and stderr, filter None out
67         out = ''.join([_f for _f in [sout, serr] if _f])
68     except OSError as e:
69         if e.errno == 2:
70             # [Errno 2] No such file or directory
71             raise errors.CreatorError('Cannot run command: %s, lost dependency?' % cmd)
72         else:
73             raise # relay
74     finally:
75         pass
76
77     return (p.returncode, out)
78
79 def show(cmdln_or_args):
80     # show all the message using msger.verbose
81
82     rc, out = runtool(cmdln_or_args, catch=3)
83
84     if isinstance(cmdln_or_args, list):
85         cmd = ' '.join(cmdln_or_args)
86     else:
87         cmd = cmdln_or_args
88
89     msg =  'running command: "%s"' % cmd
90     if out: out = out.strip()
91     if out:
92         msg += ', with output::'
93         msg += '\n  +----------------'
94         for line in out.splitlines():
95             msg += '\n  | %s' % line
96         msg += '\n  +----------------'
97
98     msger.verbose(msg)
99     return rc
100
101 def outs(cmdln_or_args, catch=1):
102     # get the outputs of tools
103     return runtool(cmdln_or_args, catch)[1].strip()
104
105 def quiet(cmdln_or_args):
106     return runtool(cmdln_or_args, catch=0)[0]