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

Source Code for Module rosh.impl.proc

  1  # Software License Agreement (BSD License) 
  2  # 
  3  # Copyright (c) 2010, Willow Garage, Inc. 
  4  # All rights reserved. 
  5  # 
  6  # Redistribution and use in source and binary forms, with or without 
  7  # modification, are permitted provided that the following conditions 
  8  # are met: 
  9  # 
 10  #  * Redistributions of source code must retain the above copyright 
 11  #    notice, this list of conditions and the following disclaimer. 
 12  #  * Redistributions in binary form must reproduce the above 
 13  #    copyright notice, this list of conditions and the following 
 14  #    disclaimer in the documentation and/or other materials provided 
 15  #    with the distribution. 
 16  #  * Neither the name of Willow Garage, Inc. nor the names of its 
 17  #    contributors may be used to endorse or promote products derived 
 18  #    from this software without specific prior written permission. 
 19  # 
 20  # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
 21  # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
 22  # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
 23  # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
 24  # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
 25  # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
 26  # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
 27  # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
 28  # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
 29  # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
 30  # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 31  # POSSIBILITY OF SUCH DAMAGE. 
 32  # 
 33  # Revision $Id: proc.py 11427 2010-10-06 23:39:08Z kwc $ 
 34   
 35  """ 
 36  ROSH library for running processes.  
 37   
 38  This library is necessary as ROSH commands, like set_master, can 
 39  manipulate the environment in ways that would affect subprocesses. 
 40  """ 
 41   
 42  import os 
 43  from subprocess import Popen, PIPE 
 44   
 45  import roslib.rosenv 
 46  # import roslaunch 
 47   
 48   
 49  # TODO: get rid of this routine or replace with Exec 
50 -def run(config, cmd, stdout=True):
51 """ 52 Run the specified command using the current ROS configuration. 53 """ 54 env = os.environ.copy() 55 env[roslib.rosenv.ROS_MASTER_URI] = config.master.master_uri 56 57 if 0: 58 print "CMD", cmd 59 60 # TODO: added Exec objects to roslaunch that are only accessible via scriptapi 61 if stdout: 62 p = Popen(cmd, stderr=PIPE, env=env) 63 else: 64 p = Popen(cmd, stdout=PIPE, stderr=PIPE, env=env) 65 66 p.poll() 67 return p.returncode in [None, 0]
68
69 -def _init_ctx(ctx):
70 """ 71 Add shared roslaunch instance to ctx if not already initialized. 72 73 @param ctx: ROSH context 74 @type ctx: Contextj 75 """ 76 if not ctx._roslaunch.started: 77 ctx._roslaunch.start()
78 79 #TODO: __str__ debugging info
80 -class Process(object):
81 """ 82 ROSH wrapper for processes to make them kill-able 83 """ 84
85 - def __init__(self, roslaunch_process):
86 self.roslaunch_process = roslaunch_process
87
88 - def _kill(self):
89 self.roslaunch_process.stop()
90
91 -def launch_node(ctx, pkg, type_, args=[], remap={}, stdout=False):
92 """ 93 Low-level launch() API for internal ROSH and ROSH plugin use. 94 95 @param ctx: ROSH context 96 @type ctx: Context 97 @param pkg: package name 98 @param type_: node type 99 @return: Node and Process instance 100 @rtype: (roslaunch.Node, roslaunch.Process) 101 """ 102 output = 'screen' if stdout else None 103 if type(args) in (list, tuple): 104 args = ' '.join([str(x) for x in args]) 105 if remap: 106 args = args + ' '.join(['%s:=%s'%(k, v) for k, v in remap.iteritems()]) 107 108 return launch_roslaunch_Node(ctx, roslaunch.Node(pkg, type_, args=args, output=output, filename='<rosh>'))
109
110 -def launch_roslaunch_file(ctx, roslaunch_filename):
111 # TODO: use roshlaunch API 112 raise NotImplemented
113
114 -def launch_roslaunch_Node(ctx, node):
115 """ 116 Launch a Node instance. This is a low-level launch() API for 117 internal ROSH and ROSH plugin use. 118 119 @param ctx: ROSH context 120 @type ctx: Context 121 @param node: node instance. The name attribute may be overwritten. 122 @type node: roslaunch.Node 123 @return: Node instance 124 @rtype: Node 125 """ 126 _init_ctx(ctx) 127 p = ctx._roslaunch.launch(node) 128 return node, Process(p)
129