Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 import os
00012 import socket
00013 import sys
00014 import rosgraph
00015
00016
00017
00018
00019
00020
00021 class Console:
00022 bold = "\033[1m"
00023 reset = "\033[0;0m"
00024 red = "\033[31m"
00025
00026
00027 def red_string(msg):
00028 """bound string with console symbols for red output"""
00029 return Console.red + msg + Console.reset
00030
00031
00032 def bold_string(msg):
00033 """bound string with console symbols for bold output"""
00034 return Console.bold + msg + Console.reset
00035
00036
00037 def loginfo(message):
00038 print("[ INFO] " + message + "\n")
00039
00040
00041 def logerror(message):
00042 print(red_string("[ERROR] " + message))
00043
00044
00045 def logfatal(message):
00046 print(red_string("[FATAL] " + message))
00047
00048
00049
00050
00051
00052
00053 def check_master():
00054 """
00055 Make sure that master is available
00056 :raises: :exc:`ROSTopicException` If unable to successfully communicate with master
00057 """
00058 try:
00059 rosgraph.Master('dude').getPid()
00060 return True
00061 except socket.error:
00062 return False
00063
00064
00065
00066
00067
00068
00069 def which(program):
00070 '''
00071 Emulate in a cross platform way the linux shell command
00072
00073 @TODO: replace this with rocon_python_utils' tool
00074 '''
00075 def is_exe(fpath):
00076 return os.path.exists(fpath) and os.access(fpath, os.X_OK)
00077
00078 fpath, unused_fname = os.path.split(program)
00079 if fpath:
00080 if is_exe(program):
00081 return program
00082 else:
00083 for path in os.environ["PATH"].split(os.pathsep):
00084 exe_file = os.path.join(path, program)
00085 if is_exe(exe_file):
00086 return exe_file
00087 return None
00088
00089
00090 def check_if_executable_available(name):
00091 '''
00092 Ensure a particular executable is available on the system.
00093
00094 Could use package names and python-apt here to find if the package is
00095 available, but more reliable and general - just check if program binary
00096 is available.
00097
00098 Aborts program execution with fatal error if not found.
00099 '''
00100 if which(name) is None:
00101 sys.exit(logfatal("Hub : " + name + " not installed - hint 'rosdep install rocon_hub'."))
00102
00103
00104
00105
00106
00107
00108 def read_template(template_filename):
00109 '''
00110 Convenience function for opening a file.
00111 '''
00112 f = open(template_filename, 'r')
00113 try:
00114 t = f.read()
00115 finally:
00116 f.close()
00117 return t