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 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))
00072 return depends
00073
00074 def package_depends(pkg):
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):
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
00122
00123
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]
00147 pkg_dependences = package_depends(pkg) + args[2:]
00148
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)