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