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