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 roslib
00015 roslib.load_manifest('rocon_hub')
00016 import rosgraph
00017 
00018 
00019 
00020 
00021 
00022 
00023 class Console:
00024     bold = "\033[1m"
00025     reset = "\033[0;0m"
00026     red = "\033[31m"
00027 
00028 
00029 def red_string(msg):
00030     """bound string with console symbols for red output"""
00031     return Console.red + msg + Console.reset
00032 
00033 
00034 def bold_string(msg):
00035     """bound string with console symbols for bold output"""
00036     return Console.bold + msg + Console.reset
00037 
00038 
00039 def loginfo(message):
00040     print("[ INFO] " + message + "\n")
00041 
00042 
00043 def logerror(message):
00044     print(red_string("[ERROR] " + message))
00045 
00046 
00047 def logfatal(message):
00048     print(red_string("[FATAL] " + message))
00049 
00050 
00051 
00052 
00053 
00054 
00055 def check_master():
00056     """
00057     Make sure that master is available
00058     :raises: :exc:`ROSTopicException` If unable to successfully communicate with master
00059     """
00060     try:
00061         rosgraph.Master('dude').getPid()
00062         return True
00063     except socket.error:
00064         return False
00065 
00066 
00067 
00068 
00069 
00070 
00071 def which(program):
00072     '''
00073     Emulate in a cross platform way the linux shell command
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