releasetools: Add dict-like setter to BuildInfo.

BuildInfo already has dict-like getters. This CL adds the matching
setter method to keep it backward compatible with device-specific
scripts that expect a dict.

It also adds items() method that allows iterating the items.

Bug: 111087332
Test: python -m unittest test_ota_from_target_files.BuildInfoTest
Test: Generate an incremental OTA that sets info_dict in device-specific
      releasetools script.
Change-Id: Idd033f98a9186740f9da1a300d4c2bdddd9c1345
This commit is contained in:
Tao Bao
2018-07-06 10:13:59 -07:00
parent 8c01b71e2d
commit 667c7534ed
2 changed files with 25 additions and 1 deletions

View File

@@ -250,6 +250,8 @@ class BuildInfo(object):
def __init__(self, info_dict, oem_dicts): def __init__(self, info_dict, oem_dicts):
"""Initializes a BuildInfo instance with the given dicts. """Initializes a BuildInfo instance with the given dicts.
Note that it only wraps up the given dicts, without making copies.
Arguments: Arguments:
info_dict: The build-time info dict. info_dict: The build-time info dict.
oem_dicts: A list of OEM dicts (which is parsed from --oem_settings). Note oem_dicts: A list of OEM dicts (which is parsed from --oem_settings). Note
@@ -289,9 +291,15 @@ class BuildInfo(object):
def __getitem__(self, key): def __getitem__(self, key):
return self.info_dict[key] return self.info_dict[key]
def __setitem__(self, key, value):
self.info_dict[key] = value
def get(self, key, default=None): def get(self, key, default=None):
return self.info_dict.get(key, default) return self.info_dict.get(key, default)
def items(self):
return self.info_dict.items()
def GetBuildProp(self, prop): def GetBuildProp(self, prop):
"""Returns the inquired build property.""" """Returns the inquired build property."""
try: try:

View File

@@ -190,6 +190,16 @@ class BuildInfoTest(unittest.TestCase):
self.assertRaises(KeyError, self.assertRaises(KeyError,
lambda: target_info['build.prop']['ro.build.foo']) lambda: target_info['build.prop']['ro.build.foo'])
def test___setitem__(self):
target_info = BuildInfo(copy.deepcopy(self.TEST_INFO_DICT), None)
self.assertEqual('value1', target_info['property1'])
target_info['property1'] = 'value2'
self.assertEqual('value2', target_info['property1'])
self.assertEqual('build-foo', target_info['build.prop']['ro.build.foo'])
target_info['build.prop']['ro.build.foo'] = 'build-bar'
self.assertEqual('build-bar', target_info['build.prop']['ro.build.foo'])
def test_get(self): def test_get(self):
target_info = BuildInfo(self.TEST_INFO_DICT, None) target_info = BuildInfo(self.TEST_INFO_DICT, None)
self.assertEqual('value1', target_info.get('property1')) self.assertEqual('value1', target_info.get('property1'))
@@ -209,6 +219,12 @@ class BuildInfoTest(unittest.TestCase):
self.assertRaises(KeyError, self.assertRaises(KeyError,
lambda: target_info.get('build.prop')['ro.build.foo']) lambda: target_info.get('build.prop')['ro.build.foo'])
def test_items(self):
target_info = BuildInfo(self.TEST_INFO_DICT, None)
items = target_info.items()
self.assertIn(('property1', 'value1'), items)
self.assertIn(('property2', 4096), items)
def test_GetBuildProp(self): def test_GetBuildProp(self):
target_info = BuildInfo(self.TEST_INFO_DICT, None) target_info = BuildInfo(self.TEST_INFO_DICT, None)
self.assertEqual('build-foo', target_info.GetBuildProp('ro.build.foo')) self.assertEqual('build-foo', target_info.GetBuildProp('ro.build.foo'))