Add preference of private overlays for finding ACLs

This heuristic embodies the notion that ACLs to be used are probably
not kept in the public space unless everything is public.

BUG=chromium:336504
TEST=unittests.

Change-Id: I810fead5054dd7ee0cd5c26005b3c5aa9f31ed5e
Reviewed-on: https://chromium-review.googlesource.com/184571
Tested-by: Peter Mayo <petermayo@chromium.org>
Reviewed-by: David James <davidjames@chromium.org>
Reviewed-by: Aviv Keshet <akeshet@chromium.org>
Commit-Queue: Peter Mayo <petermayo@chromium.org>
diff --git a/scripts/upload_prebuilts.py b/scripts/upload_prebuilts.py
index 459f1e6..720da0c 100644
--- a/scripts/upload_prebuilts.py
+++ b/scripts/upload_prebuilts.py
@@ -225,12 +225,15 @@
 
   return upload_files
 
-def GetBoardOverlay(build_path, target):
+def GetBoardOverlay(build_path, target, prefer):
   """Get the path to the board variant.
 
    Args:
      build_path: The path to the root of the build directory
      target: The target board as a BuildTarget object.
+     prefer: If we find an overlay that start with this, return
+             the last one of them.  Useful for generic override
+             variants that occur after private board overlays.
 
    Returns:
      The last overlay configured for the given board as a string.
@@ -239,6 +242,11 @@
   overlays = portage_utilities.FindOverlays(constants.BOTH_OVERLAYS, board,
                                             buildroot=build_path)
   # We only care about the last entry.
+  if prefer:
+    for path in reversed(overlays):
+      if path.startswith(prefer):
+        return path
+
   return overlays[-1]
 
 
@@ -261,7 +269,7 @@
     make_path = _PREBUILT_MAKE_CONF[target]
   else:
     # We are a board
-    board = GetBoardOverlay(build_path, target)
+    board = GetBoardOverlay(build_path, target, None)
     make_path = os.path.join(board, 'prebuilt.conf')
 
   return make_path
@@ -748,7 +756,8 @@
   if options.private:
     binhost_base_url = options.upload
     if target:
-      board_path = GetBoardOverlay(options.build_path, target)
+      board_path = GetBoardOverlay(options.build_path, target,
+                                   _PRIVATE_OVERLAY_DIR)
       acl = os.path.join(board_path, _GOOGLESTORAGE_ACL_FILE)
 
   uploader = PrebuiltUploader(options.upload, acl, binhost_base_url,
diff --git a/scripts/upload_prebuilts_unittest.py b/scripts/upload_prebuilts_unittest.py
index 072f02d..6924674 100755
--- a/scripts/upload_prebuilts_unittest.py
+++ b/scripts/upload_prebuilts_unittest.py
@@ -415,7 +415,8 @@
     self.mox.StubOutWithMock(prebuilt, 'GetBoardOverlay')
     fake_overlay_path = '/fake_path'
     prebuilt.GetBoardOverlay(
-        options.build_path, options.board).AndReturn(fake_overlay_path)
+        options.build_path, options.board, mox.IgnoreArg()).AndReturn(
+        fake_overlay_path)
     expected_gs_acl_path = os.path.join(fake_overlay_path,
                                         prebuilt._GOOGLESTORAGE_ACL_FILE)
     prebuilt.PrebuiltUploader.__init__(options.upload, expected_gs_acl_path,