geneus_main.py
Go to the documentation of this file.
00001 # Software License Agreement (BSD License)
00002 #
00003 # Copyright (c) 2014, JSK Robotics Laboratory.
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 JSK Robotics Laboratory. 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 
00034 from __future__ import print_function
00035 
00036 from optparse import OptionParser
00037 
00038 import os
00039 import sys
00040 import traceback
00041 import genmsg
00042 import genmsg.command_line
00043 from catkin import terminal_color
00044 
00045 from catkin_pkg import package, packages, workspaces, topological_order
00046 
00047 from genmsg import MsgGenerationException
00048 from . generate import generate_msg, generate_srv
00049 
00050 def usage(progname):
00051     print("%(progname)s file(s)"%vars())
00052 
00053 def get_pkg_map():
00054     pkg_map = {}
00055     for ws in workspaces.get_spaces():
00056         pkgs = packages.find_packages(ws)
00057         for pkg in pkgs.values():
00058             # packages.find_packages(workspaces.get_spaces()) returns package in high-priority-first-order, so we should not overwirte package map which is already found
00059             # https://github.com/ros-infrastructure/catkin_pkg/blob/fa4b136b16e2d2886ab97257684f6bff243edefb/src/catkin_pkg/workspaces.py#L43
00060             # https://github.com/ros-infrastructure/catkin_pkg/blob/fa4b136b16e2d2886ab97257684f6bff243edefb/src/catkin_pkg/packages.py#L71
00061             if pkg.name not in pkg_map:
00062                 pkg_map[pkg.name] = pkg
00063     return pkg_map
00064 
00065 pkg_map = None
00066 def get_depends(pkg):
00067     """Get dependencies written as run_depend in package.xml"""
00068     global pkg_map
00069     if pkg_map is None:
00070         pkg_map = get_pkg_map()
00071     pkg_obj = pkg_map[pkg]
00072     pkg_xml_path = pkg_obj.filename
00073     depends = map(lambda x: x.name,
00074                   package.parse_package(pkg_xml_path).exec_depends)
00075     depends = list(set(depends))  # for duplicate
00076     return depends
00077 
00078 def package_depends(pkg):  # pkg is string
00079     global pkg_map
00080     if pkg_map is None:
00081         pkg_map = get_pkg_map()
00082     depends = {}
00083     depends_impl = package_depends_impl(pkg)
00084     for d in depends_impl:
00085         try:
00086             pkg_obj = pkg_map[d]
00087             p_path = os.path.dirname(pkg_obj.filename)
00088             if (os.path.exists(os.path.join(p_path, "msg")) or
00089                     os.path.exists(os.path.join(p_path, "srv"))):
00090                 depends[d] = pkg_obj
00091         except Exception as e:
00092             print(terminal_color.fmt(
00093                 '@{yellow}[WARNING] path to %s is not found') % (pkg))
00094             print(e)
00095     return [p.name for n,p in topological_order.topological_order_packages(depends)]
00096 
00097 def package_depends_impl(pkg, depends=None): # takes and returns Package object
00098     if depends is None:
00099         depends = []
00100     global pkg_map
00101     if pkg_map is None:
00102         pkg_map = get_pkg_map()
00103     if not pkg in pkg_map:
00104         print(terminal_color.fmt(
00105             '@{yellow}[WARNING] %s is not found in workspace') % (pkg))
00106         return depends
00107     ros_depends = filter(lambda x: x in pkg_map, get_depends(pkg))
00108     tmp_depends = filter(lambda x: x not in depends, ros_depends)
00109     depends.extend(tmp_depends)
00110     for p in tmp_depends:
00111         depends = package_depends_impl(p, depends)
00112     return depends
00113 
00114 def genmain(argv, progname):
00115     parser = OptionParser("%s file"%(progname))
00116     parser.add_option('-p', dest='package')
00117     parser.add_option('-o', dest='outdir')
00118     parser.add_option('-I', dest='includepath', action='append')
00119     parser.add_option('-m', dest='manifest', action='store_true')
00120     options, args = parser.parse_args(argv)
00121     try:
00122         if len(args) < 2:
00123             parser.error("please specify args")
00124         if not os.path.exists(options.outdir):
00125             # This script can be run multiple times in parallel. We
00126             # don't mind if the makedirs call fails because somebody
00127             # else snuck in and created the directory before us.
00128             try:
00129                 os.makedirs(options.outdir)
00130             except OSError as e:
00131                 if not os.path.exists(options.outdir):
00132                     raise
00133         if options.manifest:
00134             import datetime
00135             global pkg_map
00136             if pkg_map is None:
00137                 pkg_map = get_pkg_map()
00138             pkg_map = get_pkg_map()
00139             f = open(options.outdir+'/manifest.l', 'w+')
00140             f.write(";;\n")
00141             f.write(";; DO NOT EDIT THIS FILE\n")
00142             f.write(";;\n")
00143             pkg_filename = 'unknown'
00144             pkg_version = 'unknown'
00145             if 'geneus' in pkg_map:
00146                 pkg_filename = pkg_map['geneus'].filename
00147                 pkg_version  = pkg_map['geneus'].version
00148             pkg = args[1]               # ARG_PKG
00149             pkg_dependences = sorted(set(package_depends(pkg) + args[2:]))   # args[1] ARG_PKG
00150             # write debug information
00151             f.write(";; THIS FILE IS AUTOMATICALLY GENERATED\n")
00152             try:
00153                 # https://github.com/jsk-ros-pkg/geneus/commit/152683d63ff23850094b472bff536946e07c76e2 depends on this comment messge
00154                 f.write(";;  FROM %s (%s)\n"%(pkg_map[pkg].filename, pkg_map[pkg].version))
00155                 f.write(";; USING %s %s (%s)\n"%(__file__,pkg_filename,pkg_version))
00156             except:
00157                 pass
00158             f.write(";;\n")
00159             # load all dependences and then load target package
00160             for p in pkg_dependences:
00161                 f.write("(ros::load-ros-package \"%s\")\n"%p)
00162             f.write("(ros::load-ros-package \"%s\")\n"%pkg)
00163             f.close()
00164             retcode = 0
00165         else:
00166             search_path = genmsg.command_line.includepath_to_dict(options.includepath)
00167             filename = args[1]
00168             if filename.endswith('.msg'):
00169                 retcode = generate_msg(options.package, args[1:], options.outdir, search_path)
00170             else:
00171                 retcode = generate_srv(options.package, args[1:], options.outdir, search_path)
00172     except genmsg.InvalidMsgSpec as e:
00173         print("ERROR: ", e, file=sys.stderr)
00174         retcode = 1
00175     except MsgGenerationException as e:
00176         print("ERROR: ", e, file=sys.stderr)
00177         retcode = 2
00178     except Exception as e:
00179         traceback.print_exc()
00180         print("ERROR: ",e)
00181         retcode = 3
00182     sys.exit(retcode or 0)


geneus
Author(s): Kei Okada
autogenerated on Sat Jun 8 2019 20:46:04