xbuddy: Expose alias lookup method.
This is needed for applications (e.g. cros flash) to be able to properly
map image aliases (e.g. 'project_sdk') into the correct XBuddy path.
The signature is slightly changed by making the board and version
arguments optional, deferring to pre-initialized board/version.
BUG=brillo:608
TEST=Unit test
Change-Id: I5fa5ad77eff7768e078f842f8c9a14c369535fca
Reviewed-on: https://chromium-review.googlesource.com/267125
Trybot-Ready: Gilad Arnold <garnold@chromium.org>
Tested-by: Gilad Arnold <garnold@chromium.org>
Reviewed-by: Chris Sosa <sosa@chromium.org>
Commit-Queue: Gilad Arnold <garnold@chromium.org>
diff --git a/xbuddy.py b/xbuddy.py
index 36cc8b2..384de49 100644
--- a/xbuddy.py
+++ b/xbuddy.py
@@ -267,16 +267,18 @@
except ConfigParser.Error:
return 5
- def _LookupAlias(self, alias, board, version):
+ def LookupAlias(self, alias, board=None, version=None):
"""Given the full xbuddy config, look up an alias for path rewrite.
Args:
alias: The xbuddy path that could be one of the aliases in the
rewrite table.
board: The board to fill in with when paths are rewritten. Can be from
- the update request xml or the default board from devserver.
+ the update request xml or the default board from devserver. If None,
+ defers to the value given during XBuddy initialization.
version: The version to fill in when rewriting paths. Could be a specific
- version number or a version alias like LATEST.
+ version number or a version alias like LATEST. If None, defers to the
+ value given during XBuddy initialization, or LATEST.
Returns:
A pair (val, suffix) where val is the rewritten path, or the original
@@ -301,7 +303,8 @@
# Fill in the board and version.
val = val.replace("BOARD", "%(board)s")
val = val.replace("VERSION", "%(version)s")
- val = val % {'board': board, 'version': version}
+ val = val % {'board': board or self._board,
+ 'version': version or self._version or LATEST}
_Log("Path is %s, location suffix is %s", val, suffix)
return val, suffix
@@ -737,7 +740,8 @@
default_board = self._board if self._board else board
default_version = self._version or version or LATEST
# Rewrite the path if there is an appropriate default.
- path, suffix = self._LookupAlias(path, default_board, default_version)
+ path, suffix = self.LookupAlias(path, board=default_board,
+ version=default_version)
# Parse the path.
image_type, board, version, is_local = self._InterpretPath(
path, default_board, default_version)
diff --git a/xbuddy_unittest.py b/xbuddy_unittest.py
index 16cccb3..c5e7ec3 100755
--- a/xbuddy_unittest.py
+++ b/xbuddy_unittest.py
@@ -77,7 +77,7 @@
self.mox.VerifyAll()
def testLookupAliasPathRewrite(self):
- """Tests _LookupAlias of path rewrite, including keyword substitution."""
+ """Tests LookupAlias of path rewrite, including keyword substitution."""
alias = 'foobar'
path = 'remote/BOARD/VERSION/test'
self.mox.StubOutWithMock(self.mock_xb.config, 'get')
@@ -86,10 +86,11 @@
self.mock_xb.config.get('PATH_REWRITES', alias).AndReturn(path)
self.mox.ReplayAll()
self.assertEqual(('remote/parrot/1.2.3/test', '-release'),
- self.mock_xb._LookupAlias(alias, 'parrot', '1.2.3'))
+ self.mock_xb.LookupAlias(alias, board='parrot',
+ version='1.2.3'))
def testLookupAliasSuffix(self):
- """Tests _LookupAlias of location suffix."""
+ """Tests LookupAlias of location suffix."""
alias = 'foobar'
suffix = '-random'
self.mox.StubOutWithMock(self.mock_xb.config, 'get')
@@ -98,10 +99,11 @@
ConfigParser.Error())
self.mox.ReplayAll()
self.assertEqual((alias, suffix),
- self.mock_xb._LookupAlias(alias, 'parrot', '1.2.3'))
+ self.mock_xb.LookupAlias(alias, board='parrot',
+ version='1.2.3'))
def testLookupAliasPathRewriteAndSuffix(self):
- """Tests _LookupAlias with both path rewrite and suffix."""
+ """Tests LookupAlias with both path rewrite and suffix."""
alias = 'foobar'
path = 'remote/BOARD/VERSION/test'
suffix = '-random'
@@ -110,7 +112,8 @@
self.mock_xb.config.get('PATH_REWRITES', alias).AndReturn(path)
self.mox.ReplayAll()
self.assertEqual(('remote/parrot/1.2.3/test', suffix),
- self.mock_xb._LookupAlias(alias, 'parrot', '1.2.3'))
+ self.mock_xb.LookupAlias(alias, board='parrot',
+ version='1.2.3'))
def testResolveVersionToBuildId_Official(self):
"""Check _ResolveVersionToBuildId recognizes aliases for official builds."""