1f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)# Copyright 2013 The Chromium Authors. All rights reserved.
2f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)# Use of this source code is governed by a BSD-style license that can be
3f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)# found in the LICENSE file.
4f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
5f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)"""Chromium cr tool main module.
6f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
7f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)Holds the main function and all it's support code.
8f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)"""
9f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
10f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)import os
11f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)import sys
12f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)import cr
13f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
14f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)_CONTACT = 'iancottrell@chromium.org'
15f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
16f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
17f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)def Main():
18f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  """Chromium cr tool main function.
19f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
20f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  This is the main entry point of the cr tool, it finds and loads all the
21f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  plugins, creates the context and then activates and runs the specified
22f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  command.
23f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  """
24f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
25f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  # Add the users plugin dir to the cr.auto.user package scan
26f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  user_path = os.path.expanduser(os.path.join('~', '.config', 'cr'))
27f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  cr.auto.user.__path__.append(user_path)
28f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
29f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  cr.loader.Scan()
30f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
31f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  # Build the command context
32effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  with cr.base.context.Create(
33f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      description='The chrome dev build tool.',
34f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      epilog='Contact ' + _CONTACT + ' if you have issues with this tool.',
35effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      ) as context:
36f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
37effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    # Try to detect the current client information
38effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    cr.base.client.DetectClient()
39effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
40effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    # Install the sub-commands
41effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    for command in cr.Command.Plugins():
42effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      cr.context.AddSubParser(command)
43effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
44effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    # test for the special autocomplete command
45effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    if cr.context.autocompleting:
46effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      # After plugins are loaded so pylint: disable=g-import-not-at-top
47effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      cr.autocomplete.Complete()
48effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      return
49effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    # Speculative argument processing to add config specific args
50effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    cr.context.ParseArgs(True)
51effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    cr.plugin.Activate()
52effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    # At this point we should know what command we are going to use
53effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    command = cr.Command.GetActivePlugin()
54effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    # Do some early processing, in case it changes the build dir
55effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    if command:
56effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      command.EarlyArgProcessing()
57effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    # Update the activated set again, in case the early processing changed it
58effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    cr.plugin.Activate()
59effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    # Load the build specific configuration
60effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    found_build_dir = cr.base.client.LoadConfig()
61effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    # Final processing or arguments
62effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    cr.context.ParseArgs()
63effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    cr.plugin.Activate()
64effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    # If we did not get a command before, it might have been fixed.
65effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    if command is None:
66effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      command = cr.Command.GetActivePlugin()
67effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    # If the verbosity level is 3 or greater, then print the environment here
68effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    if cr.context.verbose >= 3:
69effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      cr.context.DumpValues(cr.context.verbose > 3)
70effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    if command is None:
71effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      print cr.context.Substitute('No command specified.')
72f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      exit(1)
73effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    if command.requires_build_dir:
74effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      if not found_build_dir:
75effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch        if not cr.context.Find('CR_OUT_FULL'):
76effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch          print cr.context.Substitute(
77effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch              'No build directory specified. Please use cr init to make one.')
78effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch        else:
79effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch          print cr.context.Substitute(
80effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch              'Build {CR_BUILD_DIR} not a valid build directory')
81effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch        exit(1)
82effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      if cr.context.Find('CR_VERSION') != cr.base.client.VERSION:
83effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch        print cr.context.Substitute(
84effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch            'Build {CR_BUILD_DIR} is for the wrong version of cr')
85effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch        print 'Please run cr init to reset it'
86effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch        exit(1)
87effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      cr.Platform.Prepare()
88effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    if cr.context.verbose >= 1:
89effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      print cr.context.Substitute(
90effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch          'Running cr ' + command.name + ' for {CR_BUILD_DIR}')
91effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    # Invoke the given command
92effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    command.Run()
93f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
94f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)if __name__ == '__main__':
95f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  sys.exit(Main())
96