From 99399caeacf6712571b3e188783e66aea517ae6f Mon Sep 17 00:00:00 2001 From: Gavin Mak Date: Fri, 11 Dec 2020 20:56:03 +0000 Subject: [PATCH] [owners] Add BatchListOwners BatchListOwners uses git_common's ScopedPool method to run ListOwnersForFile in parallel for multiple paths. Change-Id: I545072e16181407665c8b009f87fccfb7355d7d8 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/2585657 Reviewed-by: Edward Lesmes Commit-Queue: Gavin Mak --- owners_client.py | 7 +++++++ tests/owners_client_test.py | 17 +++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/owners_client.py b/owners_client.py index 5ceec08b5d..5dabe5dc02 100644 --- a/owners_client.py +++ b/owners_client.py @@ -7,6 +7,7 @@ import os import random import gerrit_util +import git_common import owners as owners_db import scm @@ -66,6 +67,12 @@ class OwnersClient(object): """ raise Exception('Not implemented') + def BatchListOwners(self, project, branch, paths): + """Returns a dictionary {path: [owners]}.""" + with git_common.ScopedPool(kind='threads') as pool: + return dict(pool.imap_unordered( + lambda p: (p, self.ListOwnersForFile(project, branch, p)), paths)) + def GetChangeApprovalStatus(self, change_id): """Check the approval status for the latest revision_id in a change. diff --git a/tests/owners_client_test.py b/tests/owners_client_test.py index e25c1c48f0..f80bdcec74 100644 --- a/tests/owners_client_test.py +++ b/tests/owners_client_test.py @@ -256,6 +256,23 @@ class OwnersClientTest(unittest.TestCase): ['abc', 'acb', 'bac', 'bca', 'cab', 'cba']), (chris, bob)) + def testBatchListOwners(self): + self.client.owners_by_path = { + 'bar/everyone/foo.txt': [alice, bob], + 'bar/everyone/bar.txt': [bob], + 'bar/foo/': [bob, chris] + } + + self.assertEquals( + { + 'bar/everyone/foo.txt': [alice, bob], + 'bar/everyone/bar.txt': [bob], + 'bar/foo/': [bob, chris] + }, + self.client.BatchListOwners( + 'project', 'branch', + ['bar/everyone/foo.txt', 'bar/everyone/bar.txt', 'bar/foo/'])) + if __name__ == '__main__': unittest.main()