geneus_main.py
Go to the documentation of this file.
1 # Software License Agreement (BSD License)
2 #
3 # Copyright (c) 2014, JSK Robotics Laboratory.
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 JSK Robotics Laboratory. 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 
34 from __future__ import print_function
35 
36 from optparse import OptionParser
37 
38 import os
39 import sys
40 import traceback
41 import genmsg
42 import genmsg.command_line
43 from catkin import terminal_color
44 
45 from catkin_pkg import package, packages, workspaces, topological_order
46 
47 from genmsg import MsgGenerationException
48 from . generate import generate_msg, generate_srv
49 
50 def usage(progname):
51  print("%(progname)s file(s)"%vars())
52 
54  pkg_map = {}
55  for ws in workspaces.get_spaces():
56  pkgs = packages.find_packages(ws)
57  for pkg in pkgs.values():
58  # packages.find_packages(workspaces.get_spaces()) returns package in high-priority-first-order, so we should not overwirte package map which is already found
59  # https://github.com/ros-infrastructure/catkin_pkg/blob/fa4b136b16e2d2886ab97257684f6bff243edefb/src/catkin_pkg/workspaces.py#L43
60  # https://github.com/ros-infrastructure/catkin_pkg/blob/fa4b136b16e2d2886ab97257684f6bff243edefb/src/catkin_pkg/packages.py#L71
61  if pkg.name not in pkg_map:
62  pkg_map[pkg.name] = pkg
63  return pkg_map
64 
65 pkg_map = None
66 def get_depends(pkg):
67  """Get dependencies written as run_depend in package.xml"""
68  global pkg_map
69  if pkg_map is None:
70  pkg_map = get_pkg_map()
71  pkg_obj = pkg_map[pkg]
72  pkg_xml_path = pkg_obj.filename
73  depends = map(lambda x: x.name,
74  package.parse_package(pkg_xml_path).exec_depends)
75  depends = list(set(depends)) # for duplicate
76  return depends
77 
78 def package_depends(pkg): # pkg is string
79  global pkg_map
80  if pkg_map is None:
81  pkg_map = get_pkg_map()
82  depends = {}
83  depends_impl = package_depends_impl(pkg)
84  for d in depends_impl:
85  try:
86  pkg_obj = pkg_map[d]
87  p_path = os.path.dirname(pkg_obj.filename)
88  if (os.path.exists(os.path.join(p_path, "msg")) or
89  os.path.exists(os.path.join(p_path, "srv"))):
90  depends[d] = pkg_obj
91  except Exception as e:
92  print(terminal_color.fmt(
93  '@{yellow}[WARNING] path to %s is not found') % (pkg))
94  print(e)
95  return [p.name for n,p in topological_order.topological_order_packages(depends)]
96 
97 def package_depends_impl(pkg, depends=None): # takes and returns Package object
98  if depends is None:
99  depends = []
100  global pkg_map
101  if pkg_map is None:
102  pkg_map = get_pkg_map()
103  if not pkg in pkg_map:
104  print(terminal_color.fmt(
105  '@{yellow}[WARNING] %s is not found in workspace') % (pkg))
106  return depends
107  ros_depends = filter(lambda x: x in pkg_map, get_depends(pkg))
108  tmp_depends = filter(lambda x: x not in depends, ros_depends)
109  depends.extend(tmp_depends)
110  for p in tmp_depends:
111  depends = package_depends_impl(p, depends)
112  return depends
113 
114 def genmain(argv, progname):
115  parser = OptionParser("%s file"%(progname))
116  parser.add_option('-p', dest='package')
117  parser.add_option('-o', dest='outdir')
118  parser.add_option('-I', dest='includepath', action='append')
119  parser.add_option('-m', dest='manifest', action='store_true')
120  options, args = parser.parse_args(argv)
121  try:
122  if len(args) < 2:
123  parser.error("please specify args")
124  if not os.path.exists(options.outdir):
125  # This script can be run multiple times in parallel. We
126  # don't mind if the makedirs call fails because somebody
127  # else snuck in and created the directory before us.
128  try:
129  os.makedirs(options.outdir)
130  except OSError as e:
131  if not os.path.exists(options.outdir):
132  raise
133  if options.manifest:
134  import datetime
135  global pkg_map
136  if pkg_map is None:
137  pkg_map = get_pkg_map()
138  pkg_map = get_pkg_map()
139  f = open(options.outdir+'/manifest.l', 'w+')
140  f.write(";;\n")
141  f.write(";; DO NOT EDIT THIS FILE\n")
142  f.write(";;\n")
143  pkg_filename = 'unknown'
144  pkg_version = 'unknown'
145  if 'geneus' in pkg_map:
146  pkg_filename = pkg_map['geneus'].filename
147  pkg_version = pkg_map['geneus'].version
148  pkg = args[1] # ARG_PKG
149  pkg_dependences = sorted(set(package_depends(pkg) + args[2:])) # args[1] ARG_PKG
150  # write debug information
151  f.write(";; THIS FILE IS AUTOMATICALLY GENERATED\n")
152  try:
153  # https://github.com/jsk-ros-pkg/geneus/commit/152683d63ff23850094b472bff536946e07c76e2 depends on this comment messge
154  f.write(";; FROM %s (%s)\n"%(pkg_map[pkg].filename, pkg_map[pkg].version))
155  f.write(";; USING %s %s (%s)\n"%(__file__,pkg_filename,pkg_version))
156  except:
157  pass
158  f.write(";;\n")
159  # load all dependences and then load target package
160  for p in pkg_dependences:
161  f.write("(ros::load-ros-package \"%s\")\n"%p)
162  f.write("(ros::load-ros-package \"%s\")\n"%pkg)
163  f.close()
164  retcode = 0
165  else:
166  search_path = genmsg.command_line.includepath_to_dict(options.includepath)
167  filename = args[1]
168  if filename.endswith('.msg'):
169  retcode = generate_msg(options.package, args[1:], options.outdir, search_path)
170  else:
171  retcode = generate_srv(options.package, args[1:], options.outdir, search_path)
172  except genmsg.InvalidMsgSpec as e:
173  print("ERROR: ", e, file=sys.stderr)
174  retcode = 1
175  except MsgGenerationException as e:
176  print("ERROR: ", e, file=sys.stderr)
177  retcode = 2
178  except Exception as e:
179  traceback.print_exc()
180  print("ERROR: ",e)
181  retcode = 3
182  sys.exit(retcode or 0)
def generate_msg(pkg, files, out_dir, search_path)
Definition: generate.py:715
def package_depends(pkg)
Definition: geneus_main.py:78
def genmain(argv, progname)
Definition: geneus_main.py:114
def generate_srv(pkg, files, out_dir, search_path)
Definition: generate.py:727
def get_depends(pkg)
Definition: geneus_main.py:66
def usage(progname)
Definition: geneus_main.py:50
def package_depends_impl(pkg, depends=None)
Definition: geneus_main.py:97


geneus
Author(s): Kei Okada
autogenerated on Wed Jun 5 2019 19:32:31