def create_node_process(run_id, node, master_uri): """ Factory for generating processes for launching local ROS nodes. Also registers the process with the L{ProcessMonitor} so that events can be generated when the process dies. @param run_id: run_id of launch @type run_id: str @param node: node to launch. Node name must be assigned. @type node: L{Node} @param master_uri: API URI for master node @type master_uri: str @return: local process instance @rtype: L{LocalProcess} @raise NodeParamsException: If the node's parameters are improperly specific """ _logger.info( "create_node_process: package[%s] type[%s] machine[%s] master_uri[%s]", node.package, node.type, node.machine, master_uri) # check input args machine = node.machine if machine is None: raise RLException( "Internal error: no machine selected for node of type [%s/%s]" % (node.package, node.type)) if not node.name: raise ValueError("node name must be assigned") # - setup env for process (vars must be strings for os.environ) env = create_local_process_env(node, machine, master_uri) if not node.name: raise ValueError("node name must be assigned") # we have to include the counter to prevent potential name # collisions between the two branches name = "%s-%s" % (node.name, _next_counter()) _logger.info('process[%s]: env[%s]', name, env) args = create_local_process_args(node, machine) _logger.info('process[%s]: args[%s]', name, args) # default for node.output not set is 'log' log_output = node.output != 'screen' _logger.debug('process[%s]: returning LocalProcess wrapper') return LocalProcess(run_id, node.package, name, args, env, log_output, respawn=node.respawn, required=node.required, cwd=node.cwd)
def create_node_process(run_id, node, master_uri): """ Factory for generating processes for launching local ROS nodes. Also registers the process with the L{ProcessMonitor} so that events can be generated when the process dies. @param run_id: run_id of launch @type run_id: str @param node: node to launch. Node name must be assigned. @type node: L{Node} @param master_uri: API URI for master node @type master_uri: str @return: local process instance @rtype: L{LocalProcess} @raise NodeParamsException: If the node's parameters are improperly specific """ _logger.info("create_node_process: package[%s] type[%s] machine[%s] master_uri[%s]", node.package, node.type, node.machine, master_uri) # check input args machine = node.machine if machine is None: raise RLException("Internal error: no machine selected for node of type [%s/%s]"%(node.package, node.type)) if not node.name: raise ValueError("node name must be assigned") # - setup env for process (vars must be strings for os.environ) env = create_local_process_env(node, machine, master_uri) if not node.name: raise ValueError("node name must be assigned") # we have to include the counter to prevent potential name # collisions between the two branches name = "%s-%s"%(roslib.names.ns_join(node.namespace, node.name), _next_counter()) if name[0] == '/': name = name[1:] _logger.info('process[%s]: env[%s]', name, env) args = create_local_process_args(node, machine) _logger.info('process[%s]: args[%s]', name, args) # default for node.output not set is 'log' log_output = node.output != 'screen' _logger.debug('process[%s]: returning LocalProcess wrapper') return LocalProcess(run_id, node.package, name, args, env, log_output, respawn=node.respawn, required=node.required, cwd=node.cwd)
def test_create_local_process_env(self): # this is almost entirely identical to the setup_env test in test_core, with some additional # higher level checks from roslaunch.core import Node, Machine from roslaunch.node_args import create_local_process_env 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 = create_local_process_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])) # test that it inherits local environment env = os.environ.copy() env['PATH'] = '/some/path' env['FOO'] = 'foo' d = create_local_process_env(n, m, master_uri, env=env) self.assertEquals(d['FOO'], 'foo') # test that it defaults to os.environ os.environ['FOO'] = 'bar' d = create_local_process_env(n, m, master_uri) self.assertEquals(d['FOO'], 'bar') # test that our ROS_ROOT and PYTHONPATH override env env = os.environ.copy() env['ROS_ROOT'] = '/not/ros/root/' env['PYTHONPATH'] = '/some/path' d = create_local_process_env(n, m, master_uri, env=env) self.assertEquals(d['ROS_ROOT'], ros_root) self.assertEquals(d['PYTHONPATH'], os.path.join(ros_root, 'core', 'roslib', 'src')) # - make sure it didn't pollute original env self.assertEquals(env['ROS_ROOT'], '/not/ros/root/') self.assertEquals(env['PYTHONPATH'], '/some/path') # 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 = create_local_process_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 = create_local_process_env(n, m, master_uri) self.assertEquals(d['ROS_NAMESPACE'], "/ns1") # test stripping n = Node('nodepkg','nodetype', namespace="/ns2/") d = create_local_process_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 = create_local_process_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 = create_local_process_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 = create_local_process_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 = create_local_process_env(n, m, master_uri) self.assertEquals(d['MENV1'], "nodeval1") self.assertEquals(d['MENV2'], "val2")