#!/usr/bin/env python

###############################################################################
# Copyright 2017 The Apollo Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
###############################################################################
"""
Stat static info.
Usage:
    ./stat_static_info.py <bag_file>
    ./stat_static_info.py <task_dir>  # <task_dir> contains a list of bags.
"""

import collections
import glob
import os
import sys

from modules.data.proto.static_info_pb2 import StaticInfo
from rosbag.bag import Bag

kStaticInfoTopic = '/apollo/monitor/static_info'


class StaticInfoCalculator(object):
    """Stat static info."""

    def __init__(self):
        self.vehicle_name = None
        self.vehicle_vin = None

    def process_file(self, bag_file):
        """
        Extract information from bag file.
        Return True if we are done collecting all information.
        """
        if not os.path.isfile(bag_file):
            return False
        try:
            with Bag(bag_file, 'r') as bag:
                for _, msg, _ in bag.read_messages(topics=[kStaticInfoTopic]):
                    if msg.vehicle.name:
                        self.vehicle_name = msg.vehicle.name.lower()
                    if msg.vehicle.license.vin:
                        self.vehicle_vin = msg.vehicle.license.vin
                    if self.done():
                        return True
        except:
            return False
        return self.done()

    def process_dir(self, bag_dir):
        """Process a directory."""
        files = []
        dirs = []
        for f in os.listdir(bag_dir):
            f_path = os.path.join(bag_dir, f)
            if os.path.isfile(f_path):
                files.append(f_path)
            elif os.path.isdir(f_path):
                dirs.append(f_path)
            # Ignore links.

        # Reverse sort the bags or dirs, trying to get info from the latest.
        for bag in sorted(files, reverse=True):
            if self.process_file(bag):
                return True
        for subdir in sorted(dirs, reverse=True):
            if self.process_dir(subdir):
                return True
        return False

    def done(self):
        """Check if all info are collected."""
        return self.vehicle_name and self.vehicle_vin


def main(path):
    """Process a path."""
    calc = StaticInfoCalculator()
    calc.process_file(path) if os.path.isfile(path) else calc.process_dir(path)

    # Output result, which might be None
    print 'vehicle_name:', calc.vehicle_name
    print 'vehicle_vin:', calc.vehicle_vin

if __name__ == '__main__':
    main(sys.argv[1])

@@ -927,39 +927,8 @@
 %22%22%0A%0A
-import collections%0Aimport glob%0A
 impo
@@ -1080,16 +1080,52 @@
 c_info'%0A
+kTopics = %5B%0A    kStaticInfoTopic,%0A%5D%0A
 %0A%0Aclass 
@@ -1445,74 +1445,8 @@
 %22%22%22%0A
-        if not os.path.isfile(bag_file):%0A            return False%0A
     
@@ -1560,26 +1560,15 @@
 ics=
-%5BkStaticInfo
+k
 Topic
-%5D
+s
 ):%0A 
@@ -2717,37 +2717,73 @@
     
-return self.
+# Currently we only care about 
 vehicle
-_
+ 
 name
- and 
+.%0A        return bool(
 self
@@ -2791,19 +2791,21 @@
 vehicle_
-vin
+name)
 %0A%0A%0Adef m
@@ -2883,45 +2883,54 @@
     
-calc.process_file(path) if os.path.is
+if os.path.isfile(path):%0A        calc.process_
 file
@@ -2939,13 +2939,26 @@
 ath)
+%0A   
  else
+:%0A       
  cal

Stop scanning when finding vehicle name.