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


roscreate
Author(s): Ken Conley
autogenerated on Thu Apr 30 2020 06:30:16