def test_setup_env(self): from roslaunch.core import setup_env, Node, Machine master_uri = 'http://masteruri:1234' n = Node('nodepkg', 'nodetype') m = Machine('name1', '1.2.3.4') d = setup_env(n, m, master_uri) self.assertEquals(d['ROS_MASTER_URI'], master_uri) m = Machine('name1', '1.2.3.4') d = setup_env(n, m, master_uri) val = os.environ['ROS_ROOT'] self.assertEquals(d['ROS_ROOT'], val) # test ROS_NAMESPACE # test stripping n = Node('nodepkg', 'nodetype', namespace="/ns2/") d = setup_env(n, m, master_uri) self.assertEquals(d['ROS_NAMESPACE'], "/ns2") # test node.env_args n = Node('nodepkg', 'nodetype', env_args=[('NENV1', 'val1'), ('NENV2', 'val2'), ('ROS_ROOT', '/new/root')]) d = setup_env(n, m, master_uri) self.assertEquals(d['ROS_ROOT'], "/new/root") self.assertEquals(d['NENV1'], "val1") self.assertEquals(d['NENV2'], "val2")
def test_setup_env(self): from roslaunch.core import setup_env, Node, Machine master_uri = 'http://masteruri:1234' n = Node('nodepkg','nodetype') m = Machine('name1', '1.2.3.4') d = setup_env(n, m, master_uri) self.assertEquals(d['ROS_MASTER_URI'], master_uri) m = Machine('name1', '1.2.3.4') d = setup_env(n, m, master_uri) val = os.environ['ROS_ROOT'] self.assertEquals(d['ROS_ROOT'], val) # test ROS_NAMESPACE # test stripping n = Node('nodepkg','nodetype', namespace="/ns2/") d = setup_env(n, m, master_uri) self.assertEquals(d['ROS_NAMESPACE'], "/ns2") # test node.env_args n = Node('nodepkg','nodetype', env_args=[('NENV1', 'val1'), ('NENV2', 'val2'), ('ROS_ROOT', '/new/root')]) d = setup_env(n, m, master_uri) self.assertEquals(d['ROS_ROOT'], "/new/root") self.assertEquals(d['NENV1'], "val1") self.assertEquals(d['NENV2'], "val2")
def create_local_process_env(node, machine, master_uri, env=os.environ): """ Setup environment for locally launched process. The local environment includes the default os environment, with any ROS-specific environment variables overriding this enviornment. @return: environment variables @rtype: dict """ # #1029: generate environment for the node. unset # #ROS-related environment vars before # update() so that extra environment variables don't end # up in the call. full_env = env.copy() for evar in [ roslib.rosenv.ROS_MASTER_URI, roslib.rosenv.ROS_ROOT, roslib.rosenv.ROS_PACKAGE_PATH, roslib.rosenv.ROS_IP, 'PYTHONPATH', roslib.rosenv.ROS_NAMESPACE]: if evar in full_env: del full_env[evar] proc_env = setup_env(node, machine, master_uri) # #2372: add ROS_ROOT/bin to path if it is not present rosroot_bin = os.path.join(roslib.rosenv.get_ros_root(), 'bin') path = os.environ.get('PATH', '') if not rosroot_bin in path.split(os.pathsep): proc_env['PATH'] = path + os.pathsep + rosroot_bin full_env.update(proc_env) return full_env
def get_node_args(node_name, roslaunch_files): """ Get the node arguments for a node in roslaunch_files. @param node_name: name of node in roslaunch_files. @type node_name: str @param roslaunch_files: roslaunch file names @type roslaunch_files: [str] @return: list of command-line arguments used to launch node_name @rtype: [str] @raise RLException: if node args cannot be retrieved """ # we have to create our own XmlLoader so that we can use the same # resolution context for substitution args loader = roslaunch.xmlloader.XmlLoader(resolve_anon=False) config = load_config_default(roslaunch_files, None, loader=loader, verbose=False, assign_machines=False) (node_name) = substitution_args.resolve_args((node_name), resolve_anon=False) node_name = script_resolve_name( 'roslaunch', node_name) if not node_name.startswith('$') else node_name node = [n for n in config.nodes if _resolved_name(n) == node_name] + \ [n for n in config.tests if _resolved_name(n) == node_name] if not node: node_list = get_node_list(config) node_list_str = '\n'.join([" * %s" % x for x in node_list]) raise RLException( "ERROR: Cannot find node named [%s] in [%s].\nNode names are:\n%s" % (node_name, ', '.join(roslaunch_files), node_list_str)) elif len(node) > 1: raise RLException( "ERROR: multiple nodes named [%s] in [%s].\nPlease fix the launch files as duplicate names are not allowed." % (node_name, ', '.join(roslaunch_files))) node = node[0] master_uri = rosgraph.get_master_uri() machine = local_machine() env = setup_env(node, machine, master_uri) # remove setting identical to current environment for easier debugging to_remove = [] for k in env.keys(): if env[k] == os.environ.get(k, None): to_remove.append(k) for k in to_remove: del env[k] # resolve node name for generating args args = create_local_process_args(node, machine) # join environment vars are bash prefix args return ["%s=%s" % (k, v) for k, v in env.items()] + args
def get_node_args(node_name, roslaunch_files): """ Get the node arguments for a node in roslaunch_files. @param node_name: name of node in roslaunch_files. @type node_name: str @param roslaunch_files: roslaunch file names @type roslaunch_files: [str] @return: list of command-line arguments used to launch node_name @rtype: [str] @raise RLException: if node args cannot be retrieved """ # we have to create our own XmlLoader so that we can use the same # resolution context for substitution args loader = roslaunch.xmlloader.XmlLoader(resolve_anon=False) config = load_config_default(roslaunch_files, None, loader=loader, verbose=False, assign_machines=False) (node_name) = substitution_args.resolve_args((node_name), resolve_anon=False) node_name = script_resolve_name("roslaunch", node_name) if not node_name.startswith("$") else node_name node = [n for n in config.nodes if _resolved_name(n) == node_name] + [ n for n in config.tests if _resolved_name(n) == node_name ] if not node: node_list = get_node_list(config) node_list_str = "\n".join([" * %s" % x for x in node_list]) raise RLException( "ERROR: Cannot find node named [%s] in [%s].\nNode names are:\n%s" % (node_name, ", ".join(roslaunch_files), node_list_str) ) elif len(node) > 1: raise RLException( "ERROR: multiple nodes named [%s] in [%s].\nPlease fix the launch files as duplicate names are not allowed." % (node_name, ", ".join(roslaunch_files)) ) node = node[0] master_uri = rosgraph.get_master_uri() machine = local_machine() env = setup_env(node, machine, master_uri) # remove setting identical to current environment for easier debugging to_remove = [] for k in env.iterkeys(): if env[k] == os.environ.get(k, None): to_remove.append(k) for k in to_remove: del env[k] # resolve node name for generating args args = create_local_process_args(node, machine) # join environment vars are bash prefix args return ["%s=%s" % (k, v) for k, v in env.iteritems()] + args
def _start_child(self, server_node_uri, machine, counter): # generate a name for the machine. don't use config key as # it's too long to easily display name = "%s-%s"%(machine.address, counter) self.logger.info("remote[%s] starting roslaunch", name) print "remote[%s] starting roslaunch"%name env_dict = setup_env(None, machine, self.rosconfig.master.uri) p = SSHChildROSLaunchProcess(self.run_id, name, server_node_uri, env_dict, machine) success = p.start() self.pm.register(p) if not success: #treat as fatal raise RLException("unable to start remote roslaunch child: %s"%name) self.server.add_child(name, p) return p
if __name__ == '__main__': debug_node_name = None debug_node_args = None debug_launch_file = None if len(sys.argv) != 3: print 'usage: %s launch_file node_name' % sys.argv[0] exit(1) debug_launch_file = sys.argv[1] debug_node_name = sys.argv[2] env = None args = None error = None debug_node = None config = roslaunch.config.load_config_default([debug_launch_file], None) for node in (config.nodes + config.tests): fullname = rosgraph.names.ns_join(node.namespace, node.name) if fullname == debug_node_name: debug_node = node if debug_node is None: error = "Could not find a definition for %s" % debug_node_name else: env = setup_env(debug_node, debug_node.machine, os.environ[rosgraph.ROS_MASTER_URI]) args = create_local_process_args(debug_node, debug_node.machine) print(json.dumps({ 'env': env, 'args': args, 'error': error }))
def test_setup_env(self): from roslaunch.core import setup_env, Node, Machine ros_root = '/ros/root1' rpp = '/rpp1' master_uri = 'http://masteruri:1234' n = Node('nodepkg','nodetype') m = Machine('name1', ros_root, rpp, '1.2.3.4') d = setup_env(n, m, master_uri) self.assertEquals(d['ROS_MASTER_URI'], master_uri) self.assertEquals(d['ROS_ROOT'], ros_root) self.assertEquals(d['PYTHONPATH'], os.path.join(ros_root, 'core', 'roslib', 'src')) self.assertEquals(d['ROS_PACKAGE_PATH'], rpp) for k in ['ROS_IP', 'ROS_NAMESPACE']: if k in d: self.fail('%s should not be set: %s'%(k,d[k])) # don't set ROS_ROOT and ROS_PACKAGE_PATH. ROS_ROOT should default, ROS_PACKAGE_PATH does not m = Machine('name1', '', '', '1.2.3.4') d = setup_env(n, m, master_uri) val = os.environ['ROS_ROOT'] self.assertEquals(d['ROS_ROOT'], val) self.assertEquals(d['PYTHONPATH'], os.path.join(val, 'core', 'roslib', 'src')) self.failIf('ROS_PACKAGE_PATH' in d, 'ROS_PACKAGE_PATH should not be set: %s'%d) # test ROS_IP m = Machine('name1', ros_root, rpp, '1.2.3.4', ros_ip="4.5.6.7") n = Node('nodepkg','nodetype', namespace="/ns1") d = setup_env(n, m, master_uri) self.assertEquals(d['ROS_NAMESPACE'], "/ns1") # test stripping n = Node('nodepkg','nodetype', namespace="/ns2/") d = setup_env(n, m, master_uri) self.assertEquals(d['ROS_NAMESPACE'], "/ns2") # test ROS_NAMESPACE m = Machine('name1', ros_root, rpp, '1.2.3.4', ros_ip="4.5.6.7") d = setup_env(n, m, master_uri) self.assertEquals(d['ROS_IP'], "4.5.6.7") # test node.env_args n = Node('nodepkg','nodetype', env_args=[('NENV1', 'val1'), ('NENV2', 'val2'), ('ROS_ROOT', '/new/root')]) d = setup_env(n, m, master_uri) self.assertEquals(d['ROS_ROOT'], "/new/root") self.assertEquals(d['NENV1'], "val1") self.assertEquals(d['NENV2'], "val2") # test machine.env_args m = Machine('name1', ros_root, rpp, '1.2.3.4', ros_ip="4.5.6.7", env_args=[('MENV1', 'val1'), ('MENV2', 'val2'), ('ROS_ROOT', '/new/root2')]) n = Node('nodepkg','nodetype') d = setup_env(n, m, master_uri) self.assertEquals(d['ROS_ROOT'], "/new/root2") self.assertEquals(d['MENV1'], "val1") self.assertEquals(d['MENV2'], "val2") # test node.env_args precedence m = Machine('name1', ros_root, rpp, '1.2.3.4', ros_ip="4.5.6.7", env_args=[('MENV1', 'val1'), ('MENV2', 'val2')]) n = Node('nodepkg','nodetype', env_args=[('MENV1', 'nodeval1')]) d = setup_env(n, m, master_uri) self.assertEquals(d['MENV1'], "nodeval1") self.assertEquals(d['MENV2'], "val2")