• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
2#coding=utf-8
3
4#
5# Copyright (c) 2022 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#
18
19import os
20import sys
21import string
22
23DEBUG_NORMAL = 1
24DEBUG_VERBOSE = 2
25DEBUG_SPAM = 3
26
27debuglevel = DEBUG_NORMAL
28
29
30def debug(level, *msg):
31    if debuglevel >= level:
32        print(' '.join(msg))
33
34
35# return a list of lines of output of the command
36def command(command, *args):
37    debug(DEBUG_SPAM, "calling", command, ' '.join(args))
38    pipe = os.popen(command + ' ' + ' '.join(args), 'r')
39    output = pipe.read().strip()
40    status = pipe.close()
41    if status is not None and os.WEXITSTATUS(status) != 0:
42        print("Command failed with status", os.WEXITSTATUS(status), ":", \
43               command, ' '.join(args))
44        print("With output:", output)
45        sys.exit(1)
46    return [i for i in output.split('\n') if i]
47