blob: 8fb41f6e53676a7c51dfc1816175ae23ec3f5d09 [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 Harring7fcc02e2012-08-05 04:10:57 -07007import os
Mike Frysinger807d8282022-04-28 22:45:17 -04008import platform
Brian Harring7fcc02e2012-08-05 04:10:57 -07009import 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
Brian Harring7fcc02e2012-08-05 04:10:57 -070015
16
Gwendal Grignou89afc082016-09-29 21:03:20 -070017class LocalManifest(object):
Brian Harring7fcc02e2012-08-05 04:10:57 -070018 """Class which provides an abstraction for manipulating the local manifest."""
19
Brian Harringb0043ab2012-08-05 04:09:56 -070020 @classmethod
21 def FromPath(cls, path, empty_if_missing=False):
22 if os.path.isfile(path):
Mike Frysinger462dbd62019-10-19 20:26:46 -040023 return cls(osutils.ReadFile(path))
Brian Harringb0043ab2012-08-05 04:09:56 -070024 elif empty_if_missing:
25 cros_build_lib.Die('Manifest file, %r, not found' % path)
26 return cls()
27
Brian Harring7fcc02e2012-08-05 04:10:57 -070028 def __init__(self, text=None):
29 self._text = text or '<manifest>\n</manifest>'
Brian Harringb0043ab2012-08-05 04:09:56 -070030 self.nodes = ElementTree.fromstring(self._text)
Brian Harring7fcc02e2012-08-05 04:10:57 -070031
Rhyland Kleinb1d1f382012-08-23 11:53:45 -040032 def AddNonWorkonProject(self, name, path, remote=None, revision=None):
Brian Harringb0043ab2012-08-05 04:09:56 -070033 """Add a new nonworkon project element to the manifest tree."""
34 element = ElementTree.Element('project', name=name, path=path,
Rhyland Kleindd8ebbb2012-09-06 11:51:30 -040035 remote=remote)
Brian Harringb0043ab2012-08-05 04:09:56 -070036 element.attrib['workon'] = 'False'
Rhyland Kleindd8ebbb2012-09-06 11:51:30 -040037 if revision is not None:
38 element.attrib['revision'] = revision
Brian Harringb0043ab2012-08-05 04:09:56 -070039 self.nodes.append(element)
40 return element
Brian Harring7fcc02e2012-08-05 04:10:57 -070041
Brian Harringb0043ab2012-08-05 04:09:56 -070042 def GetProject(self, name, path=None):
Brian Harring7fcc02e2012-08-05 04:10:57 -070043 """Accessor method for getting a project node from the manifest tree.
44
45 Returns:
46 project element node from ElementTree, otherwise, None
47 """
Brian Harringb0043ab2012-08-05 04:09:56 -070048 if path is None:
49 # Use a unique value that can't ever match.
50 path = object()
51 for project in self.nodes.findall('project'):
52 if project.attrib['name'] == name or project.attrib['path'] == path:
Brian Harring7fcc02e2012-08-05 04:10:57 -070053 return project
54 return None
55
56 def ToString(self):
Brian Harringb0043ab2012-08-05 04:09:56 -070057 # Reset the tail for each node, then just do a hacky replace.
58 project = None
59 for project in self.nodes.findall('project'):
60 project.tail = '\n '
61 if project is not None:
62 # Tweak the last project to not have the trailing space.
63 project.tail = '\n'
64 # Fix manifest tag text and tail.
65 self.nodes.text = '\n '
66 self.nodes.tail = '\n'
Mike Frysingered41d722022-03-28 17:52:11 -040067 return ElementTree.tostring(self.nodes, encoding='unicode')
Brian Harringb0043ab2012-08-05 04:09:56 -070068
69 def GetProjects(self):
70 return list(self.nodes.findall('project'))
71
72
Gwendal Grignou89afc082016-09-29 21:03:20 -070073def _AddProjectsToManifestGroups(options, new_group):
Brian Harringb0043ab2012-08-05 04:09:56 -070074 """Enable the given manifest groups for the configured repository."""
75
Gwendal Grignou89afc082016-09-29 21:03:20 -070076 groups_to_enable = ['name:%s' % x for x in new_group]
Brian Harringb0043ab2012-08-05 04:09:56 -070077
78 git_config = options.git_config
79
David James67d73252013-09-19 17:33:12 -070080 cmd = ['config', '-f', git_config, '--get', 'manifest.groups']
Mike Frysingerf5a3b2d2019-12-12 14:36:17 -050081 enabled_groups = git.RunGit('.', cmd, check=False).output.split(',')
Brian Harringb0043ab2012-08-05 04:09:56 -070082
83 # Note that ordering actually matters, thus why the following code
84 # is written this way.
85 # Per repo behaviour, enforce an appropriate platform group if
86 # we're converting from a default manifest group to a limited one.
87 # Finally, note we reprocess the existing groups; this is to allow
88 # us to cleanup any user screwups, or our own screwups.
89 requested_groups = (
90 ['minilayout', 'platform-%s' % (platform.system().lower(),)] +
91 enabled_groups + list(groups_to_enable))
92
93 processed_groups = set()
94 finalized_groups = []
95
96 for group in requested_groups:
97 if group not in processed_groups:
98 finalized_groups.append(group)
99 processed_groups.add(group)
100
David James67d73252013-09-19 17:33:12 -0700101 cmd = ['config', '-f', git_config, 'manifest.groups',
102 ','.join(finalized_groups)]
103 git.RunGit('.', cmd)
Brian Harringb0043ab2012-08-05 04:09:56 -0700104
105
Gwendal Grignouf9d6d362016-09-30 09:29:20 -0700106def _AssertNotMiniLayout():
107 cros_build_lib.Die(
Mike Frysinger80de5012019-08-01 14:10:53 -0400108 'Your repository checkout is using the old minilayout.xml workflow; '
109 'Autoupdate is no longer supported, reinstall your tree.')
Brian Harring7fcc02e2012-08-05 04:10:57 -0700110
111
Mike Frysinger4ca60152016-09-01 00:13:36 -0400112def GetParser():
113 """Return a command line parser."""
114 parser = commandline.ArgumentParser(description=__doc__)
Mike Frysinger1b8565b2016-09-13 16:03:49 -0400115
Mike Frysinger342be272019-10-19 20:33:37 -0400116 # Subparsers are required by default under Python 2. Python 3 changed to
117 # not required, but didn't include a required option until 3.7. Setting
118 # the required member works in all versions (and setting dest name).
Mike Frysinger1b8565b2016-09-13 16:03:49 -0400119 subparsers = parser.add_subparsers(dest='command')
Mike Frysinger342be272019-10-19 20:33:37 -0400120 subparsers.required = True
Mike Frysinger1b8565b2016-09-13 16:03:49 -0400121
122 subparser = subparsers.add_parser(
123 'add',
124 help='Add projects to the manifest.')
125 subparser.add_argument('-w', '--workon', action='store_true',
126 default=False, help='Is this a workon package?')
127 subparser.add_argument('-r', '--remote',
128 help='Remote project name (for non-workon packages).')
Alex Klein3c345ec2020-03-30 16:08:40 -0600129 subparser.add_argument('--revision',
Mike Frysinger1b8565b2016-09-13 16:03:49 -0400130 help='Use to override the manifest defined default '
131 'revision used for a given project.')
132 subparser.add_argument('project', help='Name of project in the manifest.')
133 subparser.add_argument('path', nargs='?', help='Local path to the project.')
134
Mike Frysinger4ca60152016-09-01 00:13:36 -0400135 return parser
136
137
Brian Harring7fcc02e2012-08-05 04:10:57 -0700138def main(argv):
Mike Frysinger4ca60152016-09-01 00:13:36 -0400139 parser = GetParser()
140 options = parser.parse_args(argv)
Ryan Cui0b1b94b2012-12-21 12:09:57 -0800141 repo_dir = git.FindRepoDir(os.getcwd())
Brian Harringb0043ab2012-08-05 04:09:56 -0700142 if not repo_dir:
Mike Frysinger80de5012019-08-01 14:10:53 -0400143 parser.error('This script must be invoked from within a repository '
144 'checkout.')
Brian Harring7fcc02e2012-08-05 04:10:57 -0700145
Brian Harringb0043ab2012-08-05 04:09:56 -0700146 options.git_config = os.path.join(repo_dir, 'manifests.git', 'config')
Brian Harringb0043ab2012-08-05 04:09:56 -0700147 options.local_manifest_path = os.path.join(repo_dir, 'local_manifest.xml')
Brian Harringb0043ab2012-08-05 04:09:56 -0700148
Gwendal Grignou89afc082016-09-29 21:03:20 -0700149 manifest_sym_path = os.path.join(repo_dir, 'manifest.xml')
Brian Norris5ae623f2020-10-28 15:42:13 -0700150 if os.path.basename(os.path.realpath(manifest_sym_path)) == 'minilayout.xml':
Gwendal Grignouf9d6d362016-09-30 09:29:20 -0700151 _AssertNotMiniLayout()
Brian Harringb0043ab2012-08-05 04:09:56 -0700152
Mike Frysinger1b8565b2016-09-13 16:03:49 -0400153 # For now, we only support the add command.
154 assert options.command == 'add'
155 if options.workon:
156 if options.path is not None:
157 parser.error('Adding workon projects do not set project.')
Brian Harring7fcc02e2012-08-05 04:10:57 -0700158 else:
Brian Harringb0043ab2012-08-05 04:09:56 -0700159 if options.remote is None:
160 parser.error('Adding non-workon projects requires a remote.')
Mike Frysinger1b8565b2016-09-13 16:03:49 -0400161 if options.path is None:
162 parser.error('Adding non-workon projects requires a path.')
163 name = options.project
164 path = options.path
Rhyland Kleinb1d1f382012-08-23 11:53:45 -0400165 revision = options.revision
166 if revision is not None:
David James97d95872012-11-16 15:09:56 -0800167 if (not git.IsRefsTags(revision) and
168 not git.IsSHA1(revision)):
169 revision = git.StripRefsHeads(revision, False)
Rhyland Kleinb1d1f382012-08-23 11:53:45 -0400170
Gwendal Grignou89afc082016-09-29 21:03:20 -0700171 main_manifest = git.ManifestCheckout(os.getcwd())
172 main_element = main_manifest.FindCheckouts(name)
173 if path is not None:
174 main_element_from_path = main_manifest.FindCheckoutFromPath(
175 path, strict=False)
176 if main_element_from_path is not None:
177 main_element.append(main_element_from_path)
Brian Harring7fcc02e2012-08-05 04:10:57 -0700178
Gwendal Grignou89afc082016-09-29 21:03:20 -0700179 local_manifest = LocalManifest.FromPath(options.local_manifest_path)
Brian Harring7fcc02e2012-08-05 04:10:57 -0700180
Brian Harringb0043ab2012-08-05 04:09:56 -0700181 if options.workon:
Gwendal Grignou89afc082016-09-29 21:03:20 -0700182 if not main_element:
Brian Harringb0043ab2012-08-05 04:09:56 -0700183 parser.error('No project named %r in the default manifest.' % name)
Gwendal Grignou89afc082016-09-29 21:03:20 -0700184 _AddProjectsToManifestGroups(
185 options, [checkout['name'] for checkout in main_element])
Brian Harringb0043ab2012-08-05 04:09:56 -0700186
Gwendal Grignou89afc082016-09-29 21:03:20 -0700187 elif main_element:
Rhyland Kleine5faad52012-10-31 11:58:19 -0400188 if options.remote is not None:
189 # Likely this project wasn't meant to be remote, so workon main element
Mike Frysinger80de5012019-08-01 14:10:53 -0400190 print('Project already exists in manifest. Using that as workon project.')
Gwendal Grignou89afc082016-09-29 21:03:20 -0700191 _AddProjectsToManifestGroups(
192 options, [checkout['name'] for checkout in main_element])
Rhyland Kleine5faad52012-10-31 11:58:19 -0400193 else:
194 # Conflict will occur; complain.
Mike Frysinger80de5012019-08-01 14:10:53 -0400195 parser.error('Requested project name=%r path=%r will conflict with '
196 'your current manifest %s' % (
Gwendal Grignou89afc082016-09-29 21:03:20 -0700197 name, path, main_manifest.manifest_path))
Brian Harringb0043ab2012-08-05 04:09:56 -0700198
199 elif local_manifest.GetProject(name, path=path) is not None:
Mike Frysinger80de5012019-08-01 14:10:53 -0400200 parser.error('Requested project name=%r path=%r conflicts with '
201 'your local_manifest.xml' % (name, path))
Brian Harringb0043ab2012-08-05 04:09:56 -0700202
203 else:
Rhyland Kleinb1d1f382012-08-23 11:53:45 -0400204 element = local_manifest.AddNonWorkonProject(name=name, path=path,
205 remote=options.remote,
206 revision=revision)
Gwendal Grignou89afc082016-09-29 21:03:20 -0700207 _AddProjectsToManifestGroups(options, [element.attrib['name']])
Brian Harringb0043ab2012-08-05 04:09:56 -0700208
209 with open(options.local_manifest_path, 'w') as f:
210 f.write(local_manifest.ToString())
211 return 0