Christopher Wiley | ccd92f9 | 2015-04-14 14:14:19 -0700 | [diff] [blame] | 1 | # Copyright 2015 The Chromium OS 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 | """This script moves ebuilds between 'stable' and 'live' states. |
| 6 | |
| 7 | By default 'stable' ebuilds point at and build from source at the |
| 8 | last known good commit. Moving an ebuild to 'live' (via cros_workon start) |
| 9 | is intended to support development. The current source tip is fetched, |
| 10 | source modified and built using the unstable 'live' (9999) ebuild. |
| 11 | |
| 12 | commands: |
| 13 | start: Moves an ebuild to live (intended to support development) |
| 14 | stop: Moves an ebuild to stable (use last known good) |
| 15 | info: Print package name, repo name, and source directory. |
| 16 | list: List of live ebuilds (workon ebuilds if --all) |
| 17 | list-all: List all of the live ebuilds for all setup boards |
| 18 | iterate: For each ebuild, cd to the source dir and run a command" |
| 19 | |
| 20 | """ |
| 21 | |
| 22 | from __future__ import print_function |
| 23 | |
| 24 | from chromite.lib import brick_lib |
| 25 | from chromite.lib import commandline |
| 26 | from chromite.lib import cros_build_lib |
| 27 | from chromite.lib import terminal |
| 28 | from chromite.lib import workon_helper |
| 29 | |
| 30 | |
| 31 | def main(argv): |
Christopher Wiley | 9385ea1 | 2015-05-08 13:18:45 -0700 | [diff] [blame] | 32 | shared = commandline.SharedParser() |
| 33 | shared.add_argument('--board', default=cros_build_lib.GetDefaultBoard(), |
Christopher Wiley | ccd92f9 | 2015-04-14 14:14:19 -0700 | [diff] [blame] | 34 | help='The board to set package keywords for.') |
Christopher Wiley | 9385ea1 | 2015-05-08 13:18:45 -0700 | [diff] [blame] | 35 | shared.add_argument('--brick', help='The brick to set package keywords for.') |
| 36 | shared.add_argument('--host', default=False, action='store_true', |
Christopher Wiley | ccd92f9 | 2015-04-14 14:14:19 -0700 | [diff] [blame] | 37 | help='Uses the host instead of board') |
Christopher Wiley | 9385ea1 | 2015-05-08 13:18:45 -0700 | [diff] [blame] | 38 | shared.add_argument('--remote', default='', |
Christopher Wiley | ccd92f9 | 2015-04-14 14:14:19 -0700 | [diff] [blame] | 39 | help='For non-workon projects, the git remote to use.') |
Christopher Wiley | 9385ea1 | 2015-05-08 13:18:45 -0700 | [diff] [blame] | 40 | shared.add_argument('--revision', default='', |
Christopher Wiley | ccd92f9 | 2015-04-14 14:14:19 -0700 | [diff] [blame] | 41 | help='Use to override the manifest defined default ' |
| 42 | 'revision used for a project') |
Christopher Wiley | 9385ea1 | 2015-05-08 13:18:45 -0700 | [diff] [blame] | 43 | shared.add_argument('--command', default='git status', dest='iterate_command', |
Christopher Wiley | ccd92f9 | 2015-04-14 14:14:19 -0700 | [diff] [blame] | 44 | help='The command to be run by forall.') |
Christopher Wiley | 9385ea1 | 2015-05-08 13:18:45 -0700 | [diff] [blame] | 45 | shared.add_argument('--workon_only', default=False, action='store_true', |
Christopher Wiley | ccd92f9 | 2015-04-14 14:14:19 -0700 | [diff] [blame] | 46 | help='Apply to packages that have a workon ebuild only') |
Christopher Wiley | 9385ea1 | 2015-05-08 13:18:45 -0700 | [diff] [blame] | 47 | shared.add_argument('--all', default=False, action='store_true', |
Christopher Wiley | ccd92f9 | 2015-04-14 14:14:19 -0700 | [diff] [blame] | 48 | help='Apply to all possible packages for the ' |
| 49 | 'given command (overrides workon_only)') |
Christopher Wiley | 9385ea1 | 2015-05-08 13:18:45 -0700 | [diff] [blame] | 50 | |
| 51 | parser = commandline.ArgumentParser(description=__doc__, parents=[shared,]) |
| 52 | |
| 53 | # Add the shared 'packages' argument after creating the main parser so that |
| 54 | # it is only bound/shared with the subcommands and doesn't confuse argparse. |
| 55 | shared.add_argument('packages', nargs='*', |
Christopher Wiley | ccd92f9 | 2015-04-14 14:14:19 -0700 | [diff] [blame] | 56 | help='The packages to run command against.') |
Christopher Wiley | 9385ea1 | 2015-05-08 13:18:45 -0700 | [diff] [blame] | 57 | |
| 58 | commands = parser.add_subparsers(dest='command', title='commands') |
| 59 | commands.add_parser( |
| 60 | 'start', parents=[shared,], |
| 61 | help='Moves an ebuild to live (intended to support development)') |
| 62 | commands.add_parser( |
| 63 | 'stop', parents=[shared,], |
| 64 | help='Moves an ebuild to stable (use last known good)') |
| 65 | commands.add_parser( |
| 66 | 'info', parents=[shared,], |
| 67 | help='Print package name, repo name, and source directory.') |
| 68 | commands.add_parser( |
| 69 | 'list', parents=[shared,], |
| 70 | help='List of live ebuilds (workon ebuilds if --all)') |
| 71 | commands.add_parser( |
| 72 | 'list-all', parents=[shared,], |
| 73 | help='List all of the live ebuilds for all setup boards') |
| 74 | commands.add_parser( |
| 75 | 'iterate', parents=[shared,], |
| 76 | help='For each ebuild, cd to the source dir and run a command') |
| 77 | |
Christopher Wiley | ccd92f9 | 2015-04-14 14:14:19 -0700 | [diff] [blame] | 78 | options = parser.parse_args(argv) |
| 79 | options.Freeze() |
| 80 | |
| 81 | if options.command == 'list-all': |
| 82 | board_to_packages = workon_helper.ListAllWorkedOnAtoms() |
| 83 | color = terminal.Color() |
| 84 | for board in sorted(board_to_packages): |
| 85 | print(color.Start(color.GREEN) + board + ':' + color.Stop()) |
| 86 | for package in board_to_packages[board]: |
| 87 | print(' ' + package) |
| 88 | print('') |
| 89 | return 0 |
| 90 | |
| 91 | # TODO(wiley): Assert that we're not running as root. |
| 92 | cros_build_lib.AssertInsideChroot() |
| 93 | |
| 94 | if options.host: |
| 95 | friendly_name = 'host' |
| 96 | sysroot = '/' |
| 97 | elif options.board: |
| 98 | friendly_name = options.board |
| 99 | sysroot = cros_build_lib.GetSysroot(board=options.board) |
| 100 | elif options.brick: |
| 101 | brick = brick_lib.Brick(options.brick) |
| 102 | friendly_name = brick.FriendlyName() |
| 103 | # TODO(wiley) This is a hack. It doesn't really make sense to calculate |
| 104 | # the sysroot from a brick alone, since bricks are installed |
| 105 | # into sysroots. Revisit this when blueprints are working. |
| 106 | sysroot = cros_build_lib.GetSysroot(friendly_name) |
| 107 | else: |
| 108 | cros_build_lib.Die('You must specify either --host, --board or --brick') |
| 109 | |
| 110 | helper = workon_helper.WorkonHelper(sysroot, friendly_name) |
| 111 | try: |
| 112 | if options.command == 'start': |
| 113 | helper.StartWorkingOnPackages(options.packages, use_all=options.all, |
| 114 | use_workon_only=options.workon_only) |
| 115 | elif options.command == 'stop': |
| 116 | helper.StopWorkingOnPackages(options.packages, use_all=options.all, |
| 117 | use_workon_only=options.workon_only) |
| 118 | elif options.command == 'info': |
| 119 | cros_build_lib.Die('%s is not implemented.', options.command) |
| 120 | elif options.command == 'list': |
| 121 | packages = helper.ListAtoms( |
| 122 | use_all=options.all, use_workon_only=options.workon_only) |
| 123 | if packages: |
| 124 | print('\n'.join(packages)) |
| 125 | elif options.command == 'iterate': |
| 126 | helper.RunCommandInPackages(options.packages, options.iterate_command, |
| 127 | use_all=options.all, |
| 128 | use_workon_only=options.workon_only) |
| 129 | except workon_helper.WorkonError as e: |
| 130 | cros_build_lib.Die(e.message) |
| 131 | |
| 132 | return 0 |