Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
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
00059
00060
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))
00076 return depends
00077
00078 def package_depends(pkg):
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):
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
00126
00127
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]
00149 pkg_dependences = sorted(set(package_depends(pkg) + args[2:]))
00150
00151 f.write(";; THIS FILE IS AUTOMATICALLY GENERATED\n")
00152 try:
00153
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
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)