gooftool: Remove ".*.auto" suffix in probe result.

When the platform add the device, it would add the .*.auto suffix to the
device name. It causes the probe module gets the different value form the
samecomponent. Therefore we remove the auto-generated suffix.

BUG=chromium:659921
TEST=py/gooftool/probe_unittest.py

Change-Id: I6ff55f8dfaa5831afd596420a384b0b2b92f9c8c
Reviewed-on: https://chromium-review.googlesource.com/411633
Commit-Ready: Chih-Yu Huang <akahuang@chromium.org>
Tested-by: Chih-Yu Huang <akahuang@chromium.org>
Reviewed-by: Hung-Te Lin <hungte@chromium.org>
diff --git a/py/gooftool/probe.py b/py/gooftool/probe.py
index 95c5b8c..6370078 100644
--- a/py/gooftool/probe.py
+++ b/py/gooftool/probe.py
@@ -1723,6 +1723,27 @@
   return UpdateVpd('RW_VPD', values)
 
 
+def RemoveAutoSuffix(probe_value_map):
+  """Remove the '.*.auto' suffix in the probe result.
+
+  When the platform add the device, it would add the .*.auto suffix to the
+  device name. It causes the probe module gets the different value form the same
+  component. Therefore we remove the auto-generated suffix.
+  Please see https://bugs.chromium.org/p/chromium/issues/detail?id=659921
+  """
+  def _RemoveSuffix(probe_value):
+    pattern = re.compile(r'\.[0-9]+\.auto$')
+    return {key: pattern.sub('', value) for key, value in probe_value.items()}
+
+  ret = {}
+  for comp_cls, probe_value in probe_value_map.items():
+    if isinstance(probe_value, list):
+      ret[comp_cls] = [_RemoveSuffix(item) for item in probe_value]
+    else:
+      ret[comp_cls] = _RemoveSuffix(probe_value)
+  return ret
+
+
 def Probe(target_comp_classes=None,
           fast_fw_probe=False,
           probe_volatile=True,
@@ -1847,6 +1868,9 @@
                              ('rw', ReadRwVpd())):
       for k, v in sorted(vpd_field.items()):
         volatiles['vpd.%s.%s' % (which, k)] = v
+
+  # Filter the ".*.auto" suffix
+  found_probe_value_map = RemoveAutoSuffix(found_probe_value_map)
   return ProbeResults(
       found_probe_value_map=found_probe_value_map,
       missing_component_classes=missing_component_classes,
diff --git a/py/gooftool/probe_unittest.py b/py/gooftool/probe_unittest.py
index f169d82..d41f2f6 100755
--- a/py/gooftool/probe_unittest.py
+++ b/py/gooftool/probe_unittest.py
@@ -158,5 +158,26 @@
     self.assertEquals(result, None)
 
 
+class UtilFunctionsTest(unittest.TestCase):
+  def testRemoveAutoSuffix(self):
+    probe_value_map = {
+        'audio_codec': {
+            'COMPACT_STR': 'hdmi-audio-codec.1.auto'}}
+    expected_result = {
+        'audio_codec': {
+            'COMPACT_STR': 'hdmi-audio-codec'}}
+    self.assertEquals(probe.RemoveAutoSuffix(probe_value_map), expected_result)
+
+    probe_value_map = {
+        'audio_codec': [
+            {'COMPACT_STR': 'hdmi-audio-codec.1.auto'},
+            {'COMPACT_STR': 'foo.20.auto'}]}
+    expected_result = {
+        'audio_codec': [
+            {'COMPACT_STR': 'hdmi-audio-codec'},
+            {'COMPACT_STR': 'foo'}]}
+    self.assertEquals(probe.RemoveAutoSuffix(probe_value_map), expected_result)
+
+
 if __name__ == '__main__':
   unittest.main()