blob: cdd52192b3f2f17d2060bf394436b13170dad8cf [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
5"""This module allows adding and deleting of projects to the local manifest."""
6
Mike Frysinger1d4752b2014-11-08 04:00:18 -05007# pylint: disable=bad-continuation
8
Mike Frysinger383367e2014-09-16 15:06:17 -04009from __future__ import print_function
10
Brian Harringb0043ab2012-08-05 04:09:56 -070011import logging
12import platform
Brian Harring7fcc02e2012-08-05 04:10:57 -070013import optparse
14import os
Brian Harringb0043ab2012-08-05 04:09:56 -070015import sys
Brian Harring7fcc02e2012-08-05 04:10:57 -070016import xml.etree.ElementTree as ElementTree
Mike Frysinger750c5f52014-09-16 16:16:57 -040017
Brian Harringb0043ab2012-08-05 04:09:56 -070018from chromite.lib import cros_build_lib
David James97d95872012-11-16 15:09:56 -080019from chromite.lib import git
Brian Harring7fcc02e2012-08-05 04:10:57 -070020
21
Brian Harringb0043ab2012-08-05 04:09:56 -070022class Manifest(object):
Brian Harring7fcc02e2012-08-05 04:10:57 -070023 """Class which provides an abstraction for manipulating the local manifest."""
24
Brian Harringb0043ab2012-08-05 04:09:56 -070025 @classmethod
26 def FromPath(cls, path, empty_if_missing=False):
27 if os.path.isfile(path):
28 with open(path) as f:
29 return cls(f.read())
30 elif empty_if_missing:
31 cros_build_lib.Die('Manifest file, %r, not found' % path)
32 return cls()
33
Brian Harring7fcc02e2012-08-05 04:10:57 -070034 def __init__(self, text=None):
35 self._text = text or '<manifest>\n</manifest>'
Brian Harringb0043ab2012-08-05 04:09:56 -070036 self.nodes = ElementTree.fromstring(self._text)
Brian Harring7fcc02e2012-08-05 04:10:57 -070037
Rhyland Kleinb1d1f382012-08-23 11:53:45 -040038 def AddNonWorkonProject(self, name, path, remote=None, revision=None):
Brian Harringb0043ab2012-08-05 04:09:56 -070039 """Add a new nonworkon project element to the manifest tree."""
40 element = ElementTree.Element('project', name=name, path=path,
Rhyland Kleindd8ebbb2012-09-06 11:51:30 -040041 remote=remote)
Brian Harringb0043ab2012-08-05 04:09:56 -070042 element.attrib['workon'] = 'False'
Rhyland Kleindd8ebbb2012-09-06 11:51:30 -040043 if revision is not None:
44 element.attrib['revision'] = revision
Brian Harringb0043ab2012-08-05 04:09:56 -070045 self.nodes.append(element)
46 return element
Brian Harring7fcc02e2012-08-05 04:10:57 -070047
Brian Harringb0043ab2012-08-05 04:09:56 -070048 def GetProject(self, name, path=None):
Brian Harring7fcc02e2012-08-05 04:10:57 -070049 """Accessor method for getting a project node from the manifest tree.
50
51 Returns:
52 project element node from ElementTree, otherwise, None
53 """
Brian Harringb0043ab2012-08-05 04:09:56 -070054 if path is None:
55 # Use a unique value that can't ever match.
56 path = object()
57 for project in self.nodes.findall('project'):
58 if project.attrib['name'] == name or project.attrib['path'] == path:
Brian Harring7fcc02e2012-08-05 04:10:57 -070059 return project
60 return None
61
62 def ToString(self):
Brian Harringb0043ab2012-08-05 04:09:56 -070063 # Reset the tail for each node, then just do a hacky replace.
64 project = None
65 for project in self.nodes.findall('project'):
66 project.tail = '\n '
67 if project is not None:
68 # Tweak the last project to not have the trailing space.
69 project.tail = '\n'
70 # Fix manifest tag text and tail.
71 self.nodes.text = '\n '
72 self.nodes.tail = '\n'
73 return ElementTree.tostring(self.nodes)
74
75 def GetProjects(self):
76 return list(self.nodes.findall('project'))
77
78
Mike Frysingerc15efa52013-12-12 01:13:56 -050079def _AddProjectsToManifestGroups(options, *args):
Brian Harringb0043ab2012-08-05 04:09:56 -070080 """Enable the given manifest groups for the configured repository."""
81
Mike Frysingerc15efa52013-12-12 01:13:56 -050082 groups_to_enable = ['name:%s' % x for x in args]
Brian Harringb0043ab2012-08-05 04:09:56 -070083
84 git_config = options.git_config
85
David James67d73252013-09-19 17:33:12 -070086 cmd = ['config', '-f', git_config, '--get', 'manifest.groups']
87 enabled_groups = git.RunGit('.', cmd, error_code_ok=True).output.split(',')
Brian Harringb0043ab2012-08-05 04:09:56 -070088
89 # Note that ordering actually matters, thus why the following code
90 # is written this way.
91 # Per repo behaviour, enforce an appropriate platform group if
92 # we're converting from a default manifest group to a limited one.
93 # Finally, note we reprocess the existing groups; this is to allow
94 # us to cleanup any user screwups, or our own screwups.
95 requested_groups = (
96 ['minilayout', 'platform-%s' % (platform.system().lower(),)] +
97 enabled_groups + list(groups_to_enable))
98
99 processed_groups = set()
100 finalized_groups = []
101
102 for group in requested_groups:
103 if group not in processed_groups:
104 finalized_groups.append(group)
105 processed_groups.add(group)
106
David James67d73252013-09-19 17:33:12 -0700107 cmd = ['config', '-f', git_config, 'manifest.groups',
108 ','.join(finalized_groups)]
109 git.RunGit('.', cmd)
Brian Harringb0043ab2012-08-05 04:09:56 -0700110
111
112def _UpgradeMinilayout(options):
113 """Convert a repo checkout away from minilayout.xml to default.xml."""
114
115 full_tree = Manifest.FromPath(options.default_manifest_path)
116 local_manifest_exists = os.path.exists(options.local_manifest_path)
117
118 new_groups = []
119 if local_manifest_exists:
120 local_tree = Manifest.FromPath(options.local_manifest_path)
121 # Identify which projects need to be transferred across.
122 projects = local_tree.GetProjects()
123 new_groups = [x.attrib['name'] for x in projects]
124 allowed = set(x.attrib['name'] for x in full_tree.GetProjects())
125 transferred = [x for x in projects if x.attrib['name'] in allowed]
126 for project in transferred:
127 # Mangle local_manifest object, removing those projects;
128 # note we'll still be adding those projects to the default groups,
129 # including those that didn't intersect the main manifest.
130 local_tree.nodes.remove(project)
131
132 _AddProjectsToManifestGroups(options, *new_groups)
133
134 if local_manifest_exists:
135 # Rewrite the local_manifest now; if there is no settings left in
136 # the local_manifest, wipe it.
137 if local_tree.nodes.getchildren():
138 with open(options.local_manifest_path, 'w') as f:
139 f.write(local_tree.ToString())
140 else:
141 os.unlink(options.local_manifest_path)
142
143 # Finally, move the symlink.
144 os.unlink(options.manifest_sym_path)
145 os.symlink('manifests/default.xml', options.manifest_sym_path)
146 logging.info("Converted the checkout to manifest groups based minilayout.")
Brian Harring7fcc02e2012-08-05 04:10:57 -0700147
148
149def main(argv):
Brian Harringb0043ab2012-08-05 04:09:56 -0700150 parser = optparse.OptionParser(usage='usage: %prog add [options] <name> '
151 '<--workon | <path> --remote <remote> >')
Brian Harring7fcc02e2012-08-05 04:10:57 -0700152 parser.add_option('-w', '--workon', action='store_true', dest='workon',
153 default=False, help='Is this a workon package?')
Brian Harring7fcc02e2012-08-05 04:10:57 -0700154 parser.add_option('-r', '--remote', dest='remote',
155 default=None)
Rhyland Kleinb1d1f382012-08-23 11:53:45 -0400156 parser.add_option('-v', '--revision', dest='revision',
157 default=None,
158 help="Use to override the manifest defined default "
159 "revision used for a given project.")
Brian Harringb0043ab2012-08-05 04:09:56 -0700160 parser.add_option('--upgrade-minilayout', default=False, action='store_true',
161 help="Upgrade a minilayout checkout into a full.xml "
162 "checkout utilizing manifest groups.")
163 (options, args) = parser.parse_args(argv)
Brian Harring7fcc02e2012-08-05 04:10:57 -0700164
Ryan Cui0b1b94b2012-12-21 12:09:57 -0800165 repo_dir = git.FindRepoDir(os.getcwd())
Brian Harringb0043ab2012-08-05 04:09:56 -0700166 if not repo_dir:
167 parser.error("This script must be invoked from within a repository "
168 "checkout.")
Brian Harring7fcc02e2012-08-05 04:10:57 -0700169
Brian Harringb0043ab2012-08-05 04:09:56 -0700170 options.git_config = os.path.join(repo_dir, 'manifests.git', 'config')
171 options.repo_dir = repo_dir
172 options.local_manifest_path = os.path.join(repo_dir, 'local_manifest.xml')
173 # This constant is used only when we're doing an upgrade away from
174 # minilayout.xml to default.xml.
175 options.default_manifest_path = os.path.join(repo_dir, 'manifests',
176 'default.xml')
177 options.manifest_sym_path = os.path.join(repo_dir, 'manifest.xml')
178
179 active_manifest = os.path.basename(os.readlink(options.manifest_sym_path))
180 upgrade_required = active_manifest == 'minilayout.xml'
181
182 if options.upgrade_minilayout:
183 if args:
184 parser.error("--upgrade-minilayout takes no arguments.")
185 if not upgrade_required:
Mike Frysinger383367e2014-09-16 15:06:17 -0400186 print("This repository checkout isn't using minilayout.xml; "
187 "nothing to do")
Brian Harringb0043ab2012-08-05 04:09:56 -0700188 else:
189 _UpgradeMinilayout(options)
190 return 0
191 elif upgrade_required:
192 logging.warn(
193 "Your repository checkout is using the old minilayout.xml workflow; "
194 "auto-upgrading it.")
195 cros_build_lib.RunCommand(
196 [sys.argv[0], '--upgrade-minilayout'], cwd=os.getcwd(),
197 print_cmd=False)
198
199 if not args:
200 parser.error("No command specified.")
201 elif args[0] != 'add':
202 parser.error("Only supported subcommand is add right now.")
203 elif options.workon:
204 if len(args) != 2:
205 parser.error(
206 "Argument count is wrong for --workon; must be add <project>")
207 name, path = args[1], None
Brian Harring7fcc02e2012-08-05 04:10:57 -0700208 else:
Brian Harringb0043ab2012-08-05 04:09:56 -0700209 if options.remote is None:
210 parser.error('Adding non-workon projects requires a remote.')
211 elif len(args) != 3:
212 parser.error(
213 "Argument count is wrong for non-workon mode; "
214 "must be add <project> <path> --remote <remote-arg>")
215 name, path = args[1:]
Brian Harring7fcc02e2012-08-05 04:10:57 -0700216
Rhyland Kleinb1d1f382012-08-23 11:53:45 -0400217 revision = options.revision
218 if revision is not None:
David James97d95872012-11-16 15:09:56 -0800219 if (not git.IsRefsTags(revision) and
220 not git.IsSHA1(revision)):
221 revision = git.StripRefsHeads(revision, False)
Rhyland Kleinb1d1f382012-08-23 11:53:45 -0400222
Brian Harringb0043ab2012-08-05 04:09:56 -0700223 main_manifest = Manifest.FromPath(options.manifest_sym_path,
224 empty_if_missing=False)
225 local_manifest = Manifest.FromPath(options.local_manifest_path)
Brian Harring7fcc02e2012-08-05 04:10:57 -0700226
Brian Harringb0043ab2012-08-05 04:09:56 -0700227 main_element = main_manifest.GetProject(name, path=path)
Brian Harring7fcc02e2012-08-05 04:10:57 -0700228
Brian Harringb0043ab2012-08-05 04:09:56 -0700229 if options.workon:
230 if main_element is None:
231 parser.error('No project named %r in the default manifest.' % name)
232 _AddProjectsToManifestGroups(options, main_element.attrib['name'])
233
Rhyland Kleinf33f6222012-10-25 12:15:42 -0400234 elif main_element is not None:
Rhyland Kleine5faad52012-10-31 11:58:19 -0400235 if options.remote is not None:
236 # Likely this project wasn't meant to be remote, so workon main element
Mike Frysinger383367e2014-09-16 15:06:17 -0400237 print("Project already exists in manifest. Using that as workon project.")
Rhyland Kleine5faad52012-10-31 11:58:19 -0400238 _AddProjectsToManifestGroups(options, main_element.attrib['name'])
239 else:
240 # Conflict will occur; complain.
241 parser.error("Requested project name=%r path=%r will conflict with "
242 "your current manifest %s" % (name, path, active_manifest))
Brian Harringb0043ab2012-08-05 04:09:56 -0700243
244 elif local_manifest.GetProject(name, path=path) is not None:
245 parser.error("Requested project name=%r path=%r conflicts with "
246 "your local_manifest.xml" % (name, path))
247
248 else:
Rhyland Kleinb1d1f382012-08-23 11:53:45 -0400249 element = local_manifest.AddNonWorkonProject(name=name, path=path,
250 remote=options.remote,
251 revision=revision)
Brian Harringb0043ab2012-08-05 04:09:56 -0700252 _AddProjectsToManifestGroups(options, element.attrib['name'])
253
254 with open(options.local_manifest_path, 'w') as f:
255 f.write(local_manifest.ToString())
256 return 0