def test_get_etc_ros_dir(): from rospkg import get_etc_ros_dir, get_ros_root from rospkg.environment import ROS_ETC_DIR base = tempfile.gettempdir() etc_ros_dir = os.path.join(base, 'etc_ros_dir') assert '/etc/ros' == get_etc_ros_dir(env={}) # ROS_ETC_DIR has precedence env = {ROS_ETC_DIR: etc_ros_dir} assert etc_ros_dir == get_etc_ros_dir(env=env), get_etc_ros_dir(env=env) # test default assignment of env. Don't both checking return value as we would duplicate get_etc_ros_dir assert get_etc_ros_dir() is not None
def get_roscore_filename(): # precedence: look for version in /etc/ros. If it's not there, fall back to roslaunch package filename = os.path.join(rospkg.get_etc_ros_dir(), 'roscore.xml') if os.path.isfile(filename): return filename r = rospkg.RosPack() return os.path.join(r.get_path('roslaunch'), 'resources', 'roscore.xml')
def get_sources_list_dir(): # base of where we read config files from # TODO: windows if 0: # we can't use etc/ros because environment config does not carry over under sudo etc_ros = rospkg.get_etc_ros_dir() else: etc_ros = '/etc/ros' # compute cache directory return os.path.join(etc_ros, 'rosdep', SOURCES_LIST_DIR)
def get_sources_list_dir(): # base of where we read config files from # TODO: windows etc_ros = rospkg.get_etc_ros_dir() # compute default system wide sources directory sys_sources_list_dir = os.path.join(etc_ros, 'rosdep', SOURCES_LIST_DIR) sources_list_dirs = get_sources_list_dirs(sys_sources_list_dir) if sources_list_dirs: return sources_list_dirs[0] else: return sys_sources_list_dir
def get_sources_list_dir(): # base of where we read config files from if sys.platform in ['win32']: etc_ros = rospkg.get_etc_ros_dir() else: etc_ros = '/etc/ros' # compute default system wide sources directory sys_sources_list_dir = os.path.join(etc_ros, 'rosdep', SOURCES_LIST_DIR) sources_list_dirs = get_sources_list_dirs(sys_sources_list_dir) if sources_list_dirs: return sources_list_dirs[0] else: return sys_sources_list_dir
def get_sources_list_dir(): # base of where we read config files from # TODO: windows if 0: # we can't use etc/ros because environment config does not carry over under sudo etc_ros = rospkg.get_etc_ros_dir() else: etc_ros = '/etc/ros' # compute default system wide sources directory sys_sources_list_dir = os.path.join(etc_ros, 'rosdep', SOURCES_LIST_DIR) sources_list_dirs = get_sources_list_dirs(sys_sources_list_dir) if sources_list_dirs: return sources_list_dirs[0] else: return sys_sources_list_dir
def configure_logging(logname, level=logging.INFO, filename=None, env=None): """ Configure Python logging package to send log files to ROS-specific log directory :param logname str: name of logger, ``str`` :param filename: filename to log to. If not set, a log filename will be generated using logname, ``str`` :param env: override os.environ dictionary, ``dict`` :returns: log file name, ``str`` :raises: :exc:`LoggingException` If logging cannot be configured as specified """ if env is None: env = os.environ logname = logname or 'unknown' log_dir = rospkg.get_log_dir(env=env) # if filename is not explicitly provided, generate one using logname if not filename: log_filename = os.path.join(log_dir, '%s-%s.log' % (logname, os.getpid())) else: log_filename = os.path.join(log_dir, filename) logfile_dir = os.path.dirname(log_filename) if not os.path.exists(logfile_dir): try: makedirs_with_parent_perms(logfile_dir) except OSError: # cannot print to screen because command-line tools with output use this if os.path.exists(logfile_dir): # We successfully created the logging folder, but could not change # permissions of the new folder to the same as the parent folder sys.stderr.write( "WARNING: Could not change permissions for folder [%s], make sure that the parent folder has correct permissions.\n" % logfile_dir) else: # Could not create folder sys.stderr.write( "WARNING: cannot create log directory [%s]. Please set %s to a writable location.\n" % (logfile_dir, ROS_LOG_DIR)) return None elif os.path.isfile(logfile_dir): raise LoggingException( "Cannot save log files: file [%s] is in the way" % logfile_dir) # the log dir itself should not be symlinked as latest if logfile_dir != log_dir: if sys.platform not in ['win32']: try: success = renew_latest_logdir(logfile_dir) if not success: sys.stderr.write( "INFO: cannot create a symlink to latest log directory\n" ) except OSError as e: sys.stderr.write( "INFO: cannot create a symlink to latest log directory: %s\n" % e) config_file = os.environ.get('ROS_PYTHON_LOG_CONFIG_FILE') if not config_file: # search for logging config file in ROS_HOME, ROS_ETC_DIR or relative # to the rosgraph package directory. rosgraph_d = rospkg.RosPack().get_path('rosgraph') for config_dir in [ os.path.join(rospkg.get_ros_home(), 'config'), rospkg.get_etc_ros_dir(), os.path.join(rosgraph_d, 'conf') ]: for extension in ('conf', 'yaml'): f = os.path.join( config_dir, 'python_logging{}{}'.format(os.path.extsep, extension)) if os.path.isfile(f): config_file = f break if config_file is not None: break if config_file is None or not os.path.isfile(config_file): # logging is considered soft-fail sys.stderr.write( "WARNING: cannot load logging configuration file, logging is disabled\n" ) logging.getLogger(logname).setLevel(logging.CRITICAL) return log_filename # pass in log_filename as argument to pylogging.conf os.environ['ROS_LOG_FILENAME'] = log_filename if config_file.endswith(('.yaml', '.yml')): with open(config_file) as f: dict_conf = yaml.load(f) dict_conf.setdefault('version', 1) logging.config.dictConfig(dict_conf) else: # #3625: disabling_existing_loggers=False logging.config.fileConfig(config_file, disable_existing_loggers=False) return log_filename