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
00035 import roslib
00036 roslib.load_manifest('pr2_msgs')
00037
00038 import sys
00039 import struct
00040
00041 import unittest
00042
00043 import rostest
00044 import rosbag
00045 from rosbag.migration import MessageMigrator, checkbag, fixbag
00046
00047 import re
00048 from cStringIO import StringIO
00049 import os
00050
00051 import rospy
00052
00053 import math
00054
00055 migrator = MessageMigrator()
00056
00057 def repack(x):
00058 return struct.unpack('<f',struct.pack('<f',x))[0]
00059
00060 class TestPR2MsgsMigration(unittest.TestCase):
00061
00062
00063
00064
00065
00066
00067 def get_old_laser_scanner_signal(self):
00068 laser_scanner_signal_classes = self.load_saved_classes('pr2_mechanism_controllers_LaserScannerSignal.saved')
00069
00070 laser_scanner_signal = laser_scanner_signal_classes['pr2_mechanism_controllers/LaserScannerSignal']
00071
00072 return laser_scanner_signal(None, 123)
00073
00074 def get_new_laser_scanner_signal(self):
00075 from pr2_msgs.msg import LaserScannerSignal
00076
00077 return LaserScannerSignal(None, 123)
00078
00079 def test_laser_scanner_signal(self):
00080 self.do_test('laser_scanner_signal', self.get_old_laser_scanner_signal, self.get_new_laser_scanner_signal)
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105 def setUp(self):
00106 self.pkg_dir = roslib.packages.get_pkg_dir("pr2_msgs")
00107
00108
00109 def load_saved_classes(self,saved_msg):
00110 f = open("%s/test/saved/%s"%(self.pkg_dir,saved_msg), 'r')
00111
00112 type_line = f.readline()
00113 pat = re.compile(r"\[(.*)]:")
00114 type_match = pat.match(type_line)
00115
00116 self.assertTrue(type_match is not None, "Full definition file malformed. First line should be: '[my_package/my_msg]:'")
00117
00118 saved_type = type_match.groups()[0]
00119 saved_full_text = f.read()
00120
00121 saved_classes = roslib.genpy.generate_dynamic(saved_type,saved_full_text)
00122
00123 self.assertTrue(saved_classes is not None, "Could not generate class from full definition file.")
00124 self.assertTrue(saved_classes.has_key(saved_type), "Could not generate class from full definition file.")
00125
00126 return saved_classes
00127
00128 def do_test(self, name, old_msg, new_msg):
00129
00130 oldbag = "%s/test/%s_old.bag"%(self.pkg_dir,name)
00131 newbag = "%s/test/%s_new.bag"%(self.pkg_dir,name)
00132
00133
00134 with rosbag.Bag(oldbag, 'w') as bag:
00135 bag.write("topic", old_msg(), roslib.rostime.Time())
00136
00137
00138 res = checkbag(migrator, oldbag)
00139 self.assertTrue(not False in [m[1] == [] for m in res], 'Bag not ready to be migrated')
00140 res = fixbag(migrator, oldbag, newbag)
00141 self.assertTrue(res, 'Bag not converted successfully')
00142
00143
00144 msgs = [msg for msg in rosbag.Bag(newbag).read_messages()]
00145
00146
00147 m = new_msg()
00148 buff = StringIO()
00149 m.serialize(buff)
00150 m.deserialize(buff.getvalue())
00151
00152
00153
00154
00155
00156
00157
00158
00159 self.assertTrue(roslib.message.strify_message(msgs[0][1]) == roslib.message.strify_message(m))
00160
00161
00162
00163 os.remove(oldbag)
00164 os.remove(newbag)
00165
00166
00167 if __name__ == '__main__':
00168 rostest.unitrun('pr2_msgs', 'test_pr2_msgs_migration', TestPR2MsgsMigration, sys.argv)