blob: bece65bb3e1f9fa01f6aae7b1b27d286694cc9a6 [file] [log] [blame]
Mike Frysingerf1ba7ad2022-09-12 05:42:57 -04001# Copyright 2015 The ChromiumOS Authors
Christopher Wileyccd92f92015-04-14 14:14:19 -07002# 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
Mike Frysinger06a51c82021-04-06 11:39:17 -040013from chromite.lib import build_target_lib
Christopher Wileyccd92f92015-04-14 14:14:19 -070014from chromite.lib import commandline
15from chromite.lib import cros_build_lib
16from chromite.lib import terminal
17from chromite.lib import workon_helper
18
19
Mike Frysingered5ab1d2017-10-23 14:10:28 -040020def GetParser():
Alex Klein1699fab2022-09-08 08:46:06 -060021 """Get a CLI parser."""
22 parser = commandline.ArgumentParser(description=__doc__)
23 parser.add_argument(
24 "--board",
25 default=cros_build_lib.GetDefaultBoard(),
26 help="The board to set package keywords for.",
27 )
28 parser.add_argument(
29 "--host",
30 default=False,
31 action="store_true",
32 help="Uses the host instead of board",
33 )
34 parser.add_argument(
35 "--command",
36 default="git status",
37 dest="iterate_command",
38 help="The command to be run by forall.",
39 )
40 parser.add_argument(
41 "--workon_only",
42 default=False,
43 action="store_true",
44 help="Apply to packages that have a workon ebuild only",
45 )
46 parser.add_argument(
47 "--all",
48 default=False,
49 action="store_true",
50 help="Apply to all possible packages for the "
51 "given command (overrides workon_only)",
52 )
Christopher Wiley9385ea12015-05-08 13:18:45 -070053
Alex Klein1699fab2022-09-08 08:46:06 -060054 commands = [
55 ("start", "Moves an ebuild to live (intended to support development)"),
56 ("stop", "Moves an ebuild to stable (use last known good)"),
57 ("info", "Print package name, repo name, and source directory."),
58 ("list", "List of live ebuilds (workon ebuilds if --all)"),
59 ("list-all", "List all of the live ebuilds for all setup boards"),
60 ("iterate", "For each ebuild, cd to the source dir and run a command"),
61 ]
62 command_parsers = parser.add_subparsers(dest="command", title="commands")
63 for command, description in commands:
64 sub_parser = command_parsers.add_parser(
65 command, description=description, help=description
66 )
67 sub_parser.add_argument(
68 "packages", nargs="*", help="The packages to run command against."
69 )
Christopher Wiley9385ea12015-05-08 13:18:45 -070070
Alex Klein1699fab2022-09-08 08:46:06 -060071 return parser
Mike Frysingered5ab1d2017-10-23 14:10:28 -040072
73
74def main(argv):
Alex Klein1699fab2022-09-08 08:46:06 -060075 parser = GetParser()
76 options = parser.parse_args(argv)
77 options.Freeze()
Christopher Wileyccd92f92015-04-14 14:14:19 -070078
Alex Klein1699fab2022-09-08 08:46:06 -060079 if options.command == "list-all":
80 board_to_packages = workon_helper.ListAllWorkedOnAtoms()
81 color = terminal.Color()
82 for board in sorted(board_to_packages):
83 print(color.Start(color.GREEN) + board + ":" + color.Stop())
84 for package in board_to_packages[board]:
85 print(" " + package)
86 print("")
87 return 0
88
89 # TODO(wiley): Assert that we're not running as root.
90 cros_build_lib.AssertInsideChroot()
91
92 if options.host:
93 friendly_name = "host"
94 sysroot = "/"
95 elif options.board:
96 friendly_name = options.board
97 sysroot = build_target_lib.get_default_sysroot_path(options.board)
98 else:
99 cros_build_lib.Die("You must specify either --host, --board")
100
101 helper = workon_helper.WorkonHelper(sysroot, friendly_name)
102 try:
103 if options.command == "start":
104 helper.StartWorkingOnPackages(
105 options.packages,
106 use_all=options.all,
107 use_workon_only=options.workon_only,
108 )
109 elif options.command == "stop":
110 helper.StopWorkingOnPackages(
111 options.packages,
112 use_all=options.all,
113 use_workon_only=options.workon_only,
114 )
115 elif options.command == "info":
116 triples = helper.GetPackageInfo(
117 options.packages,
118 use_all=options.all,
119 use_workon_only=options.workon_only,
120 )
121 for package, repos, paths in triples:
122 print(package, ",".join(repos), ",".join(paths))
123 elif options.command == "list":
124 packages = helper.ListAtoms(
125 use_all=options.all, use_workon_only=options.workon_only
126 )
127 if packages:
128 print("\n".join(packages))
129 elif options.command == "iterate":
130 helper.RunCommandInPackages(
131 options.packages,
132 options.iterate_command,
133 use_all=options.all,
134 use_workon_only=options.workon_only,
135 )
136 except workon_helper.WorkonError as e:
137 cros_build_lib.Die(e)
138
Christopher Wileyccd92f92015-04-14 14:14:19 -0700139 return 0