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             pkg_map[pkg.name] = pkg
00059     return pkg_map
00060 
00061 pkg_map = None
00062 def get_depends(pkg):
00063     """Get dependencies written as run_depend in package.xml"""
00064     global pkg_map
00065     if pkg_map is None:
00066         pkg_map = get_pkg_map()
00067     pkg_obj = pkg_map[pkg]
00068     pkg_xml_path = pkg_obj.filename
00069     depends = map(lambda x: x.name,
00070                   package.parse_package(pkg_xml_path).exec_depends)
00071     depends = list(set(depends))  # for duplicate
00072     return depends
00073 
00074 def package_depends(pkg):  # pkg is string
00075     global pkg_map
00076     if pkg_map is None:
00077         pkg_map = get_pkg_map()
00078     depends = {}
00079     depends_impl = package_depends_impl(pkg)
00080     for d in depends_impl:
00081         try:
00082             pkg_obj = pkg_map[d]
00083             p_path = os.path.dirname(pkg_obj.filename)
00084             if (os.path.exists(os.path.join(p_path, "msg")) or
00085                     os.path.exists(os.path.join(p_path, "srv"))):
00086                 depends[d] = pkg_obj
00087         except Exception as e:
00088             print(terminal_color.fmt(
00089                 '@{yellow}[WARNING] path to %s is not found') % (pkg))
00090             print(e)
00091     return [p.name for n,p in topological_order.topological_order_packages(depends)]
00092 
00093 def package_depends_impl(pkg, depends=None): # takes and returns Package object
00094     if depends is None:
00095         depends = []
00096     global pkg_map
00097     if pkg_map is None:
00098         pkg_map = get_pkg_map()
00099     if not pkg in pkg_map:
00100         print(terminal_color.fmt(
00101             '@{yellow}[WARNING] %s is not found in workspace') % (pkg))
00102         return depends
00103     ros_depends = filter(lambda x: x in pkg_map, get_depends(pkg))
00104     tmp_depends = filter(lambda x: x not in depends, ros_depends)
00105     depends.extend(tmp_depends)
00106     for p in tmp_depends:
00107         depends = package_depends_impl(p, depends)
00108     return depends
00109 
00110 def genmain(argv, progname):
00111     parser = OptionParser("%s file"%(progname))
00112     parser.add_option('-p', dest='package')
00113     parser.add_option('-o', dest='outdir')
00114     parser.add_option('-I', dest='includepath', action='append')
00115     parser.add_option('-m', dest='manifest', action='store_true')
00116     options, args = parser.parse_args(argv)
00117     try:
00118         if len(args) < 2:
00119             parser.error("please specify args")
00120         if not os.path.exists(options.outdir):
00121             # This script can be run multiple times in parallel. We
00122             # don't mind if the makedirs call fails because somebody
00123             # else snuck in and created the directory before us.
00124             try:
00125                 os.makedirs(options.outdir)
00126             except OSError as e:
00127                 if not os.path.exists(options.outdir):
00128                     raise
00129         if options.manifest:
00130             import datetime
00131             global pkg_map
00132             if pkg_map is None:
00133                 pkg_map = get_pkg_map()
00134             pkg_map = get_pkg_map()
00135             f = open(options.outdir+'/manifest.l', 'w+')
00136             f.write(";;\n")
00137             f.write(";; DO NOT EDIT THIS FILE\n")
00138             f.write(";;\n")
00139             pkg_filename = 'unknown'
00140             pkg_version = 'unknown'
00141             if 'geneus' in pkg_map:
00142                 pkg_filename = pkg_map['geneus'].filename
00143                 pkg_version  = pkg_map['geneus'].version
00144             f.write(";; THIS FILE IS AUTOMATICALLY GENERATED BY %s %s %s %s\n"%(__file__,pkg_filename,pkg_version,datetime.datetime.now()))
00145             f.write(";;\n")
00146             pkg = args[1]               # ARG_PKG
00147             pkg_dependences = package_depends(pkg) + args[2:]   # args[1] ARG_PKG
00148             # load all dependences and then load target package
00149             for p in pkg_dependences:
00150                 f.write("(ros::load-ros-package \"%s\")\n"%p)
00151             f.write("(ros::load-ros-package \"%s\")\n"%pkg)
00152             f.close()
00153             retcode = 0
00154         else:
00155             search_path = genmsg.command_line.includepath_to_dict(options.includepath)
00156             filename = args[1]
00157             if filename.endswith('.msg'):
00158                 retcode = generate_msg(options.package, args[1:], options.outdir, search_path)
00159             else:
00160                 retcode = generate_srv(options.package, args[1:], options.outdir, search_path)
00161     except genmsg.InvalidMsgSpec as e:
00162         print("ERROR: ", e, file=sys.stderr)
00163         retcode = 1
00164     except MsgGenerationException as e:
00165         print("ERROR: ", e, file=sys.stderr)
00166         retcode = 2
00167     except Exception as e:
00168         traceback.print_exc()
00169         print("ERROR: ",e)
00170         retcode = 3
00171     sys.exit(retcode or 0)


geneus
Author(s): Kei Okada
autogenerated on Fri Aug 28 2015 10:46:33