rosruby_genmsg.py
Go to the documentation of this file.
00001 #! /usr/bin/env python
00002 
00003 # msg/srv generator for precompiled system
00004 # It is useful if you are using precompiled ROS system,
00005 # because it needs root access and there are ROS_NOBUILD file in the packages.
00006 # This script generates *.rb files in the rosruby dir.
00007 
00008 import sys
00009 import os
00010 
00011 from rospkg import RosPack
00012 
00013 import genmsg
00014 import rosmsg
00015 from genrb.generator import msg_generator
00016 from genrb.generator import srv_generator
00017 
00018 def get_all_deps(packages):
00019     rp = RosPack()
00020     all_deps = set()
00021     for pack in packages:
00022         depends = rp.get_depends(pack)
00023         for dep in depends:
00024             all_deps.add(dep)
00025     for pack in packages:
00026         all_deps.add(pack)
00027     return all_deps
00028 
00029 
00030 def generate_rb_files(msg_srv, generator, list_msgs, load_msg_by_type, base_dir, overwrite=True):
00031     search_path = {}
00032     for p in rospack.list():
00033         search_path[p] = [os.path.join(rospack.get_path(p), 'msg'),
00034                           os.path.join(rospack.get_path(p), 'srv')]
00035     for pack in all_dep_packages:
00036         output_prefix = base_dir
00037         output_dir = "%s/%s/"%(output_prefix, pack)
00038         all_pkg = list_msgs(pack)
00039         if all_pkg and not os.path.isdir(output_dir):
00040             os.makedirs(output_dir)
00041         for pkg_msg in all_pkg:
00042             spec = load_msg_by_type(context, pkg_msg, search_path)
00043             output_file = "%s/%s.rb"%(output_prefix, pkg_msg)
00044             if os.path.isfile(output_file) and not overwrite:
00045                 print "%s already exists skipping it (please use --overwrite to regenerate it)"%output_file
00046             else:
00047                 with open(output_file, 'w') as f:
00048                     for l in generator(context, spec, search_path):
00049                         f.write(l)
00050 
00051 
00052 if __name__ == "__main__":
00053     import argparse
00054     parser = argparse.ArgumentParser(description='generate rosruby msg/srv files')
00055     parser.add_argument('packages', nargs='+')
00056     parser.add_argument('-d', '--output-dir')
00057 #    parser.add_argument('--overwrite', default=False, help='overwrite .rb file if it is already exists. default: False', action='store_true')
00058     args = parser.parse_args()
00059     packages = args.packages
00060     base_dir = os.environ.get("HOME") + "/.ros/rosruby"
00061     if args.output_dir:
00062         base_dir = args.output_dir
00063     rospack = RosPack()
00064 
00065     context = genmsg.MsgContext.create_default()
00066     all_dep_packages = get_all_deps(packages)
00067 
00068     generate_rb_files('msg', msg_generator, rosmsg.list_msgs, genmsg.load_msg_by_type, base_dir)
00069     generate_rb_files('srv', srv_generator, rosmsg.list_srvs, genmsg.load_srv_by_type, base_dir)


rosruby
Author(s): Takashi Ogura
autogenerated on Wed Aug 26 2015 16:09:57