Package rosh :: Package impl :: Module launch
[frames] | no frames]

Source Code for Module rosh.impl.launch

  1  #!/usr/bin/env python 
  2  # Software License Agreement (BSD License) 
  3  # 
  4  # Copyright (c) 2010, Willow Garage, Inc. 
  5  # All rights reserved. 
  6  # 
  7  # Redistribution and use in source and binary forms, with or without 
  8  # modification, are permitted provided that the following conditions 
  9  # are met: 
 10  # 
 11  #  * Redistributions of source code must retain the above copyright 
 12  #    notice, this list of conditions and the following disclaimer. 
 13  #  * Redistributions in binary form must reproduce the above 
 14  #    copyright notice, this list of conditions and the following 
 15  #    disclaimer in the documentation and/or other materials provided 
 16  #    with the distribution. 
 17  #  * Neither the name of Willow Garage, Inc. nor the names of its 
 18  #    contributors may be used to endorse or promote products derived 
 19  #    from this software without specific prior written permission. 
 20  # 
 21  # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
 22  # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
 23  # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
 24  # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
 25  # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
 26  # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
 27  # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
 28  # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
 29  # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
 30  # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
 31  # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 32  # POSSIBILITY OF SUCH DAMAGE. 
 33  # 
 34  # Revision $Id: launch.py 11427 2010-10-06 23:39:08Z kwc $ 
 35   
 36  """ 
 37  roslaunch API for clients. The lower-level implementation is in 
 38  rosh.impl.proc, which is the general process system of 
 39  rosh. rosh.impl.launch is just the roslaunch-level API for clients. 
 40  """ 
 41   
 42  import os 
 43  import sys 
 44   
 45  import roslib.packages 
 46  import roslaunch 
 47   
 48  import rosh.impl.proc 
 49   
50 -def launch_factory(ctx):
51 52 def launch(launchable, type_=None, args=[], remap={}): 53 """ 54 Launch things that are launchable. This includes:: 55 56 launch('path/to/file.launch') 57 launch('my_pkg', 'file.launch') 58 59 launch('path/to/node') 60 launch('pkg', 'nodetype') 61 launch('pkg') # == launch('pkg', 'pkg'), e.g. 'rviz' 62 63 launch(roslaunch.Node) 64 65 @param launchable: ROS package name or Node instance 66 @type launchable: str or Node 67 @param type_: node type (executable name). If None and 68 launchable is a package name, this will default to package 69 name. 70 @type type_: str 71 @param args: node arguments 72 @type args: [str] 73 @param remap: remapping arguments 74 @type remap: dict 75 76 @return: List of nodes launched 77 @rtype: [rosh.impl.Node] 78 """ 79 if type(launchable) == str: 80 81 if os.path.isfile(launchable): 82 if launchable.endswith('.launch'): 83 if type_: 84 raise ValueError("launch of launch file does not accept additional type_ arg") 85 if remap: 86 raise ValueError("launch of launch file does not accept remap arguments") 87 88 return _launch_roslaunch_file(ctx, launchable, args) 89 else: 90 # reverse-derive package name 91 pkg, pkg_dir = roslib.packages.get_dir_pkg(launchable) 92 if pkg == None: 93 raise ValueError("launch: cannot determine package of [%s]"%(launchable)) 94 type_ = os.path.basename(launchable) 95 return _launch_node(ctx, pkg, type_, args, remap) 96 else: 97 # validate launchable is a package name 98 try: 99 roslib.packages.get_pkg_dir(launchable) 100 except roslib.packages.InvalidROSPkgException: 101 raise ValueError("launch: [%s] does not appear to be a package or filename"%(launchable)) 102 103 # launch('rviz') -> launch('rviz', 'rviz') 104 if type_ is None: 105 type_ = launchable 106 107 # determine if we are launching a file or a node 108 if type_.endswith('.launch'): 109 if remap: 110 raise ValueError("launch of launch file does not accept remap arguments") 111 return _launch_roslaunch_file(ctx, findros(launchable, type_), args) 112 else: 113 return _launch_node(ctx, launchable, type_, args, remap) 114 115 elif type(launchable) == roslaunch.Node: 116 if type_: 117 raise ValueError("launch of Node instance does not accept additional type_ arg") 118 return _launch_Node(ctx, launchable, args, remap) 119 elif hasattr(launchable, '_launch'): 120 return launchable._launch(*args, **remap)
121 122 return launch 123
124 -def _launch_roslaunch_file(ctx, filename, args):
125 val = rosh.impl.proc.launch_roslaunch_file(ctx, pkg, type_, args) 126 return [_node_ref(ctx, n, p) for n, p in val]
127
128 -def _launch_Node(ctx, n, args, remap):
129 if args: 130 if type(args) in (list, tuple): 131 args = ' '.join([str(x) for x in args]) 132 n.args = args 133 if remap: 134 raise NotImplemented 135 n, p = rosh.impl.proc.launch_roslaunch_Node(ctx, n) 136 return [_node_ref(ctx, n, p)]
137
138 -def _launch_node(ctx, pkg, type_, args, remap):
139 n, p = rosh.impl.proc.launch_node(ctx, pkg, type_, args, remap=remap) 140 return [_node_ref(ctx, n, p)]
141
142 -def _node_ref(ctx, node, process):
143 """ 144 Convert to rosh.impl.Node reference and attach process for killing 145 @param node: roslaunch Node instance. 'name' attribute must be initialized 146 @type node: roslaunch.Node 147 @param process: Process instance 148 @type process: rosh.impl.proc.Process 149 """ 150 151 # TODO: split apart namespaces 152 ns_node = ctx.nodes[node.name] 153 # attach process for _kill 154 ns_node._process = process 155 return ns_node
156
157 -def rosrun_factory(ctx):
158 def rosrun(pkg, type_, args=[]): 159 n, p = rosh.impl.proc.launch(ctx, pkg, type_, args) 160 ns_node = _node_ref(ctx, n) 161 # attach process for _kill 162 ns_node._process = p 163 return ns_node
164 return rosrun 165
166 -def launch_symbols(ctx):
167 # store shared roslaunch instance in context. All Launchable items 168 # need access to this. 169 170 # _roslaunch is the underlying roslaunc instance for this 171 # ctx. ctx.launch is the plugin-accessible API. 172 ctx._roslaunch = roslaunch.ROSLaunch() 173 ctx.launch = launch_factory(ctx) 174 return { 175 'rosrun': rosrun_factory(ctx), 176 'launch': ctx.launch, 177 }
178