1#!/usr/bin/env python3
2# coding=utf-8
3
4#
5# Copyright (c) 2020-2021 Huawei Device Co., Ltd.
6# Licensed under the Apache License, Version 2.0 (the "License");
7# you may not use this file except in compliance with the License.
8# You may obtain a copy of the License at
9#
10#     http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17#
18import datetime
19import os
20import signal
21import subprocess
22import time
23import logging
24import platform
25
26
27def get_decode(stream):
28    if isinstance(stream, str):
29        return stream
30
31    if not isinstance(stream, bytes):
32        return str(stream)
33
34    try:
35        ret = stream.decode("utf-8", errors="ignore")
36    except (ValueError, AttributeError, TypeError):
37        ret = str(stream)
38    return ret
39
40
41def exec_cmd(cmd, timeout=10, error_print=True, join_result=False, waitOut=False):
42
43    sys_type = platform.system()
44    if sys_type == "Linux" or sys_type == "Darwin":
45        proc = subprocess.Popen(cmd, stdout=subprocess.PIPE,
46                                stderr=subprocess.PIPE, shell=False,
47                                preexec_fn=os.setsid)
48    else:
49        proc = subprocess.Popen(cmd, stdout=subprocess.PIPE,
50                                stderr=subprocess.PIPE, shell=True)
51    if waitOut:
52        try:
53            (out, err) = proc.communicate(timeout=timeout)
54            err = get_decode(err).strip()
55            out = get_decode(out).strip()
56            if err and error_print:
57                logging.exception(err, exc_info=False)
58            if join_result:
59                return "%s\n %s" % (out, err) if err else out
60            else:
61                return err if err else out
62
63        except (TimeoutError, KeyboardInterrupt, AttributeError, ValueError,
64                EOFError, IOError):
65            sys_type = platform.system()
66            if sys_type == "Linux" or sys_type == "Darwin":
67                os.killpg(proc.pid, signal.SIGTERM)
68            else:
69                os.kill(proc.pid, signal.SIGINT)
70            raise
71