"""Chromium cr tool main module.
Holds the main function and all it's support code.
"""
from __future__ import print_function
import os
import sys
import cr
_CONTACT = 'iancottrell@chromium.org'
def Main():
"""Chromium cr tool main function.
This is the main entry point of the cr tool, it finds and loads all the
plugins, creates the context and then activates and runs the specified
command.
"""
user_path = os.path.expanduser(os.path.join('~', '.config', 'cr'))
cr.auto.user.__path__.append(user_path)
cr.loader.Scan()
with cr.base.context.Create(
description='The chrome dev build tool.',
epilog='Contact ' + _CONTACT + ' if you have issues with this tool.',
) as context:
cr.base.client.DetectClient()
for command in cr.Command.Plugins():
cr.context.AddSubParser(command)
if cr.context.autocompleting:
cr.autocomplete.Complete()
return
cr.context.ParseArgs(True)
cr.plugin.Activate()
command = cr.Command.GetActivePlugin()
if command:
command.EarlyArgProcessing()
cr.plugin.Activate()
found_build_dir = cr.base.client.LoadConfig()
cr.plugin.Activate()
cr.context.ParseArgs()
if command is None:
command = cr.Command.GetActivePlugin()
if cr.context.verbose >= 3:
cr.context.DumpValues(cr.context.verbose > 3)
if command is None:
print(cr.context.Substitute('No command specified.'))
exit(1)
if command.requires_build_dir:
if not found_build_dir:
if not cr.context.Find('CR_OUT_FULL'):
print(cr.context.Substitute(
'No build directory specified. Please use cr init to make one.'))
else:
print(cr.context.Substitute(
'Build {CR_BUILD_DIR} not a valid build directory'))
exit(1)
if cr.context.Find('CR_VERSION') != cr.base.client.VERSION:
print(cr.context.Substitute(
'Build {CR_BUILD_DIR} is for the wrong version of cr'))
print('Please run cr init to reset it')
exit(1)
cr.Platform.Prepare()
if cr.context.verbose >= 1:
print(cr.context.Substitute('Running cr ' + command.name +
' for {CR_BUILD_DIR}'))
command.Run()
if __name__ == '__main__':
sys.exit(Main())