blob: 087c0a8286c3a05fc869df94b6a24a9cbc86a34b [file] [log] [blame]
Christopher Wileyccd92f92015-04-14 14:14:19 -07001# 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
7By default 'stable' ebuilds point at and build from source at the
8last known good commit. Moving an ebuild to 'live' (via cros_workon start)
9is intended to support development. The current source tip is fetched,
10source modified and built using the unstable 'live' (9999) ebuild.
Christopher Wileyccd92f92015-04-14 14:14:19 -070011"""
12
13from __future__ import print_function
14
15from chromite.lib import brick_lib
16from chromite.lib import commandline
17from chromite.lib import cros_build_lib
18from chromite.lib import terminal
19from chromite.lib import workon_helper
20
21
22def main(argv):
Christopher Wiley9385ea12015-05-08 13:18:45 -070023 shared = commandline.SharedParser()
24 shared.add_argument('--board', default=cros_build_lib.GetDefaultBoard(),
Christopher Wileyccd92f92015-04-14 14:14:19 -070025 help='The board to set package keywords for.')
Christopher Wiley9385ea12015-05-08 13:18:45 -070026 shared.add_argument('--brick', help='The brick to set package keywords for.')
27 shared.add_argument('--host', default=False, action='store_true',
Christopher Wileyccd92f92015-04-14 14:14:19 -070028 help='Uses the host instead of board')
Christopher Wiley9385ea12015-05-08 13:18:45 -070029 shared.add_argument('--remote', default='',
Christopher Wileyccd92f92015-04-14 14:14:19 -070030 help='For non-workon projects, the git remote to use.')
Christopher Wiley9385ea12015-05-08 13:18:45 -070031 shared.add_argument('--revision', default='',
Christopher Wileyccd92f92015-04-14 14:14:19 -070032 help='Use to override the manifest defined default '
33 'revision used for a project')
Christopher Wiley9385ea12015-05-08 13:18:45 -070034 shared.add_argument('--command', default='git status', dest='iterate_command',
Christopher Wileyccd92f92015-04-14 14:14:19 -070035 help='The command to be run by forall.')
Christopher Wiley9385ea12015-05-08 13:18:45 -070036 shared.add_argument('--workon_only', default=False, action='store_true',
Christopher Wileyccd92f92015-04-14 14:14:19 -070037 help='Apply to packages that have a workon ebuild only')
Christopher Wiley9385ea12015-05-08 13:18:45 -070038 shared.add_argument('--all', default=False, action='store_true',
Christopher Wileyccd92f92015-04-14 14:14:19 -070039 help='Apply to all possible packages for the '
40 'given command (overrides workon_only)')
Christopher Wiley9385ea12015-05-08 13:18:45 -070041
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 Wileyccd92f92015-04-14 14:14:19 -070047 help='The packages to run command against.')
Christopher Wiley9385ea12015-05-08 13:18:45 -070048
Christopher Wiley6475b2b2015-05-12 14:25:33 -070049 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 Wiley9385ea12015-05-08 13:18:45 -070061
Christopher Wileyccd92f92015-04-14 14:14:19 -070062 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 Wileya13655e2015-05-08 16:26:38 -0700103 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 Wileyccd92f92015-04-14 14:14:19 -0700107 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