roscreatepkg.py
Go to the documentation of this file.
00001 # Software License Agreement (BSD License)
00002 #
00003 # Copyright (c) 2008, Willow Garage, Inc.
00004 # All rights reserved.
00005 #
00006 # Redistribution and use in source and binary forms, with or without
00007 # modification, are permitted provided that the following conditions
00008 # are met:
00009 #
00010 #  * Redistributions of source code must retain the above copyright
00011 #    notice, this list of conditions and the following disclaimer.
00012 #  * Redistributions in binary form must reproduce the above
00013 #    copyright notice, this list of conditions and the following
00014 #    disclaimer in the documentation and/or other materials provided
00015 #    with the distribution.
00016 #  * Neither the name of Willow Garage, Inc. nor the names of its
00017 #    contributors may be used to endorse or promote products derived
00018 #    from this software without specific prior written permission.
00019 #
00020 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00021 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00022 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00023 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00024 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00025 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00026 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00027 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00028 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00029 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00030 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00031 # POSSIBILITY OF SUCH DAMAGE.
00032 #
00033 # Revision $Id$
00034 
00035 from __future__ import print_function
00036 
00037 NAME='roscreate-pkg'
00038 
00039 import os
00040 import sys
00041 
00042 import roslib.names
00043 
00044 from roscreate.core import read_template, author_name
00045 from rospkg import on_ros_path, RosPack, ResourceNotFound
00046 
00047 def get_templates():
00048     templates = {}
00049     templates['CMakeLists.txt'] = read_template('CMakeLists.tmpl')
00050     templates['manifest.xml'] = read_template('manifest.tmpl')
00051     templates['mainpage.dox'] = read_template('mainpage.tmpl')
00052     templates['Makefile'] = read_template('Makefile.tmpl')
00053     return templates
00054 
00055 def instantiate_template(template, package, brief, description, author, depends):
00056     return template%locals()
00057 
00058 def create_package(package, author, depends, uses_roscpp=False, uses_rospy=False):
00059     p = os.path.abspath(package)
00060     if os.path.exists(p):
00061         print("%s already exists, aborting"%p, file=sys.stderr)
00062         sys.exit(1)
00063 
00064     os.makedirs(p)
00065     print("Created package directory", p)
00066         
00067     if uses_roscpp:
00068         # create package/include/package and package/src for roscpp code
00069         cpp_path = os.path.join(p, 'include', package)
00070         try:        
00071             os.makedirs(cpp_path)
00072             print("Created include directory", cpp_path)
00073             cpp_path = os.path.join(p, 'src')
00074             os.makedirs(cpp_path)
00075             print("Created cpp source directory", cpp_path)
00076         except:
00077             # file exists
00078             pass
00079     if uses_rospy:
00080         # create package/src/ for python files
00081         py_path = os.path.join(p, 'src')
00082         try:
00083             os.makedirs(py_path)
00084             print("Created python source directory", py_path)
00085         except:
00086             # file exists
00087             pass
00088         
00089     templates = get_templates()
00090     for filename, template in templates.items():
00091         contents = instantiate_template(template, package, package, package, author, depends)
00092         p = os.path.abspath(os.path.join(package, filename))
00093         with open(p, 'w') as f:
00094             f.write(contents.encode('utf-8'))
00095             print("Created package file", p)
00096     print("\nPlease edit %s/manifest.xml and mainpage.dox to finish creating your package"%package)
00097 
00098 def roscreatepkg_main():
00099     from optparse import OptionParser    
00100     parser = OptionParser(usage="usage: %prog <package-name> [dependencies...]", prog=NAME)
00101     options, args = parser.parse_args()
00102     if not args:
00103         parser.error("you must specify a package name and optionally also list package dependencies")
00104     package = args[0]
00105 
00106     if not roslib.names.is_legal_resource_base_name(package):
00107         parser.error("illegal package name: %s\nNames must start with a letter and contain only alphanumeric characters\nand underscores."%package)
00108 
00109     # validate dependencies and turn into XML
00110     depends = args[1:]
00111     uses_roscpp = 'roscpp' in depends
00112     uses_rospy = 'rospy' in depends
00113 
00114     rospack = RosPack()
00115     for d in depends:
00116         try:
00117             rospack.get_path(d)
00118         except ResourceNotFound:
00119             print("ERROR: dependency [%s] cannot be found"%d, file=sys.stderr)
00120             sys.exit(1)
00121 
00122     depends = u''.join([u'  <depend package="%s"/>\n'%d for d in depends])
00123 
00124     if not on_ros_path(os.getcwd()):
00125         print('!'*80+"\nWARNING: current working directory is not on ROS_PACKAGE_PATH!\nPlease update your ROS_PACKAGE_PATH environment variable.\n"+'!'*80, file=sys.stderr)
00126     if type(package) == str:
00127         package = package.decode('utf-8')
00128     create_package(package, author_name(), depends, uses_roscpp=uses_roscpp, uses_rospy=uses_rospy)


roscreate
Author(s): Ken Conley
autogenerated on Thu Mar 9 2017 05:00:41