diff options
author | iancottrell@chromium.org <iancottrell@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-11-20 14:06:35 +0000 |
---|---|---|
committer | iancottrell@chromium.org <iancottrell@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-11-20 14:06:35 +0000 |
commit | 54e15685c9ccbe9d810dc555a7e594104d1b7050 (patch) | |
tree | aa6ab87068ab77b28aa91423c424c1be8f571bc8 | |
parent | c7ff71b9705bf74a16713d0ea61ff69811caf939 (diff) | |
download | chromium_src-54e15685c9ccbe9d810dc555a7e594104d1b7050.zip chromium_src-54e15685c9ccbe9d810dc555a7e594104d1b7050.tar.gz chromium_src-54e15685c9ccbe9d810dc555a7e594104d1b7050.tar.bz2 |
Adding the main function and remaining pieces.
BUG=316397
Committed: https://src.chromium.org/viewvc/chrome?view=rev&revision=235983
Review URL: https://codereview.chromium.org/71483003
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@236244 0039d316-1c4b-4281-b951-d872f2087c98
-rwxr-xr-x | tools/cr/cr.sh | 38 | ||||
-rw-r--r-- | tools/cr/cr/auto/user/__init__.py | 5 | ||||
-rw-r--r-- | tools/cr/cr/autocomplete.py | 27 | ||||
-rw-r--r-- | tools/cr/main.py | 94 |
4 files changed, 164 insertions, 0 deletions
diff --git a/tools/cr/cr.sh b/tools/cr/cr.sh new file mode 100755 index 0000000..cd38aba --- /dev/null +++ b/tools/cr/cr.sh @@ -0,0 +1,38 @@ +# Copyright 2013 The Chromium Authors. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +# Source this file into your shell to gain the cr function and tab completion +# for it + +cr_base_dir=$(dirname $(realpath "${BASH_SOURCE:-$0}")) +cr_main="${cr_base_dir}/main.py" +cr_exec="PYTHONDONTWRITEBYTECODE=1 python ${cr_main}" + +# The main entry point to the cr tool. +# Invokes the python script with pyc files turned off. +function cr() { + env $cr_exec "$@" +} + +# Attempts to cd to the root/src of the current client. +function crcd() { + cd $(cr info -s CR_SRC) +} + +# Add to your PS1 to have the current selected output directory in your prompt +function _cr_ps1() { + cr info -s CR_OUT_FULL +} + +# The tab completion handler, delegates into the python script. +function _cr_complete() { + COMPREPLY=() + local cur="${COMP_WORDS[COMP_CWORD]}" + local main="python -B "${cr_main}")" + local completions="$(env COMP_CWORD=${COMP_CWORD} COMP_WORD=${cur} $cr_exec)" + COMPREPLY=( $(compgen -W "${completions}" -- ${cur}) ) +} + +# Setup the bash auto complete +complete -F _cr_complete cr diff --git a/tools/cr/cr/auto/user/__init__.py b/tools/cr/cr/auto/user/__init__.py new file mode 100644 index 0000000..9f93de8 --- /dev/null +++ b/tools/cr/cr/auto/user/__init__.py @@ -0,0 +1,5 @@ +# Copyright 2013 The Chromium Authors. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +"""A package to hold the modules loaded from the users home directory."""
\ No newline at end of file diff --git a/tools/cr/cr/autocomplete.py b/tools/cr/cr/autocomplete.py new file mode 100644 index 0000000..ab9ce2f --- /dev/null +++ b/tools/cr/cr/autocomplete.py @@ -0,0 +1,27 @@ +# Copyright 2013 The Chromium Authors. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +"""Bash auto completion support. + +Contains the special mode that returns lists of possible completions for the +current command line. +""" + +import cr + + +def Complete(context): + """Attempts to build a completion list for the current command line. + + COMP_WORD contains the word that is being completed, and COMP_CWORD has + the index of that word on the command line. + + Args: + context: The cr context object, of type cr.context.Context. + """ + + _ = context + # TODO(iancottrell): support auto complete of more than just the command + # try to parse the command line using parser + print ' '.join(command.name for command in cr.Command.Plugins()) diff --git a/tools/cr/main.py b/tools/cr/main.py new file mode 100644 index 0000000..dced8cd --- /dev/null +++ b/tools/cr/main.py @@ -0,0 +1,94 @@ +# Copyright 2013 The Chromium Authors. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +"""Chromium cr tool main module. + +Holds the main function and all it's support code. +""" + +import os +import sys +import cr +import cr.auto.user +import cr.autocomplete +import cr.loader + +_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. + """ + + # Add the users plugin dir to the cr.auto.user package scan + user_path = os.path.expanduser(os.path.join('~', '.config', 'cr')) + cr.auto.user.__path__.append(user_path) + + cr.loader.Scan() + + # Build the command context + context = cr.Context( + description='The chrome dev build tool.', + epilog='Contact ' + _CONTACT + ' if you have issues with this tool.', + ) + # Install the sub-commands + for command in cr.Command.Plugins(): + context.AddSubParser(command) + + # test for the special autocomplete command + if context.autocompleting: + # After plugins are loaded so pylint: disable=g-import-not-at-top + cr.autocomplete.Complete(context) + return + # Speculative argument processing to add config specific args + context.ParseArgs(True) + cr.plugin.Activate(context) + # At this point we should know what command we are going to use + command = cr.Command.GetActivePlugin(context) + # Do some early processing, in case it changes the build dir + if command: + command.EarlyArgProcessing(context) + # Update the activated set again, in case the early processing changed it + cr.plugin.Activate(context) + # Load the build specific configuration + found_build_dir = cr.base.client.LoadConfig(context) + # Final processing or arguments + context.ParseArgs() + cr.plugin.Activate(context) + # If we did not get a command before, it might have been fixed. + if command is None: + command = cr.Command.GetActivePlugin(context) + # If the verbosity level is 3 or greater, then print the environment here + if context.verbose >= 3: + context.DumpValues(context.verbose > 3) + if command is None: + print context.Substitute('No command specified.') + exit(1) + if command.requires_build_dir: + if not found_build_dir: + if not context.Find('CR_OUT_FULL'): + print context.Substitute( + 'No build directory specified. Please use cr init to make one.') + else: + print context.Substitute( + 'Build {CR_BUILD_DIR} not a valid build directory') + exit(1) + if context.Find('CR_VERSION') != cr.base.client.VERSION: + print 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(context) + if context.verbose >= 1: + print context.Substitute( + 'Running cr ' + command.name + ' for {CR_BUILD_DIR}') + # Invoke the given command + command.Run(context) + +if __name__ == '__main__': + sys.exit(Main()) |