"""Util for fetching recipe bundles from CIPD."""
import logging
import pathlib
import subprocess
import sys
_THIS_DIR = pathlib.Path(__file__).resolve().parent
_CIPD_ROOT_BASE_DIR = _THIS_DIR.joinpath('.bundles')
_CHROME_RECIPE_BUNDLE = (
'infra_internal/recipe_bundles/chrome-internal.googlesource.com/'
'chrome/tools/build')
_CHROMIUM_RECIPE_BUNDLE = (
'infra/recipe_bundles/chromium.googlesource.com/chromium/tools/build')
_RECIPE_BUNDLE_VERSION = 'refs/heads/main'
def fetch_recipe_bundle(project, is_verbose):
cipd_root_dir = _CIPD_ROOT_BASE_DIR.joinpath(project)
exe = 'cipd.bat' if sys.platform == 'win32' else 'cipd'
if not cipd_root_dir.exists():
cmd = [exe, 'init', '-force', str(cipd_root_dir)]
logging.info('Initializing cipd root for bundle:')
logging.getLogger('basic_logger').info(' '.join(cmd))
subprocess.check_call(cmd)
recipe_bundle_package = _CHROME_RECIPE_BUNDLE
if project == 'chromium':
recipe_bundle_package = _CHROMIUM_RECIPE_BUNDLE
cmd = [
exe,
'install',
recipe_bundle_package,
_RECIPE_BUNDLE_VERSION,
'-root',
str(cipd_root_dir),
'-log-level',
'debug' if is_verbose else 'warning',
]
logging.info('[cyan]Running bundle install command:[/]')
logging.getLogger('basic_logger').info(' '.join(cmd))
subprocess.check_call(cmd, stdout=subprocess.DEVNULL)
return cipd_root_dir