takashi kadono / Mbed OS Nucleo_446

Dependencies:   ssd1331

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers utils.py Source File

utils.py

00001 """
00002 Copyright (c) 2018, Arm Limited
00003 SPDX-License-Identifier: Apache-2.0
00004 
00005 Licensed under the Apache License, Version 2.0 (the "License");
00006 you may not use this file except in compliance with the License.
00007 You may obtain a copy of the License at
00008 
00009     http://www.apache.org/licenses/LICENSE-2.0
00010 
00011 Unless required by applicable law or agreed to in writing, software
00012 distributed under the License is distributed on an "AS IS" BASIS,
00013 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014 See the License for the specific language governing permissions and
00015 limitations under the License.
00016 
00017 
00018 UNIT TEST UTILITIES
00019 """
00020 
00021 import platform
00022 import subprocess
00023 import logging
00024 import sys
00025 
00026 def is_tool (name):
00027     """
00028     Test if tool is found in PATH
00029 
00030     @param name: executable name
00031     @return: true if tool found, false otherwise
00032     """
00033     cmd = "where" if platform.system() == "Windows" else "which"
00034     try:
00035         subprocess.check_output([cmd, name], stderr=subprocess.STDOUT)
00036         return True
00037     except subprocess.CalledProcessError:
00038         return False
00039 
00040 def execute_program (args, error_msg="An error occurred!", success_msg=None):
00041     """
00042     Execute program in a subprocess with given arguments
00043 
00044     @param args: program and its arguments in a list
00045     @param success_msg: message to show in case of success
00046     @param error_msg: message to show in case of failure
00047     """
00048 
00049     try:
00050         process = subprocess.Popen(args,
00051                                    stdout=subprocess.PIPE,
00052                                    stderr=subprocess.STDOUT)
00053 
00054         while process.poll() is None:
00055             logging.info(process.stdout.readline().decode("utf8"))
00056 
00057         retcode = process.wait()
00058 
00059         if retcode:
00060             raise subprocess.CalledProcessError(retcode, args)
00061         elif success_msg:
00062             logging.info(success_msg)
00063 
00064     except subprocess.CalledProcessError as error:
00065         logging.error(error_msg)
00066         sys.exit(error.returncode)