2 # Copyright (c) 2000 - 2016 Samsung Electronics Co., Ltd. All rights reserved.
5 # @author Chulwoo Shin <cw1.shin@samsung.com>
7 # Licensed under the Apache License, Version 2.0 (the "License");
8 # you may not use this file except in compliance with the License.
9 # You may obtain a copy of the License at
11 # http://www.apache.org/licenses/LICENSE-2.0
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
26 from tic.utils.error import TICError
28 def run(cmdln, catch=3):
29 logger = logging.getLogger(__name__)
30 if isinstance(cmdln, list):
35 cmd = shlex.split(cmdln)[0]
41 dev_null = os.open("/dev/null", os.O_WRONLY)
49 # both STDOUT and STDERR
50 sout = subprocess.PIPE
51 serr = subprocess.STDOUT
54 logger.info('subprocess open: %s', cmd)
55 p = subprocess.Popen(cmdln,
60 (sout, serr) = p.communicate()
61 out = ''.join(filter(None, [sout, serr]))
64 # No such file or directory
65 raise TICError('Cannot run command: %s' % cmd)
72 return (p.returncode, out)