Merge "Use push group VtsHalReplayTest for fingerprint replay test"
diff --git a/script/build/build_rule_gen.py b/script/build/build_rule_gen.py
index c8f6b6e..695f994 100755
--- a/script/build/build_rule_gen.py
+++ b/script/build/build_rule_gen.py
@@ -53,11 +53,11 @@
         Args:
             test_config_dir: string, directory storing the configurations.
         """
+        hal_list = self._vts_spec_parser.HalNamesAndVersions()
+        gen_file_paths = self.UpdateHalDirBuildRule(hal_list, test_config_dir)
         utils.RemoveFilesInDirIf(
             os.path.join(self._ANDROID_BUILD_TOP, test_config_dir),
-            lambda x: self._IsAutoGenerated(x))
-        hal_list = self._vts_spec_parser.HalNamesAndVersions()
-        self.UpdateHalDirBuildRule(hal_list, test_config_dir)
+            lambda x: self._IsAutoGenerated(x) and (x not in gen_file_paths))
 
     def UpdateHalDirBuildRule(self, hal_list, test_config_dir):
         """Updates build rules for vts drivers/profilers.
@@ -69,7 +69,12 @@
             hal_list: list of tuple of strings. For example,
                 [('vibrator', '1.3'), ('sensors', '1.7')]
             test_config_dir: string, directory storing the configurations.
+
+        Returns:
+            a list of strings where each string contains the path of a checked
+            or updated build file.
         """
+        file_paths = []
         for target in hal_list:
             hal_name = target[0]
             hal_version = target[1]
@@ -81,6 +86,8 @@
             file_path = os.path.join(hal_dir, 'build', 'Android.bp')
             utils.WriteBuildRule(file_path, self._VtsBuildRuleFromTemplate(
                 self._VTS_BUILD_TEMPLATE, hal_name, hal_version))
+            file_paths.append(file_path)
+        return file_paths
 
     def _VtsBuildRuleFromTemplate(self, template_path, hal_name, hal_version):
         """Returns build rules in string form by filling out a template.
diff --git a/script/build/build_rule_gen_utils.py b/script/build/build_rule_gen_utils.py
index 29462e9..1565eec 100644
--- a/script/build/build_rule_gen_utils.py
+++ b/script/build/build_rule_gen_utils.py
@@ -36,15 +36,29 @@
     Args:
       file_path: string, path to file to which to write.
       build_rule: string, build rule to be written into file.
-    """
-    print 'Updating %s' % file_path
-    dir_path = os.path.dirname(file_path)
 
+    Returns:
+      True if updated, False otherwise
+    """
+    exist = True
+    dir_path = os.path.dirname(file_path)
     if not os.path.exists(dir_path):
         os.makedirs(dir_path)
+        exist = False
+    elif not os.path.isfile(file_path):
+        exist = False
 
+    if exist:
+        with open(file_path, 'r') as existing_file:
+            existing_content = "".join(existing_file.readlines())
+        if build_rule == existing_content:
+            print 'Skipping %s' % file_path
+            return False
+
+    print 'Updating %s' % file_path
     with open(file_path, 'w') as bp_file:
         bp_file.write(build_rule)
+    return True
 
 
 def OnlySubdirsBpRule(warning_header, subdirs):
@@ -64,6 +78,7 @@
     result += ']\n'
     return result
 
+
 def RemoveFilesInDirIf(dir_path, condition):
     """Removes all files under directory under given condition.
 
@@ -76,4 +91,5 @@
         for f in files:
             abs_path = os.path.join(base, f)
             if condition(abs_path):
+                print "Removing", abs_path
                 os.remove(abs_path)