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


rocon_gateway_hub
Author(s): Daniel Stonier, Jihoon Lee
autogenerated on Fri Nov 30 2012 17:46:28