1ba991379Sopenharmony_ci#!/usr/bin/env python
2ba991379Sopenharmony_ci#coding=utf-8
3ba991379Sopenharmony_ci
4ba991379Sopenharmony_ci#
5ba991379Sopenharmony_ci# Copyright (c) 2022 Huawei Device Co., Ltd.
6ba991379Sopenharmony_ci# Licensed under the Apache License, Version 2.0 (the "License");
7ba991379Sopenharmony_ci# you may not use this file except in compliance with the License.
8ba991379Sopenharmony_ci# You may obtain a copy of the License at
9ba991379Sopenharmony_ci#
10ba991379Sopenharmony_ci#     http://www.apache.org/licenses/LICENSE-2.0
11ba991379Sopenharmony_ci#
12ba991379Sopenharmony_ci# Unless required by applicable law or agreed to in writing, software
13ba991379Sopenharmony_ci# distributed under the License is distributed on an "AS IS" BASIS,
14ba991379Sopenharmony_ci# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15ba991379Sopenharmony_ci# See the License for the specific language governing permissions and
16ba991379Sopenharmony_ci# limitations under the License.
17ba991379Sopenharmony_ci#
18ba991379Sopenharmony_ci
19ba991379Sopenharmony_ciimport os
20ba991379Sopenharmony_ciimport sys
21ba991379Sopenharmony_ciimport string
22ba991379Sopenharmony_ci
23ba991379Sopenharmony_ciDEBUG_NORMAL = 1
24ba991379Sopenharmony_ciDEBUG_VERBOSE = 2
25ba991379Sopenharmony_ciDEBUG_SPAM = 3
26ba991379Sopenharmony_ci
27ba991379Sopenharmony_cidebuglevel = DEBUG_NORMAL
28ba991379Sopenharmony_ci
29ba991379Sopenharmony_ci
30ba991379Sopenharmony_cidef debug(level, *msg):
31ba991379Sopenharmony_ci    if debuglevel >= level:
32ba991379Sopenharmony_ci        print(' '.join(msg))
33ba991379Sopenharmony_ci
34ba991379Sopenharmony_ci
35ba991379Sopenharmony_ci# return a list of lines of output of the command
36ba991379Sopenharmony_cidef command(command, *args):
37ba991379Sopenharmony_ci    debug(DEBUG_SPAM, "calling", command, ' '.join(args))
38ba991379Sopenharmony_ci    pipe = os.popen(command + ' ' + ' '.join(args), 'r')
39ba991379Sopenharmony_ci    output = pipe.read().strip()
40ba991379Sopenharmony_ci    status = pipe.close()
41ba991379Sopenharmony_ci    if status is not None and os.WEXITSTATUS(status) != 0:
42ba991379Sopenharmony_ci        print("Command failed with status", os.WEXITSTATUS(status), ":", \
43ba991379Sopenharmony_ci               command, ' '.join(args))
44ba991379Sopenharmony_ci        print("With output:", output)
45ba991379Sopenharmony_ci        sys.exit(1)
46ba991379Sopenharmony_ci    return [i for i in output.split('\n') if i]
47