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 14291 2011-07-13 03:24:43Z 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 prefix = [] 70 if 'ROS_BUILD' in os.environ: 71 if os.environ['ROS_BUILD'] == os.environ['ROS_ROOT']: 72 return 73 prefix = [os.path.join(os.environ['ROS_BUILD'], 'gen', 'py'), 74 os.path.join(os.environ['ROS_BUILD'], '..', 'rosidl', 'src')] 75 sys.path = prefix + _generate_python_path(package_name, [], os.environ) + sys.path
76
77 -def _append_package_paths(manifest_, paths, pkg_dir):
78 """ 79 Added paths for package to paths 80 @param manifest_: package manifest 81 @type manifest_: Manifest 82 @param pkg_dir: package's filesystem directory path 83 @type pkg_dir: str 84 @param paths: list of paths 85 @type paths: [str] 86 """ 87 exports = manifest_.get_export('python','path') 88 if exports: 89 for export in exports: 90 if ':' in export: 91 export = export.split(':') 92 else: 93 export = [export] 94 for e in export: 95 paths.append(e.replace('${prefix}', pkg_dir)) 96 else: 97 dirs = [os.path.join(pkg_dir, d) for d in ['src', 'lib']] 98 paths.extend(list(filter(os.path.isdir, dirs))) #py3k
99
100 -def _generate_python_path(pkg, depends, env=os.environ):
101 """ 102 Recursive subroutine for building dependency list and python path 103 @param manifest_file: manifest to parse for additional dependencies 104 @param depends: current dependency set. Will be modified 105 @return: list of directory paths to add to python path in order to include 106 package and dependencies described in manifest file. 107 @raise InvalidROSPkgException: if an error occurs while attempting to load package or dependencies 108 """ 109 if pkg in _bootstrapped: 110 return [] 111 manifest_file = roslib.manifest.manifest_file(pkg, True, env) 112 if not manifest_file: 113 raise roslib.packages.InvalidROSPkgException("cannot locate package [%s]"%pkg) 114 _bootstrapped.append(pkg) 115 116 pkg_dir = os.path.dirname(os.path.abspath(manifest_file)) 117 depends.append(pkg) 118 m = roslib.manifest.parse_file(manifest_file) 119 120 paths = [] 121 _append_package_paths(m, paths, pkg_dir) 122 123 try: 124 for d in m.depends: 125 if d.package in depends: 126 continue 127 try: #add sub-dependencies to paths and depends 128 paths.extend(_generate_python_path(d.package, depends, env)) 129 except roslib.packages.InvalidROSPkgException as e: 130 # translate error message to give more context 131 raise roslib.packages.InvalidROSPkgException("While loading package '%s': %s"%(d.package, str(e))) 132 except: 133 import traceback 134 raise roslib.packages.InvalidROSPkgException("While loading package '%s': cannot load dependency '%s'\nLower level error was %s"%(pkg, d.package, traceback.format_exc())) 135 except: 136 if pkg in _bootstrapped: 137 _bootstrapped.remove(pkg) 138 raise 139 return paths
140