utils.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 #
3 # License: BSD
4 # https://raw.github.com/robotics-in-concert/rocon_multimaster/license/LICENSE
5 #
6 
7 ##############################################################################
8 # Imports
9 ##############################################################################
10 
11 import os
12 import socket
13 import sys
14 import rosgraph
15 import rocon_console.console as console
16 
17 ##############################################################################
18 # Logging
19 ##############################################################################
20 
21 
22 def loginfo(message):
23  print("[ INFO] " + message)
24 
25 
26 def logwarn(message):
27  print(console.yellow + ("[ WARN] " + message) + console.reset)
28 
29 
30 def logerror(message):
31  print(console.red + ("[ERROR] " + message) + console.reset)
32 
33 
34 def logfatal(message):
35  print(console.red + ("[FATAL] " + message) + console.reset)
36 
37 
38 ##############################################################################
39 # Ros
40 ##############################################################################
41 
43  """
44  Make sure that master is available
45  :raises: :exc:`ROSTopicException` If unable to successfully communicate with master
46  """
47  try:
48  rosgraph.Master('dude').getPid()
49  return True
50  except socket.error:
51  return False
52 
53 ##############################################################################
54 # System
55 ##############################################################################
56 
57 
58 def which(program):
59  '''
60  Emulate in a cross platform way the linux shell command
61 
62  @TODO: replace this with rocon_python_utils' tool
63  '''
64  def is_exe(fpath):
65  return os.path.exists(fpath) and os.access(fpath, os.X_OK)
66 
67  fpath, unused_fname = os.path.split(program)
68  if fpath:
69  if is_exe(program):
70  return program
71  else:
72  for path in os.environ["PATH"].split(os.pathsep):
73  exe_file = os.path.join(path, program)
74  if is_exe(exe_file):
75  return exe_file
76  return None
77 
78 
80  '''
81  Ensure a particular executable is available on the system.
82 
83  Could use package names and python-apt here to find if the package is
84  available, but more reliable and general - just check if program binary
85  is available.
86 
87  Deprecated - aborts program execution with fatal error if not found.
88  '''
89  if which(name + 'z') is None:
90  logwarn("Hub : " + name + " not found")
91  logwarn("Hub : either you can't look up the admin PATH (ok)")
92  logwarn("Hub : or it is not installed - hint 'rosdep install rocon_hub'")
93 
94 ##############################################################################
95 # File Handling
96 ##############################################################################
97 
98 
99 def read_template(template_filename):
100  '''
101  Convenience function for opening a file.
102  '''
103  f = open(template_filename, 'r')
104  try:
105  t = f.read()
106  finally:
107  f.close()
108  return t
def logerror(message)
Definition: utils.py:30
def check_if_executable_available(name)
Definition: utils.py:79
def loginfo(message)
Logging.
Definition: utils.py:22
def logwarn(message)
Definition: utils.py:26
def logfatal(message)
Definition: utils.py:34
def check_master()
Ros.
Definition: utils.py:42
def which(program)
System.
Definition: utils.py:58
def read_template(template_filename)
File Handling.
Definition: utils.py:99


rocon_hub
Author(s): Daniel Stonier , Jihoon Lee , Piyush Khandelwal
autogenerated on Mon Jun 10 2019 14:40:13