Package roslib :: Module launcher
[frames] | no frames]

Source Code for Module roslib.launcher

  1  #! /usr/bin/env python 
  2  # Software License Agreement (BSD License) 
  3  # 
  4  # Copyright (c) 2008, 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: launcher.py 6565 2009-10-21 20:53:37Z kwc $ 
 35  # $Author: kwc $ 
 36   
 37  """ 
 38  Python path loader for python scripts and applications. Paths are 
 39  derived from dependency structure declared in ROS manifest files. 
 40  """ 
 41   
 42  import sys 
 43  import os 
 44   
 45  import roslib.manifest 
 46  import roslib.packages 
 47   
48 -def get_manifest_file(package_name):
49 """ 50 @return: name of package to get manifest for 51 @rtype: str 52 @raise InvalidROSPkgException: if required is True and package cannot be located 53 """ 54 return roslib.manifest.manifest_file(package_name, required=True)
55 56 # bootstrapped keeps track of which packages we've loaded so we don't update the path multiple times 57 _bootstrapped = [] 58
59 -def load_manifest(package_name, bootstrap_version="0.7"):
60 """ 61 Update the Python sys.path with package's dependencies 62 @param package_name: name of the package that load_manifest() is being called from. 63 @type package_name: str 64 @param bootstrap_version: (keyword argument) do not use. Soon to be deprecated 65 @type bootstrap_version: str 66 """ 67 if package_name in _bootstrapped: 68 return 69 sys.path = _generate_python_path(package_name, [], os.environ) + sys.path
70
71 -def _append_package_paths(manifest_, paths, pkg_dir):
72 """ 73 Added paths for package to paths 74 @param manifest_: package manifest 75 @type manifest_: Manifest 76 @param pkg_dir: package's filesystem directory path 77 @type pkg_dir: str 78 @param paths: list of paths 79 @type paths: [str] 80 """ 81 exports = manifest_.get_export('python','path') 82 if exports: 83 for export in exports: 84 if ':' in export: 85 export = export.split(':') 86 else: 87 export = [export] 88 for e in export: 89 paths.append(e.replace('${prefix}', pkg_dir)) 90 else: 91 dirs = [os.path.join(pkg_dir, d) for d in ['src', 'lib']] 92 paths.extend(filter(os.path.isdir, dirs))
93
94 -def _generate_python_path(pkg, depends, env=os.environ):
95 """ 96 Recursive subroutine for building dependency list and python path 97 @param manifest_file: manifest to parse for additional dependencies 98 @param depends: current dependency set. Will be modified 99 @return: list of directory paths to add to python path in order to include 100 package and dependencies described in manifest file. 101 @raise InvalidROSPkgException: if an error occurs while attempting to load package or dependencies 102 """ 103 if pkg in _bootstrapped: 104 return [] 105 manifest_file = roslib.manifest.manifest_file(pkg, True, env) 106 if not manifest_file: 107 raise roslib.packages.InvalidROSPkgException("cannot locate package [%s]"%pkg) 108 _bootstrapped.append(pkg) 109 110 pkg_dir = os.path.dirname(os.path.abspath(manifest_file)) 111 depends.append(pkg) 112 m = roslib.manifest.parse_file(manifest_file) 113 114 paths = [] 115 _append_package_paths(m, paths, pkg_dir) 116 117 try: 118 for d in m.depends: 119 if d.package in depends: 120 continue 121 try: #add sub-dependencies to paths and depends 122 paths.extend(_generate_python_path(d.package, depends, env)) 123 except roslib.packages.InvalidROSPkgException, e: 124 # translate error message to give more context 125 raise roslib.packages.InvalidROSPkgException("While loading package '%s': %s"%(d.package, str(e))) 126 except: 127 import traceback 128 raise roslib.packages.InvalidROSPkgException("While loading package '%s': cannot load dependency '%s'\nLower level error was %s"%(pkg, d.package, traceback.format_exc())) 129 except: 130 if pkg in _bootstrapped: 131 _bootstrapped.remove(pkg) 132 raise 133 return paths
134