blob: 900e0f4672baa540a25d943e0e3c20af22f9256a [file] [log] [blame]
Brian Harring7fcc02e2012-08-05 04:10:57 -07001# Copyright (c) 2010 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
Mike Frysinger4ca60152016-09-01 00:13:36 -04005"""Manage projects in the local manifest."""
Brian Harring7fcc02e2012-08-05 04:10:57 -07006
Brian Harringb0043ab2012-08-05 04:09:56 -07007import platform
Brian Harring7fcc02e2012-08-05 04:10:57 -07008import os
9import xml.etree.ElementTree as ElementTree
Mike Frysinger750c5f52014-09-16 16:16:57 -040010
Mike Frysinger4ca60152016-09-01 00:13:36 -040011from chromite.lib import commandline
Brian Harringb0043ab2012-08-05 04:09:56 -070012from chromite.lib import cros_build_lib
David James97d95872012-11-16 15:09:56 -080013from chromite.lib import git
Mike Frysinger462dbd62019-10-19 20:26:46 -040014from chromite.lib import osutils
15from chromite.lib import repo_manifest
Brian Harring7fcc02e2012-08-05 04:10:57 -070016
17
Gwendal Grignou89afc082016-09-29 21:03:20 -070018class LocalManifest(object):
Brian Harring7fcc02e2012-08-05 04:10:57 -070019 """Class which provides an abstraction for manipulating the local manifest."""
20
Brian Harringb0043ab2012-08-05 04:09:56 -070021 @classmethod
22 def FromPath(cls, path, empty_if_missing=False):
23 if os.path.isfile(path):
Mike Frysinger462dbd62019-10-19 20:26:46 -040024 return cls(osutils.ReadFile(path))
Brian Harringb0043ab2012-08-05 04:09:56 -070025 elif empty_if_missing:
26 cros_build_lib.Die('Manifest file, %r, not found' % path)
27 return cls()
28
Brian Harring7fcc02e2012-08-05 04:10:57 -070029 def __init__(self, text=None):
30 self._text = text or '<manifest>\n</manifest>'
Brian Harringb0043ab2012-08-05 04:09:56 -070031 self.nodes = ElementTree.fromstring(self._text)
Brian Harring7fcc02e2012-08-05 04:10:57 -070032
Rhyland Kleinb1d1f382012-08-23 11:53:45 -040033 def AddNonWorkonProject(self, name, path, remote=None, revision=None):
Brian Harringb0043ab2012-08-05 04:09:56 -070034 """Add a new nonworkon project element to the manifest tree."""
35 element = ElementTree.Element('project', name=name, path=path,
Rhyland Kleindd8ebbb2012-09-06 11:51:30 -040036 remote=remote)
Brian Harringb0043ab2012-08-05 04:09:56 -070037 element.attrib['workon'] = 'False'
Rhyland Kleindd8ebbb2012-09-06 11:51:30 -040038 if revision is not None:
39 element.attrib['revision'] = revision
Brian Harringb0043ab2012-08-05 04:09:56 -070040 self.nodes.append(element)
41 return element
Brian Harring7fcc02e2012-08-05 04:10:57 -070042
Brian Harringb0043ab2012-08-05 04:09:56 -070043 def GetProject(self, name, path=None):
Brian Harring7fcc02e2012-08-05 04:10:57 -070044 """Accessor method for getting a project node from the manifest tree.
45
46 Returns:
47 project element node from ElementTree, otherwise, None
48 """
Brian Harringb0043ab2012-08-05 04:09:56 -070049 if path is None:
50 # Use a unique value that can't ever match.
51 path = object()
52 for project in self.nodes.findall('project'):
53 if project.attrib['name'] == name or project.attrib['path'] == path:
Brian Harring7fcc02e2012-08-05 04:10:57 -070054 return project
55 return None
56
57 def ToString(self):
Brian Harringb0043ab2012-08-05 04:09:56 -070058 # Reset the tail for each node, then just do a hacky replace.
59 project = None
60 for project in self.nodes.findall('project'):
61 project.tail = '\n '
62 if project is not None:
63 # Tweak the last project to not have the trailing space.
64 project.tail = '\n'
65 # Fix manifest tag text and tail.
66 self.nodes.text = '\n '
67 self.nodes.tail = '\n'
Mike Frysinger462dbd62019-10-19 20:26:46 -040068 return ElementTree.tostring(
69 self.nodes, encoding=repo_manifest.TOSTRING_ENCODING)
Brian Harringb0043ab2012-08-05 04:09:56 -070070
71 def GetProjects(self):
72 return list(self.nodes.findall('project'))
73
74
Gwendal Grignou89afc082016-09-29 21:03:20 -070075def _AddProjectsToManifestGroups(options, new_group):
Brian Harringb0043ab2012-08-05 04:09:56 -070076 """Enable the given manifest groups for the configured repository."""
77
Gwendal Grignou89afc082016-09-29 21:03:20 -070078 groups_to_enable = ['name:%s' % x for x in new_group]
Brian Harringb0043ab2012-08-05 04:09:56 -070079
80 git_config = options.git_config
81
David James67d73252013-09-19 17:33:12 -070082 cmd = ['config', '-f', git_config, '--get', 'manifest.groups']
Mike Frysingerf5a3b2d2019-12-12 14:36:17 -050083 enabled_groups = git.RunGit('.', cmd, check=False).output.split(',')
Brian Harringb0043ab2012-08-05 04:09:56 -070084
85 # Note that ordering actually matters, thus why the following code
86 # is written this way.
87 # Per repo behaviour, enforce an appropriate platform group if
88 # we're converting from a default manifest group to a limited one.
89 # Finally, note we reprocess the existing groups; this is to allow
90 # us to cleanup any user screwups, or our own screwups.
91 requested_groups = (
92 ['minilayout', 'platform-%s' % (platform.system().lower(),)] +
93 enabled_groups + list(groups_to_enable))
94
95 processed_groups = set()
96 finalized_groups = []
97
98 for group in requested_groups:
99 if group not in processed_groups:
100 finalized_groups.append(group)
101 processed_groups.add(group)
102
David James67d73252013-09-19 17:33:12 -0700103 cmd = ['config', '-f', git_config, 'manifest.groups',
104 ','.join(finalized_groups)]
105 git.RunGit('.', cmd)
Brian Harringb0043ab2012-08-05 04:09:56 -0700106
107
Gwendal Grignouf9d6d362016-09-30 09:29:20 -0700108def _AssertNotMiniLayout():
109 cros_build_lib.Die(
Mike Frysinger80de5012019-08-01 14:10:53 -0400110 'Your repository checkout is using the old minilayout.xml workflow; '
111 'Autoupdate is no longer supported, reinstall your tree.')
Brian Harring7fcc02e2012-08-05 04:10:57 -0700112
113
Mike Frysinger4ca60152016-09-01 00:13:36 -0400114def GetParser():
115 """Return a command line parser."""
116 parser = commandline.ArgumentParser(description=__doc__)
Mike Frysinger1b8565b2016-09-13 16:03:49 -0400117
Mike Frysinger342be272019-10-19 20:33:37 -0400118 # Subparsers are required by default under Python 2. Python 3 changed to
119 # not required, but didn't include a required option until 3.7. Setting
120 # the required member works in all versions (and setting dest name).
Mike Frysinger1b8565b2016-09-13 16:03:49 -0400121 subparsers = parser.add_subparsers(dest='command')
Mike Frysinger342be272019-10-19 20:33:37 -0400122 subparsers.required = True
Mike Frysinger1b8565b2016-09-13 16:03:49 -0400123
124 subparser = subparsers.add_parser(
125 'add',
126 help='Add projects to the manifest.')
127 subparser.add_argument('-w', '--workon', action='store_true',
128 default=False, help='Is this a workon package?')
129 subparser.add_argument('-r', '--remote',
130 help='Remote project name (for non-workon packages).')
Alex Klein3c345ec2020-03-30 16:08:40 -0600131 subparser.add_argument('--revision',
Mike Frysinger1b8565b2016-09-13 16:03:49 -0400132 help='Use to override the manifest defined default '
133 'revision used for a given project.')
134 subparser.add_argument('project', help='Name of project in the manifest.')
135 subparser.add_argument('path', nargs='?', help='Local path to the project.')
136
Mike Frysinger4ca60152016-09-01 00:13:36 -0400137 return parser
138
139
Brian Harring7fcc02e2012-08-05 04:10:57 -0700140def main(argv):
Mike Frysinger4ca60152016-09-01 00:13:36 -0400141 parser = GetParser()
142 options = parser.parse_args(argv)
Ryan Cui0b1b94b2012-12-21 12:09:57 -0800143 repo_dir = git.FindRepoDir(os.getcwd())
Brian Harringb0043ab2012-08-05 04:09:56 -0700144 if not repo_dir:
Mike Frysinger80de5012019-08-01 14:10:53 -0400145 parser.error('This script must be invoked from within a repository '
146 'checkout.')
Brian Harring7fcc02e2012-08-05 04:10:57 -0700147
Brian Harringb0043ab2012-08-05 04:09:56 -0700148 options.git_config = os.path.join(repo_dir, 'manifests.git', 'config')
Brian Harringb0043ab2012-08-05 04:09:56 -0700149 options.local_manifest_path = os.path.join(repo_dir, 'local_manifest.xml')
Brian Harringb0043ab2012-08-05 04:09:56 -0700150
Gwendal Grignou89afc082016-09-29 21:03:20 -0700151 manifest_sym_path = os.path.join(repo_dir, 'manifest.xml')
Brian Norris5ae623f2020-10-28 15:42:13 -0700152 if os.path.basename(os.path.realpath(manifest_sym_path)) == 'minilayout.xml':
Gwendal Grignouf9d6d362016-09-30 09:29:20 -0700153 _AssertNotMiniLayout()
Brian Harringb0043ab2012-08-05 04:09:56 -0700154
Mike Frysinger1b8565b2016-09-13 16:03:49 -0400155 # For now, we only support the add command.
156 assert options.command == 'add'
157 if options.workon:
158 if options.path is not None:
159 parser.error('Adding workon projects do not set project.')
Brian Harring7fcc02e2012-08-05 04:10:57 -0700160 else:
Brian Harringb0043ab2012-08-05 04:09:56 -0700161 if options.remote is None:
162 parser.error('Adding non-workon projects requires a remote.')
Mike Frysinger1b8565b2016-09-13 16:03:49 -0400163 if options.path is None:
164 parser.error('Adding non-workon projects requires a path.')
165 name = options.project
166 path = options.path
Rhyland Kleinb1d1f382012-08-23 11:53:45 -0400167 revision = options.revision
168 if revision is not None:
David James97d95872012-11-16 15:09:56 -0800169 if (not git.IsRefsTags(revision) and
170 not git.IsSHA1(revision)):
171 revision = git.StripRefsHeads(revision, False)
Rhyland Kleinb1d1f382012-08-23 11:53:45 -0400172
Gwendal Grignou89afc082016-09-29 21:03:20 -0700173 main_manifest = git.ManifestCheckout(os.getcwd())
174 main_element = main_manifest.FindCheckouts(name)
175 if path is not None:
176 main_element_from_path = main_manifest.FindCheckoutFromPath(
177 path, strict=False)
178 if main_element_from_path is not None:
179 main_element.append(main_element_from_path)
Brian Harring7fcc02e2012-08-05 04:10:57 -0700180
Gwendal Grignou89afc082016-09-29 21:03:20 -0700181 local_manifest = LocalManifest.FromPath(options.local_manifest_path)
Brian Harring7fcc02e2012-08-05 04:10:57 -0700182
Brian Harringb0043ab2012-08-05 04:09:56 -0700183 if options.workon:
Gwendal Grignou89afc082016-09-29 21:03:20 -0700184 if not main_element:
Brian Harringb0043ab2012-08-05 04:09:56 -0700185 parser.error('No project named %r in the default manifest.' % name)
Gwendal Grignou89afc082016-09-29 21:03:20 -0700186 _AddProjectsToManifestGroups(
187 options, [checkout['name'] for checkout in main_element])
Brian Harringb0043ab2012-08-05 04:09:56 -0700188
Gwendal Grignou89afc082016-09-29 21:03:20 -0700189 elif main_element:
Rhyland Kleine5faad52012-10-31 11:58:19 -0400190 if options.remote is not None:
191 # Likely this project wasn't meant to be remote, so workon main element
Mike Frysinger80de5012019-08-01 14:10:53 -0400192 print('Project already exists in manifest. Using that as workon project.')
Gwendal Grignou89afc082016-09-29 21:03:20 -0700193 _AddProjectsToManifestGroups(
194 options, [checkout['name'] for checkout in main_element])
Rhyland Kleine5faad52012-10-31 11:58:19 -0400195 else:
196 # Conflict will occur; complain.
Mike Frysinger80de5012019-08-01 14:10:53 -0400197 parser.error('Requested project name=%r path=%r will conflict with '
198 'your current manifest %s' % (
Gwendal Grignou89afc082016-09-29 21:03:20 -0700199 name, path, main_manifest.manifest_path))
Brian Harringb0043ab2012-08-05 04:09:56 -0700200
201 elif local_manifest.GetProject(name, path=path) is not None:
Mike Frysinger80de5012019-08-01 14:10:53 -0400202 parser.error('Requested project name=%r path=%r conflicts with '
203 'your local_manifest.xml' % (name, path))
Brian Harringb0043ab2012-08-05 04:09:56 -0700204
205 else:
Rhyland Kleinb1d1f382012-08-23 11:53:45 -0400206 element = local_manifest.AddNonWorkonProject(name=name, path=path,
207 remote=options.remote,
208 revision=revision)
Gwendal Grignou89afc082016-09-29 21:03:20 -0700209 _AddProjectsToManifestGroups(options, [element.attrib['name']])
Brian Harringb0043ab2012-08-05 04:09:56 -0700210
211 with open(options.local_manifest_path, 'w') as f:
212 f.write(local_manifest.ToString())
213 return 0