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