blob: 22e03fc03f9245353d9f9a47f0203bf51215855e [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.
11
12commands:
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
22from __future__ import print_function
23
24from chromite.lib import brick_lib
25from chromite.lib import commandline
26from chromite.lib import cros_build_lib
27from chromite.lib import terminal
28from chromite.lib import workon_helper
29
30
31def main(argv):
Christopher Wiley9385ea12015-05-08 13:18:45 -070032 shared = commandline.SharedParser()
33 shared.add_argument('--board', default=cros_build_lib.GetDefaultBoard(),
Christopher Wileyccd92f92015-04-14 14:14:19 -070034 help='The board to set package keywords for.')
Christopher Wiley9385ea12015-05-08 13:18:45 -070035 shared.add_argument('--brick', help='The brick to set package keywords for.')
36 shared.add_argument('--host', default=False, action='store_true',
Christopher Wileyccd92f92015-04-14 14:14:19 -070037 help='Uses the host instead of board')
Christopher Wiley9385ea12015-05-08 13:18:45 -070038 shared.add_argument('--remote', default='',
Christopher Wileyccd92f92015-04-14 14:14:19 -070039 help='For non-workon projects, the git remote to use.')
Christopher Wiley9385ea12015-05-08 13:18:45 -070040 shared.add_argument('--revision', default='',
Christopher Wileyccd92f92015-04-14 14:14:19 -070041 help='Use to override the manifest defined default '
42 'revision used for a project')
Christopher Wiley9385ea12015-05-08 13:18:45 -070043 shared.add_argument('--command', default='git status', dest='iterate_command',
Christopher Wileyccd92f92015-04-14 14:14:19 -070044 help='The command to be run by forall.')
Christopher Wiley9385ea12015-05-08 13:18:45 -070045 shared.add_argument('--workon_only', default=False, action='store_true',
Christopher Wileyccd92f92015-04-14 14:14:19 -070046 help='Apply to packages that have a workon ebuild only')
Christopher Wiley9385ea12015-05-08 13:18:45 -070047 shared.add_argument('--all', default=False, action='store_true',
Christopher Wileyccd92f92015-04-14 14:14:19 -070048 help='Apply to all possible packages for the '
49 'given command (overrides workon_only)')
Christopher Wiley9385ea12015-05-08 13:18:45 -070050
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 Wileyccd92f92015-04-14 14:14:19 -070056 help='The packages to run command against.')
Christopher Wiley9385ea12015-05-08 13:18:45 -070057
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 Wileyccd92f92015-04-14 14:14:19 -070078 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':
Christopher Wileya13655e2015-05-08 16:26:38 -0700119 triples = helper.GetPackageInfo(options.packages, use_all=options.all,
120 use_workon_only=options.workon_only)
121 for package, repos, paths in triples:
122 print(package, ','.join(repos), ','.join(paths))
Christopher Wileyccd92f92015-04-14 14:14:19 -0700123 elif options.command == 'list':
124 packages = helper.ListAtoms(
125 use_all=options.all, use_workon_only=options.workon_only)
126 if packages:
127 print('\n'.join(packages))
128 elif options.command == 'iterate':
129 helper.RunCommandInPackages(options.packages, options.iterate_command,
130 use_all=options.all,
131 use_workon_only=options.workon_only)
132 except workon_helper.WorkonError as e:
133 cros_build_lib.Die(e.message)
134
135 return 0