main.py 3.2 KB

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