From aa65be9856206448e066d630fe26e50109766649 Mon Sep 17 00:00:00 2001 From: Takuto Ikuta Date: Tue, 12 Dec 2023 04:39:55 +0000 Subject: [PATCH] autoninja: cache negative result of gcloud account check This is to remove 1~3 seconds from no-op build using autoninja. Bug: b/309720176 Change-Id: I2c15a43517d5a99eae794015c18c884a3f3fdef4 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/5092847 Commit-Queue: Takuto Ikuta Reviewed-by: Philipp Wollermann Reviewed-by: Scott Lee --- .gitignore | 3 +++ autoninja.py | 31 +++++++++++++++++++++++-------- tests/autoninja_test.py | 6 ++++++ 3 files changed, 32 insertions(+), 8 deletions(-) diff --git a/.gitignore b/.gitignore index 47bda262e7..22b610ad45 100644 --- a/.gitignore +++ b/.gitignore @@ -101,3 +101,6 @@ testing_support/google_appengine # Ignore the file that logs Python 2 scripts run during presubmits. /python2_usage.txt + +# Ignore the internal data used by autoninja. +/.autoninja* \ No newline at end of file diff --git a/autoninja.py b/autoninja.py index 9bf0d1b911..253f2847bb 100755 --- a/autoninja.py +++ b/autoninja.py @@ -19,10 +19,12 @@ import multiprocessing import os import platform import re +import shelve import shlex import shutil import subprocess import sys +import time import warnings import google.auth @@ -103,16 +105,29 @@ def _is_google_corp_machine_using_external_account(): if not _is_google_corp_machine(): return False - account = _adc_account() - if account and not account.endswith("@google.com"): - return True + with shelve.open(os.path.join(SCRIPT_DIR, ".autoninja")) as db: + last_false = db.get("last_false") + now = time.time() + if last_false is not None and now < last_false + 12 * 60 * 60: + # Do not check account if it is checked in last 12 hours. + return False - account = _gcloud_auth_account() - if not account: + account = _adc_account() + if account and not account.endswith("@google.com"): + return True + + account = _gcloud_auth_account() + if not account: + db["last_false"] = now + return False + + # Handle service account and google account as internal account. + if not (account.endswith("@google.com") + or account.endswith("gserviceaccount.com")): + return True + + db["last_false"] = now return False - # Handle service account and google account as internal account. - return not (account.endswith("@google.com") - or account.endswith("gserviceaccount.com")) def _quote_for_cmd(arg): diff --git a/tests/autoninja_test.py b/tests/autoninja_test.py index 5a17190453..d562b4bfdf 100755 --- a/tests/autoninja_test.py +++ b/tests/autoninja_test.py @@ -3,6 +3,7 @@ # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. +import glob import multiprocessing import os import os.path @@ -165,6 +166,11 @@ class AutoninjaTest(trial_dir.TestCase): adc_account, gcloud_auth_account, expected): + for shelve_file in glob.glob( + os.path.join(autoninja.SCRIPT_DIR, ".autoninja*")): + # Clear cache. + os.remove(shelve_file) + with mock.patch('autoninja._is_google_corp_machine', return_value=is_corp), mock.patch( 'autoninja._adc_account',