131c75014Sopenharmony_ci#!/usr/bin/env python3
231c75014Sopenharmony_ci# coding=utf-8
331c75014Sopenharmony_ci
431c75014Sopenharmony_ci#
531c75014Sopenharmony_ci# Copyright (c) 2020-2021 Huawei Device Co., Ltd.
631c75014Sopenharmony_ci# Licensed under the Apache License, Version 2.0 (the "License");
731c75014Sopenharmony_ci# you may not use this file except in compliance with the License.
831c75014Sopenharmony_ci# You may obtain a copy of the License at
931c75014Sopenharmony_ci#
1031c75014Sopenharmony_ci#     http://www.apache.org/licenses/LICENSE-2.0
1131c75014Sopenharmony_ci#
1231c75014Sopenharmony_ci# Unless required by applicable law or agreed to in writing, software
1331c75014Sopenharmony_ci# distributed under the License is distributed on an "AS IS" BASIS,
1431c75014Sopenharmony_ci# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1531c75014Sopenharmony_ci# See the License for the specific language governing permissions and
1631c75014Sopenharmony_ci# limitations under the License.
1731c75014Sopenharmony_ci#
1831c75014Sopenharmony_ciimport datetime
1931c75014Sopenharmony_ciimport os
2031c75014Sopenharmony_ciimport signal
2131c75014Sopenharmony_ciimport subprocess
2231c75014Sopenharmony_ciimport time
2331c75014Sopenharmony_ciimport logging
2431c75014Sopenharmony_ciimport platform
2531c75014Sopenharmony_ci
2631c75014Sopenharmony_ci
2731c75014Sopenharmony_cidef get_decode(stream):
2831c75014Sopenharmony_ci    if isinstance(stream, str):
2931c75014Sopenharmony_ci        return stream
3031c75014Sopenharmony_ci
3131c75014Sopenharmony_ci    if not isinstance(stream, bytes):
3231c75014Sopenharmony_ci        return str(stream)
3331c75014Sopenharmony_ci
3431c75014Sopenharmony_ci    try:
3531c75014Sopenharmony_ci        ret = stream.decode("utf-8", errors="ignore")
3631c75014Sopenharmony_ci    except (ValueError, AttributeError, TypeError):
3731c75014Sopenharmony_ci        ret = str(stream)
3831c75014Sopenharmony_ci    return ret
3931c75014Sopenharmony_ci
4031c75014Sopenharmony_ci
4131c75014Sopenharmony_cidef exec_cmd(cmd, timeout=10, error_print=True, join_result=False, waitOut=False):
4231c75014Sopenharmony_ci
4331c75014Sopenharmony_ci    sys_type = platform.system()
4431c75014Sopenharmony_ci    if sys_type == "Linux" or sys_type == "Darwin":
4531c75014Sopenharmony_ci        proc = subprocess.Popen(cmd, stdout=subprocess.PIPE,
4631c75014Sopenharmony_ci                                stderr=subprocess.PIPE, shell=False,
4731c75014Sopenharmony_ci                                preexec_fn=os.setsid)
4831c75014Sopenharmony_ci    else:
4931c75014Sopenharmony_ci        proc = subprocess.Popen(cmd, stdout=subprocess.PIPE,
5031c75014Sopenharmony_ci                                stderr=subprocess.PIPE, shell=True)
5131c75014Sopenharmony_ci    if waitOut:
5231c75014Sopenharmony_ci        try:
5331c75014Sopenharmony_ci            (out, err) = proc.communicate(timeout=timeout)
5431c75014Sopenharmony_ci            err = get_decode(err).strip()
5531c75014Sopenharmony_ci            out = get_decode(out).strip()
5631c75014Sopenharmony_ci            if err and error_print:
5731c75014Sopenharmony_ci                logging.exception(err, exc_info=False)
5831c75014Sopenharmony_ci            if join_result:
5931c75014Sopenharmony_ci                return "%s\n %s" % (out, err) if err else out
6031c75014Sopenharmony_ci            else:
6131c75014Sopenharmony_ci                return err if err else out
6231c75014Sopenharmony_ci
6331c75014Sopenharmony_ci        except (TimeoutError, KeyboardInterrupt, AttributeError, ValueError,
6431c75014Sopenharmony_ci                EOFError, IOError):
6531c75014Sopenharmony_ci            sys_type = platform.system()
6631c75014Sopenharmony_ci            if sys_type == "Linux" or sys_type == "Darwin":
6731c75014Sopenharmony_ci                os.killpg(proc.pid, signal.SIGTERM)
6831c75014Sopenharmony_ci            else:
6931c75014Sopenharmony_ci                os.kill(proc.pid, signal.SIGINT)
7031c75014Sopenharmony_ci            raise
71