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 = [x.name for x in package.parse_package(pkg_xml_path).exec_depends]
74  depends = list(set(depends)) # for duplicate
75  return depends
76 
77 def package_depends(pkg): # pkg is string
78  global pkg_map
79  if pkg_map is None:
80  pkg_map = get_pkg_map()
81  depends = {}
82  depends_impl = package_depends_impl(pkg)
83  for d in depends_impl:
84  try:
85  pkg_obj = pkg_map[d]
86  p_path = os.path.dirname(pkg_obj.filename)
87  if (os.path.exists(os.path.join(p_path, "msg")) or
88  os.path.exists(os.path.join(p_path, "srv"))):
89  depends[d] = pkg_obj
90  except Exception as e:
91  print(terminal_color.fmt(
92  '@{yellow}[WARNING] path to %s is not found') % (pkg))
93  print(e)
94  return [p.name for n,p in topological_order.topological_order_packages(depends)]
95 
96 def package_depends_impl(pkg, depends=None): # takes and returns Package object
97  if depends is None:
98  depends = []
99  global pkg_map
100  if pkg_map is None:
101  pkg_map = get_pkg_map()
102  if not pkg in pkg_map:
103  print(terminal_color.fmt(
104  '@{yellow}[WARNING] %s is not found in workspace') % (pkg))
105  return depends
106  ros_depends = [x for x in get_depends(pkg) if x in pkg_map]
107  tmp_depends = [x for x in ros_depends if x not in depends]
108  depends.extend(tmp_depends)
109  for p in tmp_depends:
110  depends = package_depends_impl(p, depends)
111  return depends
112 
113 def genmain(argv, progname):
114  parser = OptionParser("%s file"%(progname))
115  parser.add_option('-p', dest='package')
116  parser.add_option('-o', dest='outdir')
117  parser.add_option('-I', dest='includepath', action='append')
118  parser.add_option('-m', dest='manifest', action='store_true')
119  options, args = parser.parse_args(argv)
120  try:
121  if len(args) < 2:
122  parser.error("please specify args")
123  if not os.path.exists(options.outdir):
124  # This script can be run multiple times in parallel. We
125  # don't mind if the makedirs call fails because somebody
126  # else snuck in and created the directory before us.
127  try:
128  os.makedirs(options.outdir)
129  except OSError as e:
130  if not os.path.exists(options.outdir):
131  raise
132  if options.manifest:
133  import datetime
134  global pkg_map
135  if pkg_map is None:
136  pkg_map = get_pkg_map()
137  pkg_map = get_pkg_map()
138  f = open(options.outdir+'/manifest.l', 'w+')
139  f.write(";;\n")
140  f.write(";; DO NOT EDIT THIS FILE\n")
141  f.write(";;\n")
142  pkg_filename = 'unknown'
143  pkg_version = 'unknown'
144  if 'geneus' in pkg_map:
145  pkg_filename = pkg_map['geneus'].filename
146  pkg_version = pkg_map['geneus'].version
147  pkg = args[1] # ARG_PKG
148  pkg_dependences = sorted(set(package_depends(pkg) + args[2:])) # args[1] ARG_PKG
149  # write debug information
150  f.write(";; THIS FILE IS AUTOMATICALLY GENERATED\n")
151  try:
152  # https://github.com/jsk-ros-pkg/geneus/commit/152683d63ff23850094b472bff536946e07c76e2 depends on this comment messge
153  f.write(";; FROM %s (%s)\n"%(pkg_map[pkg].filename, pkg_map[pkg].version))
154  f.write(";; USING %s %s (%s)\n"%(__file__,pkg_filename,pkg_version))
155  except:
156  pass
157  f.write(";;\n")
158  # load all dependences and then load target package
159  for p in pkg_dependences:
160  f.write("(ros::load-ros-package \"%s\")\n"%p)
161  f.write("(ros::load-ros-package \"%s\")\n"%pkg)
162  f.close()
163  retcode = 0
164  else:
165  search_path = genmsg.command_line.includepath_to_dict(options.includepath)
166  filename = args[1]
167  if filename.endswith('.msg'):
168  retcode = generate_msg(options.package, args[1:], options.outdir, search_path)
169  else:
170  retcode = generate_srv(options.package, args[1:], options.outdir, search_path)
171  except genmsg.InvalidMsgSpec as e:
172  print("ERROR: ", e, file=sys.stderr)
173  retcode = 1
174  except MsgGenerationException as e:
175  print("ERROR: ", e, file=sys.stderr)
176  retcode = 2
177  except Exception as e:
178  traceback.print_exc()
179  print("ERROR: ",e)
180  retcode = 3
181  sys.exit(retcode or 0)
geneus.geneus_main.get_depends
def get_depends(pkg)
Definition: geneus_main.py:66
geneus.generate.generate_msg
def generate_msg(pkg, files, out_dir, search_path)
Definition: generate.py:724
geneus.geneus_main.get_pkg_map
def get_pkg_map()
Definition: geneus_main.py:53
geneus.geneus_main.package_depends_impl
def package_depends_impl(pkg, depends=None)
Definition: geneus_main.py:96
geneus.generate.generate_srv
def generate_srv(pkg, files, out_dir, search_path)
Definition: generate.py:736
geneus.geneus_main.package_depends
def package_depends(pkg)
Definition: geneus_main.py:77
geneus.geneus_main.genmain
def genmain(argv, progname)
Definition: geneus_main.py:113
geneus.geneus_main.usage
def usage(progname)
Definition: geneus_main.py:50


geneus
Author(s): Kei Okada
autogenerated on Wed Mar 2 2022 00:17:05