Mike Frysinger | e58c0e2 | 2017-10-04 15:43:30 -0400 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
Christopher Wiley | ccd92f9 | 2015-04-14 14:14:19 -0700 | [diff] [blame] | 2 | # Copyright 2015 The Chromium OS Authors. All rights reserved. |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
| 6 | """This script moves ebuilds between 'stable' and 'live' states. |
| 7 | |
| 8 | By default 'stable' ebuilds point at and build from source at the |
| 9 | last known good commit. Moving an ebuild to 'live' (via cros_workon start) |
| 10 | is intended to support development. The current source tip is fetched, |
| 11 | source modified and built using the unstable 'live' (9999) ebuild. |
Christopher Wiley | ccd92f9 | 2015-04-14 14:14:19 -0700 | [diff] [blame] | 12 | """ |
| 13 | |
| 14 | from __future__ import print_function |
| 15 | |
Mike Frysinger | a942aee | 2020-03-20 03:53:37 -0400 | [diff] [blame] | 16 | import sys |
| 17 | |
Christopher Wiley | ccd92f9 | 2015-04-14 14:14:19 -0700 | [diff] [blame] | 18 | from chromite.lib import commandline |
| 19 | from chromite.lib import cros_build_lib |
| 20 | from chromite.lib import terminal |
| 21 | from chromite.lib import workon_helper |
| 22 | |
| 23 | |
Mike Frysinger | a942aee | 2020-03-20 03:53:37 -0400 | [diff] [blame] | 24 | assert sys.version_info >= (3, 6), 'This module requires Python 3.6+' |
| 25 | |
| 26 | |
Mike Frysinger | ed5ab1d | 2017-10-23 14:10:28 -0400 | [diff] [blame] | 27 | def GetParser(): |
| 28 | """Get a CLI parser.""" |
Mike Frysinger | 1341bdd | 2017-08-04 11:04:38 -0400 | [diff] [blame] | 29 | parser = commandline.ArgumentParser(description=__doc__) |
| 30 | parser.add_argument('--board', default=cros_build_lib.GetDefaultBoard(), |
Christopher Wiley | ccd92f9 | 2015-04-14 14:14:19 -0700 | [diff] [blame] | 31 | help='The board to set package keywords for.') |
Mike Frysinger | 1341bdd | 2017-08-04 11:04:38 -0400 | [diff] [blame] | 32 | parser.add_argument('--host', default=False, action='store_true', |
Christopher Wiley | ccd92f9 | 2015-04-14 14:14:19 -0700 | [diff] [blame] | 33 | help='Uses the host instead of board') |
Mike Frysinger | 1341bdd | 2017-08-04 11:04:38 -0400 | [diff] [blame] | 34 | parser.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.') |
Mike Frysinger | 1341bdd | 2017-08-04 11:04:38 -0400 | [diff] [blame] | 36 | parser.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') |
Mike Frysinger | 1341bdd | 2017-08-04 11:04:38 -0400 | [diff] [blame] | 38 | parser.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 | |
Christopher Wiley | 6475b2b | 2015-05-12 14:25:33 -0700 | [diff] [blame] | 42 | commands = [ |
| 43 | ('start', 'Moves an ebuild to live (intended to support development)'), |
| 44 | ('stop', 'Moves an ebuild to stable (use last known good)'), |
| 45 | ('info', 'Print package name, repo name, and source directory.'), |
| 46 | ('list', 'List of live ebuilds (workon ebuilds if --all)'), |
| 47 | ('list-all', 'List all of the live ebuilds for all setup boards'), |
| 48 | ('iterate', 'For each ebuild, cd to the source dir and run a command'), |
| 49 | ] |
| 50 | command_parsers = parser.add_subparsers(dest='command', title='commands') |
| 51 | for command, description in commands: |
Mike Frysinger | 1341bdd | 2017-08-04 11:04:38 -0400 | [diff] [blame] | 52 | sub_parser = command_parsers.add_parser(command, description=description, |
| 53 | help=description) |
| 54 | sub_parser.add_argument('packages', nargs='*', |
| 55 | help='The packages to run command against.') |
Christopher Wiley | 9385ea1 | 2015-05-08 13:18:45 -0700 | [diff] [blame] | 56 | |
Mike Frysinger | ed5ab1d | 2017-10-23 14:10:28 -0400 | [diff] [blame] | 57 | return parser |
| 58 | |
| 59 | |
| 60 | def main(argv): |
| 61 | parser = GetParser() |
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 |
Jack Rosenthal | b984e10 | 2021-04-07 21:18:29 +0000 | [diff] [blame] | 83 | sysroot = cros_build_lib.GetSysroot(board=options.board) |
Christopher Wiley | ccd92f9 | 2015-04-14 14:14:19 -0700 | [diff] [blame] | 84 | else: |
Don Garrett | c0c7400 | 2015-10-09 12:58:19 -0700 | [diff] [blame] | 85 | cros_build_lib.Die('You must specify either --host, --board') |
Christopher Wiley | ccd92f9 | 2015-04-14 14:14:19 -0700 | [diff] [blame] | 86 | |
| 87 | helper = workon_helper.WorkonHelper(sysroot, friendly_name) |
| 88 | try: |
| 89 | if options.command == 'start': |
| 90 | helper.StartWorkingOnPackages(options.packages, use_all=options.all, |
| 91 | use_workon_only=options.workon_only) |
| 92 | elif options.command == 'stop': |
| 93 | helper.StopWorkingOnPackages(options.packages, use_all=options.all, |
| 94 | use_workon_only=options.workon_only) |
| 95 | elif options.command == 'info': |
Christopher Wiley | a13655e | 2015-05-08 16:26:38 -0700 | [diff] [blame] | 96 | triples = helper.GetPackageInfo(options.packages, use_all=options.all, |
| 97 | use_workon_only=options.workon_only) |
| 98 | for package, repos, paths in triples: |
| 99 | print(package, ','.join(repos), ','.join(paths)) |
Christopher Wiley | ccd92f9 | 2015-04-14 14:14:19 -0700 | [diff] [blame] | 100 | elif options.command == 'list': |
| 101 | packages = helper.ListAtoms( |
| 102 | use_all=options.all, use_workon_only=options.workon_only) |
| 103 | if packages: |
| 104 | print('\n'.join(packages)) |
| 105 | elif options.command == 'iterate': |
| 106 | helper.RunCommandInPackages(options.packages, options.iterate_command, |
| 107 | use_all=options.all, |
| 108 | use_workon_only=options.workon_only) |
| 109 | except workon_helper.WorkonError as e: |
Mike Frysinger | 6b5c3cd | 2019-08-27 16:51:00 -0400 | [diff] [blame] | 110 | cros_build_lib.Die(e) |
Christopher Wiley | ccd92f9 | 2015-04-14 14:14:19 -0700 | [diff] [blame] | 111 | |
| 112 | return 0 |