def _parseRosVersionSetupBash(version, onlyDifferent=True): """ Load the setup.bash file in a bash terminal, and import the env variables into a dict """ """ onlyDifferent==True(default): Compare this dict to the current env vars and only return """ """ Variables that are new or different in the ros bash terminal """ if not ROS._envVars.has_key(version): # executes the bash script and exports env vars bashScript = '/opt/ros/%s/setup.bash' % version diffEnv = {} if os.path.exists(bashScript): rosEnv = ROS._parseBashEnviron('source ' + bashScript) baseEnv = ROS._parseBashEnviron() # find all the variables that ros added/changed for key, value in rosEnv.items(): if not baseEnv.has_key(key): diffEnv[key] = value elif baseEnv[key] != value: # We really only want the bit that ros added diffEnv[key] = value.replace(baseEnv[key], '').strip(':') # Add in any overrides from the config file if ros_config.has_key('envVars'): diffEnv.update(ros_config['envVars']) rosEnv.update(ros_config['envVars']) else: print >> sys.stderr, "Unable to read ros bash script, file not found: %s" % bashScript ROS._envVars[version] = (diffEnv, rosEnv) if onlyDifferent: return ROS._envVars[version][0] else: return ROS._envVars[version][1]
def parseRosBash(version=None, onlyDifferent=True): version = version or ros_config['version'] if not ROS._envVars.has_key(version): # executes the bash script and exports env vars bashScript = '/opt/ros/%s/setup.bash' % version diffEnv = {} if os.path.exists(bashScript): rosEnv = ROS.parseBashEnviron('source ' + bashScript) baseEnv = ROS.parseBashEnviron() # find all the variables that ros added/changed for key, value in rosEnv.items(): if not baseEnv.has_key(key): diffEnv[key] = value elif baseEnv[key] != value: # We really only want the bit that ros added diffEnv[key] = value.replace(baseEnv[key], '').strip(':') # Add in any overrides from the config file if ros_config.has_key('envVars'): diffEnv.update(ros_config['envVars']) rosEnv.update(ros_config['envVars']) ROS._envVars[version] = (diffEnv, rosEnv) if onlyDifferent: return ROS._envVars[version][0] else: return ROS._envVars[version][1]
def configureROS(version=None, packagePath=None, packageName=None, rosMaster=None, overlayPath=None): """Any values not provided will be read from ros_config in config.py""" version = version or ros_config['version'] overlayPath = overlayPath or ros_config['overlayPath'] if(rosMaster == None and ros_config.has_key('rosMaster')): rosMaster = ros_config['rosMaster'] for k, v in ROS.parseRosBash(version).items(): if k == 'PYTHONPATH' and sys.path.count(v) == 0: sys.path.append(v) elif not os.environ.has_key(k): os.environ[k] = v elif k.endswith('PATH') and os.environ[k].find(v) == -1: os.environ[k] = ':'.join((v, os.environ[k])) # if 'ROS_MASTER_URI' not in os.environ.keys(): if rosMaster != None: os.environ['ROS_MASTER_URI'] = rosMaster path = '/opt/ros/%(version)s/ros' % { 'version': version} if 'ROS_ROOT' not in os.environ.keys() or os.environ['ROS_ROOT'] != path: os.environ['ROS_ROOT'] = path path = '%(root)s/bin' % { 'root': os.environ['ROS_ROOT']} if os.environ['PATH'].find(path) == -1: os.environ['PATH'] = ':'.join((path, os.environ['PATH'])) path = '/opt/ros/%(version)s/stacks' % { 'version': version} if 'ROS_PACKAGE_PATH' not in os.environ.keys(): os.environ['ROS_PACKAGE_PATH'] = path elif os.environ['ROS_PACKAGE_PATH'].find(path) == -1: os.environ['ROS_PACKAGE_PATH'] = ':'.join((path, os.environ['ROS_PACKAGE_PATH'])) path = os.path.expanduser(overlayPath) if os.environ['ROS_PACKAGE_PATH'].find(path) == -1: os.environ['ROS_PACKAGE_PATH'] = ':'.join((path, os.environ['ROS_PACKAGE_PATH'])) path = packagePath or os.path.dirname(os.path.realpath(__file__)) + '/ROS_Packages' if os.environ['ROS_PACKAGE_PATH'].find(path) == -1: os.environ['ROS_PACKAGE_PATH'] = ':'.join((path, os.environ['ROS_PACKAGE_PATH'])) path = '%(root)s/core/roslib/src' % { 'root': os.environ['ROS_ROOT']} if sys.path.count(path) == 0: sys.path.append(path) if packageName != None: import roslib roslib.load_manifest(packageName)
def configureROS(version=None, packagePath=None, packageName=None, rosMaster=None, overlayPath=None): """ Perform all tasks necessary to configure ROS interface for use. All variables not specified """ """ are detected from first the ros_config(if specified) or from the users .bashrc """ """ Most common use is configureRos(packageName='name') in order to import a ros package for use""" """ Can be called multiple times without issue """ if version == None: if not ros_config.has_key('version'): version = ROS._locateRosVersion() else: version = ros_config['version'] if overlayPath == None: if not ros_config.has_key('overlayPath'): overlayPath = ROS._locateRosOverlayPath() else: overlayPath = ros_config['overlayPath'] if(rosMaster == None and ros_config.has_key('rosMaster')): rosMaster = ros_config['rosMaster'] for k, v in ROS._parseRosVersionSetupBash(version).items(): if k == 'PYTHONPATH' and sys.path.count(v) == 0: sys.path.append(v) elif not os.environ.has_key(k): os.environ[k] = v elif k.endswith('PATH') and os.environ[k].find(v) == -1: os.environ[k] = ':'.join((v, os.environ[k])) # if 'ROS_MASTER_URI' not in os.environ.keys(): if rosMaster != None: os.environ['ROS_MASTER_URI'] = rosMaster path = '/opt/ros/%(version)s/ros' % { 'version': version} if 'ROS_ROOT' not in os.environ.keys() or os.environ['ROS_ROOT'] != path: os.environ['ROS_ROOT'] = path path = '%(root)s/bin' % { 'root': os.environ['ROS_ROOT']} if os.environ['PATH'].find(path) == -1: os.environ['PATH'] = ':'.join((path, os.environ['PATH'])) path = '/opt/ros/%(version)s/stacks' % { 'version': version} if 'ROS_PACKAGE_PATH' not in os.environ.keys(): os.environ['ROS_PACKAGE_PATH'] = path elif os.environ['ROS_PACKAGE_PATH'].find(path) == -1: os.environ['ROS_PACKAGE_PATH'] = ':'.join((path, os.environ['ROS_PACKAGE_PATH'])) if overlayPath != None: for path in overlayPath.split(':'): path = os.path.expanduser(path) if os.environ['ROS_PACKAGE_PATH'].find(path) == -1: os.environ['ROS_PACKAGE_PATH'] = ':'.join((path, os.environ['ROS_PACKAGE_PATH'])) path = packagePath or os.path.dirname(os.path.realpath(__file__)) + '/ROS_Packages' if os.environ['ROS_PACKAGE_PATH'].find(path) == -1: os.environ['ROS_PACKAGE_PATH'] = ':'.join((path, os.environ['ROS_PACKAGE_PATH'])) path = '%(root)s/core/roslib/src' % { 'root': os.environ['ROS_ROOT']} if sys.path.count(path) == 0: sys.path.append(path) if packageName != None: with _threadLock: import roslib try: roslib.load_manifest(packageName) except Exception as e: print >> sys.stderr, "Error loading package: %s" % packageName print >> sys.stderr, repr(e)