Mike Frysinger | e58c0e2 | 2017-10-04 15:43:30 -0400 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
Brian Harring | 7fcc02e | 2012-08-05 04:10:57 -0700 | [diff] [blame] | 2 | # Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
Mike Frysinger | 4ca6015 | 2016-09-01 00:13:36 -0400 | [diff] [blame] | 6 | """Manage projects in the local manifest.""" |
Brian Harring | 7fcc02e | 2012-08-05 04:10:57 -0700 | [diff] [blame] | 7 | |
Mike Frysinger | 383367e | 2014-09-16 15:06:17 -0400 | [diff] [blame] | 8 | from __future__ import print_function |
| 9 | |
Brian Harring | b0043ab | 2012-08-05 04:09:56 -0700 | [diff] [blame] | 10 | import platform |
Brian Harring | 7fcc02e | 2012-08-05 04:10:57 -0700 | [diff] [blame] | 11 | import os |
| 12 | import xml.etree.ElementTree as ElementTree |
Mike Frysinger | 750c5f5 | 2014-09-16 16:16:57 -0400 | [diff] [blame] | 13 | |
Mike Frysinger | 4ca6015 | 2016-09-01 00:13:36 -0400 | [diff] [blame] | 14 | from chromite.lib import commandline |
Brian Harring | b0043ab | 2012-08-05 04:09:56 -0700 | [diff] [blame] | 15 | from chromite.lib import cros_build_lib |
David James | 97d9587 | 2012-11-16 15:09:56 -0800 | [diff] [blame] | 16 | from chromite.lib import git |
Brian Harring | 7fcc02e | 2012-08-05 04:10:57 -0700 | [diff] [blame] | 17 | |
| 18 | |
Gwendal Grignou | 89afc08 | 2016-09-29 21:03:20 -0700 | [diff] [blame] | 19 | class LocalManifest(object): |
Brian Harring | 7fcc02e | 2012-08-05 04:10:57 -0700 | [diff] [blame] | 20 | """Class which provides an abstraction for manipulating the local manifest.""" |
| 21 | |
Brian Harring | b0043ab | 2012-08-05 04:09:56 -0700 | [diff] [blame] | 22 | @classmethod |
| 23 | def FromPath(cls, path, empty_if_missing=False): |
| 24 | if os.path.isfile(path): |
| 25 | with open(path) as f: |
| 26 | return cls(f.read()) |
| 27 | elif empty_if_missing: |
| 28 | cros_build_lib.Die('Manifest file, %r, not found' % path) |
| 29 | return cls() |
| 30 | |
Brian Harring | 7fcc02e | 2012-08-05 04:10:57 -0700 | [diff] [blame] | 31 | def __init__(self, text=None): |
| 32 | self._text = text or '<manifest>\n</manifest>' |
Brian Harring | b0043ab | 2012-08-05 04:09:56 -0700 | [diff] [blame] | 33 | self.nodes = ElementTree.fromstring(self._text) |
Brian Harring | 7fcc02e | 2012-08-05 04:10:57 -0700 | [diff] [blame] | 34 | |
Rhyland Klein | b1d1f38 | 2012-08-23 11:53:45 -0400 | [diff] [blame] | 35 | def AddNonWorkonProject(self, name, path, remote=None, revision=None): |
Brian Harring | b0043ab | 2012-08-05 04:09:56 -0700 | [diff] [blame] | 36 | """Add a new nonworkon project element to the manifest tree.""" |
| 37 | element = ElementTree.Element('project', name=name, path=path, |
Rhyland Klein | dd8ebbb | 2012-09-06 11:51:30 -0400 | [diff] [blame] | 38 | remote=remote) |
Brian Harring | b0043ab | 2012-08-05 04:09:56 -0700 | [diff] [blame] | 39 | element.attrib['workon'] = 'False' |
Rhyland Klein | dd8ebbb | 2012-09-06 11:51:30 -0400 | [diff] [blame] | 40 | if revision is not None: |
| 41 | element.attrib['revision'] = revision |
Brian Harring | b0043ab | 2012-08-05 04:09:56 -0700 | [diff] [blame] | 42 | self.nodes.append(element) |
| 43 | return element |
Brian Harring | 7fcc02e | 2012-08-05 04:10:57 -0700 | [diff] [blame] | 44 | |
Brian Harring | b0043ab | 2012-08-05 04:09:56 -0700 | [diff] [blame] | 45 | def GetProject(self, name, path=None): |
Brian Harring | 7fcc02e | 2012-08-05 04:10:57 -0700 | [diff] [blame] | 46 | """Accessor method for getting a project node from the manifest tree. |
| 47 | |
| 48 | Returns: |
| 49 | project element node from ElementTree, otherwise, None |
| 50 | """ |
Brian Harring | b0043ab | 2012-08-05 04:09:56 -0700 | [diff] [blame] | 51 | if path is None: |
| 52 | # Use a unique value that can't ever match. |
| 53 | path = object() |
| 54 | for project in self.nodes.findall('project'): |
| 55 | if project.attrib['name'] == name or project.attrib['path'] == path: |
Brian Harring | 7fcc02e | 2012-08-05 04:10:57 -0700 | [diff] [blame] | 56 | return project |
| 57 | return None |
| 58 | |
| 59 | def ToString(self): |
Brian Harring | b0043ab | 2012-08-05 04:09:56 -0700 | [diff] [blame] | 60 | # Reset the tail for each node, then just do a hacky replace. |
| 61 | project = None |
| 62 | for project in self.nodes.findall('project'): |
| 63 | project.tail = '\n ' |
| 64 | if project is not None: |
| 65 | # Tweak the last project to not have the trailing space. |
| 66 | project.tail = '\n' |
| 67 | # Fix manifest tag text and tail. |
| 68 | self.nodes.text = '\n ' |
| 69 | self.nodes.tail = '\n' |
| 70 | return ElementTree.tostring(self.nodes) |
| 71 | |
| 72 | def GetProjects(self): |
| 73 | return list(self.nodes.findall('project')) |
| 74 | |
| 75 | |
Gwendal Grignou | 89afc08 | 2016-09-29 21:03:20 -0700 | [diff] [blame] | 76 | def _AddProjectsToManifestGroups(options, new_group): |
Brian Harring | b0043ab | 2012-08-05 04:09:56 -0700 | [diff] [blame] | 77 | """Enable the given manifest groups for the configured repository.""" |
| 78 | |
Gwendal Grignou | 89afc08 | 2016-09-29 21:03:20 -0700 | [diff] [blame] | 79 | groups_to_enable = ['name:%s' % x for x in new_group] |
Brian Harring | b0043ab | 2012-08-05 04:09:56 -0700 | [diff] [blame] | 80 | |
| 81 | git_config = options.git_config |
| 82 | |
David James | 67d7325 | 2013-09-19 17:33:12 -0700 | [diff] [blame] | 83 | cmd = ['config', '-f', git_config, '--get', 'manifest.groups'] |
| 84 | enabled_groups = git.RunGit('.', cmd, error_code_ok=True).output.split(',') |
Brian Harring | b0043ab | 2012-08-05 04:09:56 -0700 | [diff] [blame] | 85 | |
| 86 | # Note that ordering actually matters, thus why the following code |
| 87 | # is written this way. |
| 88 | # Per repo behaviour, enforce an appropriate platform group if |
| 89 | # we're converting from a default manifest group to a limited one. |
| 90 | # Finally, note we reprocess the existing groups; this is to allow |
| 91 | # us to cleanup any user screwups, or our own screwups. |
| 92 | requested_groups = ( |
| 93 | ['minilayout', 'platform-%s' % (platform.system().lower(),)] + |
| 94 | enabled_groups + list(groups_to_enable)) |
| 95 | |
| 96 | processed_groups = set() |
| 97 | finalized_groups = [] |
| 98 | |
| 99 | for group in requested_groups: |
| 100 | if group not in processed_groups: |
| 101 | finalized_groups.append(group) |
| 102 | processed_groups.add(group) |
| 103 | |
David James | 67d7325 | 2013-09-19 17:33:12 -0700 | [diff] [blame] | 104 | cmd = ['config', '-f', git_config, 'manifest.groups', |
| 105 | ','.join(finalized_groups)] |
| 106 | git.RunGit('.', cmd) |
Brian Harring | b0043ab | 2012-08-05 04:09:56 -0700 | [diff] [blame] | 107 | |
| 108 | |
Gwendal Grignou | f9d6d36 | 2016-09-30 09:29:20 -0700 | [diff] [blame] | 109 | def _AssertNotMiniLayout(): |
| 110 | cros_build_lib.Die( |
Mike Frysinger | 80de501 | 2019-08-01 14:10:53 -0400 | [diff] [blame^] | 111 | 'Your repository checkout is using the old minilayout.xml workflow; ' |
| 112 | 'Autoupdate is no longer supported, reinstall your tree.') |
Brian Harring | 7fcc02e | 2012-08-05 04:10:57 -0700 | [diff] [blame] | 113 | |
| 114 | |
Mike Frysinger | 4ca6015 | 2016-09-01 00:13:36 -0400 | [diff] [blame] | 115 | def GetParser(): |
| 116 | """Return a command line parser.""" |
| 117 | parser = commandline.ArgumentParser(description=__doc__) |
Mike Frysinger | 1b8565b | 2016-09-13 16:03:49 -0400 | [diff] [blame] | 118 | |
| 119 | subparsers = parser.add_subparsers(dest='command') |
| 120 | |
| 121 | subparser = subparsers.add_parser( |
| 122 | 'add', |
| 123 | help='Add projects to the manifest.') |
| 124 | subparser.add_argument('-w', '--workon', action='store_true', |
| 125 | default=False, help='Is this a workon package?') |
| 126 | subparser.add_argument('-r', '--remote', |
| 127 | help='Remote project name (for non-workon packages).') |
| 128 | subparser.add_argument('-v', '--revision', |
| 129 | help='Use to override the manifest defined default ' |
| 130 | 'revision used for a given project.') |
| 131 | subparser.add_argument('project', help='Name of project in the manifest.') |
| 132 | subparser.add_argument('path', nargs='?', help='Local path to the project.') |
| 133 | |
Mike Frysinger | 4ca6015 | 2016-09-01 00:13:36 -0400 | [diff] [blame] | 134 | return parser |
| 135 | |
| 136 | |
Brian Harring | 7fcc02e | 2012-08-05 04:10:57 -0700 | [diff] [blame] | 137 | def main(argv): |
Mike Frysinger | 4ca6015 | 2016-09-01 00:13:36 -0400 | [diff] [blame] | 138 | parser = GetParser() |
| 139 | options = parser.parse_args(argv) |
Ryan Cui | 0b1b94b | 2012-12-21 12:09:57 -0800 | [diff] [blame] | 140 | repo_dir = git.FindRepoDir(os.getcwd()) |
Brian Harring | b0043ab | 2012-08-05 04:09:56 -0700 | [diff] [blame] | 141 | if not repo_dir: |
Mike Frysinger | 80de501 | 2019-08-01 14:10:53 -0400 | [diff] [blame^] | 142 | parser.error('This script must be invoked from within a repository ' |
| 143 | 'checkout.') |
Brian Harring | 7fcc02e | 2012-08-05 04:10:57 -0700 | [diff] [blame] | 144 | |
Brian Harring | b0043ab | 2012-08-05 04:09:56 -0700 | [diff] [blame] | 145 | options.git_config = os.path.join(repo_dir, 'manifests.git', 'config') |
Brian Harring | b0043ab | 2012-08-05 04:09:56 -0700 | [diff] [blame] | 146 | options.local_manifest_path = os.path.join(repo_dir, 'local_manifest.xml') |
Brian Harring | b0043ab | 2012-08-05 04:09:56 -0700 | [diff] [blame] | 147 | |
Gwendal Grignou | 89afc08 | 2016-09-29 21:03:20 -0700 | [diff] [blame] | 148 | manifest_sym_path = os.path.join(repo_dir, 'manifest.xml') |
| 149 | if os.path.basename(os.readlink(manifest_sym_path)) == 'minilayout.xml': |
Gwendal Grignou | f9d6d36 | 2016-09-30 09:29:20 -0700 | [diff] [blame] | 150 | _AssertNotMiniLayout() |
Brian Harring | b0043ab | 2012-08-05 04:09:56 -0700 | [diff] [blame] | 151 | |
Mike Frysinger | 1b8565b | 2016-09-13 16:03:49 -0400 | [diff] [blame] | 152 | # For now, we only support the add command. |
| 153 | assert options.command == 'add' |
| 154 | if options.workon: |
| 155 | if options.path is not None: |
| 156 | parser.error('Adding workon projects do not set project.') |
Brian Harring | 7fcc02e | 2012-08-05 04:10:57 -0700 | [diff] [blame] | 157 | else: |
Brian Harring | b0043ab | 2012-08-05 04:09:56 -0700 | [diff] [blame] | 158 | if options.remote is None: |
| 159 | parser.error('Adding non-workon projects requires a remote.') |
Mike Frysinger | 1b8565b | 2016-09-13 16:03:49 -0400 | [diff] [blame] | 160 | if options.path is None: |
| 161 | parser.error('Adding non-workon projects requires a path.') |
| 162 | name = options.project |
| 163 | path = options.path |
Rhyland Klein | b1d1f38 | 2012-08-23 11:53:45 -0400 | [diff] [blame] | 164 | revision = options.revision |
| 165 | if revision is not None: |
David James | 97d9587 | 2012-11-16 15:09:56 -0800 | [diff] [blame] | 166 | if (not git.IsRefsTags(revision) and |
| 167 | not git.IsSHA1(revision)): |
| 168 | revision = git.StripRefsHeads(revision, False) |
Rhyland Klein | b1d1f38 | 2012-08-23 11:53:45 -0400 | [diff] [blame] | 169 | |
Gwendal Grignou | 89afc08 | 2016-09-29 21:03:20 -0700 | [diff] [blame] | 170 | main_manifest = git.ManifestCheckout(os.getcwd()) |
| 171 | main_element = main_manifest.FindCheckouts(name) |
| 172 | if path is not None: |
| 173 | main_element_from_path = main_manifest.FindCheckoutFromPath( |
| 174 | path, strict=False) |
| 175 | if main_element_from_path is not None: |
| 176 | main_element.append(main_element_from_path) |
Brian Harring | 7fcc02e | 2012-08-05 04:10:57 -0700 | [diff] [blame] | 177 | |
Gwendal Grignou | 89afc08 | 2016-09-29 21:03:20 -0700 | [diff] [blame] | 178 | local_manifest = LocalManifest.FromPath(options.local_manifest_path) |
Brian Harring | 7fcc02e | 2012-08-05 04:10:57 -0700 | [diff] [blame] | 179 | |
Brian Harring | b0043ab | 2012-08-05 04:09:56 -0700 | [diff] [blame] | 180 | if options.workon: |
Gwendal Grignou | 89afc08 | 2016-09-29 21:03:20 -0700 | [diff] [blame] | 181 | if not main_element: |
Brian Harring | b0043ab | 2012-08-05 04:09:56 -0700 | [diff] [blame] | 182 | parser.error('No project named %r in the default manifest.' % name) |
Gwendal Grignou | 89afc08 | 2016-09-29 21:03:20 -0700 | [diff] [blame] | 183 | _AddProjectsToManifestGroups( |
| 184 | options, [checkout['name'] for checkout in main_element]) |
Brian Harring | b0043ab | 2012-08-05 04:09:56 -0700 | [diff] [blame] | 185 | |
Gwendal Grignou | 89afc08 | 2016-09-29 21:03:20 -0700 | [diff] [blame] | 186 | elif main_element: |
Rhyland Klein | e5faad5 | 2012-10-31 11:58:19 -0400 | [diff] [blame] | 187 | if options.remote is not None: |
| 188 | # Likely this project wasn't meant to be remote, so workon main element |
Mike Frysinger | 80de501 | 2019-08-01 14:10:53 -0400 | [diff] [blame^] | 189 | print('Project already exists in manifest. Using that as workon project.') |
Gwendal Grignou | 89afc08 | 2016-09-29 21:03:20 -0700 | [diff] [blame] | 190 | _AddProjectsToManifestGroups( |
| 191 | options, [checkout['name'] for checkout in main_element]) |
Rhyland Klein | e5faad5 | 2012-10-31 11:58:19 -0400 | [diff] [blame] | 192 | else: |
| 193 | # Conflict will occur; complain. |
Mike Frysinger | 80de501 | 2019-08-01 14:10:53 -0400 | [diff] [blame^] | 194 | parser.error('Requested project name=%r path=%r will conflict with ' |
| 195 | 'your current manifest %s' % ( |
Gwendal Grignou | 89afc08 | 2016-09-29 21:03:20 -0700 | [diff] [blame] | 196 | name, path, main_manifest.manifest_path)) |
Brian Harring | b0043ab | 2012-08-05 04:09:56 -0700 | [diff] [blame] | 197 | |
| 198 | elif local_manifest.GetProject(name, path=path) is not None: |
Mike Frysinger | 80de501 | 2019-08-01 14:10:53 -0400 | [diff] [blame^] | 199 | parser.error('Requested project name=%r path=%r conflicts with ' |
| 200 | 'your local_manifest.xml' % (name, path)) |
Brian Harring | b0043ab | 2012-08-05 04:09:56 -0700 | [diff] [blame] | 201 | |
| 202 | else: |
Rhyland Klein | b1d1f38 | 2012-08-23 11:53:45 -0400 | [diff] [blame] | 203 | element = local_manifest.AddNonWorkonProject(name=name, path=path, |
| 204 | remote=options.remote, |
| 205 | revision=revision) |
Gwendal Grignou | 89afc08 | 2016-09-29 21:03:20 -0700 | [diff] [blame] | 206 | _AddProjectsToManifestGroups(options, [element.attrib['name']]) |
Brian Harring | b0043ab | 2012-08-05 04:09:56 -0700 | [diff] [blame] | 207 | |
| 208 | with open(options.local_manifest_path, 'w') as f: |
| 209 | f.write(local_manifest.ToString()) |
| 210 | return 0 |