roscreatepkg.py
Go to the documentation of this file.
1 # Software License Agreement (BSD License)
2 #
3 # Copyright (c) 2008, 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$
34 
35 from __future__ import print_function
36 
37 NAME='roscreate-pkg'
38 
39 import os
40 import sys
41 
42 import roslib.names
43 
44 from roscreate.core import read_template, author_name
45 from rospkg import on_ros_path, RosPack, ResourceNotFound
46 
48  templates = {}
49  templates['CMakeLists.txt'] = read_template('CMakeLists.tmpl')
50  templates['manifest.xml'] = read_template('manifest.tmpl')
51  templates['mainpage.dox'] = read_template('mainpage.tmpl')
52  templates['Makefile'] = read_template('Makefile.tmpl')
53  return templates
54 
55 def instantiate_template(template, package, brief, description, author, depends):
56  return template%locals()
57 
58 def create_package(package, author, depends, uses_roscpp=False, uses_rospy=False):
59  p = os.path.abspath(package)
60  if os.path.exists(p):
61  print("%s already exists, aborting"%p, file=sys.stderr)
62  sys.exit(1)
63 
64  os.makedirs(p)
65  print("Created package directory", p)
66 
67  if uses_roscpp:
68  # create package/include/package and package/src for roscpp code
69  cpp_path = os.path.join(p, 'include', package)
70  try:
71  os.makedirs(cpp_path)
72  print("Created include directory", cpp_path)
73  cpp_path = os.path.join(p, 'src')
74  os.makedirs(cpp_path)
75  print("Created cpp source directory", cpp_path)
76  except:
77  # file exists
78  pass
79  if uses_rospy:
80  # create package/src/ for python files
81  py_path = os.path.join(p, 'src')
82  try:
83  os.makedirs(py_path)
84  print("Created python source directory", py_path)
85  except:
86  # file exists
87  pass
88 
89  templates = get_templates()
90  for filename, template in templates.items():
91  contents = instantiate_template(template, package, package, package, author, depends)
92  p = os.path.abspath(os.path.join(package, filename))
93  with open(p, 'w') as f:
94  f.write(contents.encode('utf-8'))
95  print("Created package file", p)
96  print("\nPlease edit %s/manifest.xml and mainpage.dox to finish creating your package"%package)
97 
99  from optparse import OptionParser
100  parser = OptionParser(usage="usage: %prog <package-name> [dependencies...]", prog=NAME)
101  options, args = parser.parse_args()
102  if not args:
103  parser.error("you must specify a package name and optionally also list package dependencies")
104  package = args[0]
105 
106  if not roslib.names.is_legal_resource_base_name(package):
107  parser.error("illegal package name: %s\nNames must start with a letter and contain only alphanumeric characters\nand underscores."%package)
108 
109  # validate dependencies and turn into XML
110  depends = args[1:]
111  uses_roscpp = 'roscpp' in depends
112  uses_rospy = 'rospy' in depends
113 
114  rospack = RosPack()
115  for d in depends:
116  try:
117  rospack.get_path(d)
118  except ResourceNotFound:
119  print("ERROR: dependency [%s] cannot be found"%d, file=sys.stderr)
120  sys.exit(1)
121 
122  depends = u''.join([u' <depend package="%s"/>\n'%d for d in depends])
123 
124  if not on_ros_path(os.getcwd()):
125  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)
126  if type(package) == str:
127  package = package.decode('utf-8')
128  create_package(package, author_name(), depends, uses_roscpp=uses_roscpp, uses_rospy=uses_rospy)
def instantiate_template(template, package, brief, description, author, depends)
Definition: roscreatepkg.py:55
def author_name()
Definition: core.py:46
def read_template(tmplf)
Definition: core.py:71
def create_package(package, author, depends, uses_roscpp=False, uses_rospy=False)
Definition: roscreatepkg.py:58


roscreate
Author(s): Ken Conley
autogenerated on Tue Apr 2 2019 02:14:18