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