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  # TODO: get rid of this routine or replace with Exec 
49 -def run(config, cmd, stdout=True):
50 """ 51 Run the specified command using the current ROS configuration. 52 """ 53 env = os.environ.copy() 54 env[roslib.rosenv.ROS_MASTER_URI] = config.master.master_uri 55 56 if 0: 57 print "CMD", cmd 58 59 # TODO: added Exec objects to roslaunch that are only accessible via scriptapi 60 if stdout: 61 p = Popen(cmd, stderr=PIPE, env=env) 62 else: 63 p = Popen(cmd, stdout=PIPE, stderr=PIPE, env=env) 64 65 p.poll() 66 return p.returncode in [None, 0]
67
68 -def _init_ctx(ctx):
69 """ 70 Add shared roslaunch instance to ctx if not already initialized. 71 72 @param ctx: ROSH context 73 @type ctx: Contextj 74 """ 75 if not ctx._roslaunch.started: 76 ctx._roslaunch.start()
77 78 #TODO: __str__ debugging info
79 -class Process(object):
80 """ 81 ROSH wrapper for processes to make them kill-able 82 """ 83
84 - def __init__(self, roslaunch_process):
85 self.roslaunch_process = roslaunch_process
86
87 - def _kill(self):
88 self.roslaunch_process.stop()
89
90 -def launch_node(ctx, pkg, type_, args=[], remap={}, stdout=False):
91 """ 92 Low-level launch() API for internal ROSH and ROSH plugin use. 93 94 @param ctx: ROSH context 95 @type ctx: Context 96 @param pkg: package name 97 @param type_: node type 98 @return: Node and Process instance 99 @rtype: (roslaunch.Node, roslaunch.Process) 100 """ 101 output = 'screen' if stdout else None 102 if type(args) in (list, tuple): 103 args = ' '.join([str(x) for x in args]) 104 if remap: 105 args = args + ' '.join(['%s:=%s'%(k, v) for k, v in remap.iteritems()]) 106 107 return launch_roslaunch_Node(ctx, roslaunch.Node(pkg, type_, args=args, output=output, filename='<rosh>'))
108
109 -def launch_roslaunch_file(ctx, roslaunch_filename):
110 # TODO: use roshlaunch API 111 raise NotImplemented
112
113 -def launch_roslaunch_Node(ctx, node):
114 """ 115 Launch a Node instance. This is a low-level launch() API for 116 internal ROSH and ROSH plugin use. 117 118 @param ctx: ROSH context 119 @type ctx: Context 120 @param node: node instance. The name attribute may be overwritten. 121 @type node: roslaunch.Node 122 @return: Node instance 123 @rtype: Node 124 """ 125 _init_ctx(ctx) 126 p = ctx._roslaunch.launch(node) 127 return node, Process(p)
128