Mike Frysinger | f601376 | 2019-06-13 02:30:51 -0400 | [diff] [blame] | 1 | # -*- coding:utf-8 -*- |
Simran Basi | bdb5271 | 2015-08-10 13:23:23 -0700 | [diff] [blame] | 2 | # |
| 3 | # Copyright (C) 2015 The Android Open Source Project |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | |
| 17 | from __future__ import print_function |
| 18 | import os |
Dan Willemsen | 5ea32d1 | 2015-09-08 13:27:20 -0700 | [diff] [blame] | 19 | import platform |
| 20 | import re |
David Pursehouse | 46496d8 | 2015-08-20 16:37:09 +0900 | [diff] [blame] | 21 | import sys |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 22 | import time |
Simran Basi | bdb5271 | 2015-08-10 13:23:23 -0700 | [diff] [blame] | 23 | |
| 24 | import git_command |
| 25 | import git_config |
Simran Basi | 8ce5041 | 2015-08-28 14:25:44 -0700 | [diff] [blame] | 26 | import wrapper |
Simran Basi | bdb5271 | 2015-08-10 13:23:23 -0700 | [diff] [blame] | 27 | |
Xin Li | f97e72e | 2016-06-24 12:17:14 -0700 | [diff] [blame] | 28 | from error import ManifestParseError |
| 29 | |
Dan Willemsen | 39252ba | 2016-08-23 14:06:59 -0700 | [diff] [blame] | 30 | NUM_BATCH_RETRIEVE_REVISIONID = 32 |
Simran Basi | bdb5271 | 2015-08-10 13:23:23 -0700 | [diff] [blame] | 31 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 32 | |
Simran Basi | 8ce5041 | 2015-08-28 14:25:44 -0700 | [diff] [blame] | 33 | def get_gitc_manifest_dir(): |
| 34 | return wrapper.Wrapper().get_gitc_manifest_dir() |
| 35 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 36 | |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 37 | def parse_clientdir(gitc_fs_path): |
Dan Willemsen | 745b4ad | 2015-10-06 15:23:19 -0700 | [diff] [blame] | 38 | return wrapper.Wrapper().gitc_parse_clientdir(gitc_fs_path) |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 39 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 40 | |
Simran Basi | bdb5271 | 2015-08-10 13:23:23 -0700 | [diff] [blame] | 41 | def _set_project_revisions(projects): |
| 42 | """Sets the revisionExpr for a list of projects. |
| 43 | |
| 44 | Because of the limit of open file descriptors allowed, length of projects |
| 45 | should not be overly large. Recommend calling this function multiple times |
| 46 | with each call not exceeding NUM_BATCH_RETRIEVE_REVISIONID projects. |
| 47 | |
Mike Frysinger | 0a849b6 | 2020-12-11 03:26:42 -0500 | [diff] [blame^] | 48 | Args: |
| 49 | projects: List of project objects to set the revionExpr for. |
Simran Basi | bdb5271 | 2015-08-10 13:23:23 -0700 | [diff] [blame] | 50 | """ |
| 51 | # Retrieve the commit id for each project based off of it's current |
| 52 | # revisionExpr and it is not already a commit id. |
| 53 | project_gitcmds = [( |
| 54 | project, git_command.GitCommand(None, |
| 55 | ['ls-remote', |
| 56 | project.remote.url, |
| 57 | project.revisionExpr], |
| 58 | capture_stdout=True, cwd='/tmp')) |
David Pursehouse | abdf750 | 2020-02-12 14:58:39 +0900 | [diff] [blame] | 59 | for project in projects if not git_config.IsId(project.revisionExpr)] |
Simran Basi | bdb5271 | 2015-08-10 13:23:23 -0700 | [diff] [blame] | 60 | for proj, gitcmd in project_gitcmds: |
| 61 | if gitcmd.Wait(): |
David Pursehouse | 022a1d4 | 2015-08-20 16:41:04 +0900 | [diff] [blame] | 62 | print('FATAL: Failed to retrieve revisionExpr for %s' % proj) |
Simran Basi | bdb5271 | 2015-08-10 13:23:23 -0700 | [diff] [blame] | 63 | sys.exit(1) |
Xin Li | f97e72e | 2016-06-24 12:17:14 -0700 | [diff] [blame] | 64 | revisionExpr = gitcmd.stdout.split('\t')[0] |
| 65 | if not revisionExpr: |
Mike Frysinger | 31067c0 | 2019-06-13 02:13:23 -0400 | [diff] [blame] | 66 | raise ManifestParseError('Invalid SHA-1 revision project %s (%s)' % |
| 67 | (proj.remote.url, proj.revisionExpr)) |
Xin Li | f97e72e | 2016-06-24 12:17:14 -0700 | [diff] [blame] | 68 | proj.revisionExpr = revisionExpr |
Simran Basi | bdb5271 | 2015-08-10 13:23:23 -0700 | [diff] [blame] | 69 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 70 | |
Dan Willemsen | 5ea32d1 | 2015-09-08 13:27:20 -0700 | [diff] [blame] | 71 | def _manifest_groups(manifest): |
| 72 | """Returns the manifest group string that should be synced |
| 73 | |
| 74 | This is the same logic used by Command.GetProjects(), which is used during |
| 75 | repo sync |
| 76 | |
Mike Frysinger | 0a849b6 | 2020-12-11 03:26:42 -0500 | [diff] [blame^] | 77 | Args: |
| 78 | manifest: The XmlManifest object |
Dan Willemsen | 5ea32d1 | 2015-09-08 13:27:20 -0700 | [diff] [blame] | 79 | """ |
| 80 | mp = manifest.manifestProject |
| 81 | groups = mp.config.GetString('manifest.groups') |
| 82 | if not groups: |
| 83 | groups = 'default,platform-' + platform.system().lower() |
| 84 | return groups |
| 85 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 86 | |
Dan Willemsen | 5ea32d1 | 2015-09-08 13:27:20 -0700 | [diff] [blame] | 87 | def generate_gitc_manifest(gitc_manifest, manifest, paths=None): |
Simran Basi | bdb5271 | 2015-08-10 13:23:23 -0700 | [diff] [blame] | 88 | """Generate a manifest for shafsd to use for this GITC client. |
| 89 | |
Mike Frysinger | 0a849b6 | 2020-12-11 03:26:42 -0500 | [diff] [blame^] | 90 | Args: |
| 91 | gitc_manifest: Current gitc manifest, or None if there isn't one yet. |
| 92 | manifest: A GitcManifest object loaded with the current repo manifest. |
| 93 | paths: List of project paths we want to update. |
Simran Basi | bdb5271 | 2015-08-10 13:23:23 -0700 | [diff] [blame] | 94 | """ |
Dan Willemsen | 5ea32d1 | 2015-09-08 13:27:20 -0700 | [diff] [blame] | 95 | |
Simran Basi | bdb5271 | 2015-08-10 13:23:23 -0700 | [diff] [blame] | 96 | print('Generating GITC Manifest by fetching revision SHAs for each ' |
| 97 | 'project.') |
Dan Willemsen | 5ea32d1 | 2015-09-08 13:27:20 -0700 | [diff] [blame] | 98 | if paths is None: |
Mike Frysinger | 31067c0 | 2019-06-13 02:13:23 -0400 | [diff] [blame] | 99 | paths = list(manifest.paths.keys()) |
Dan Willemsen | 5ea32d1 | 2015-09-08 13:27:20 -0700 | [diff] [blame] | 100 | |
| 101 | groups = [x for x in re.split(r'[,\s]+', _manifest_groups(manifest)) if x] |
| 102 | |
| 103 | # Convert the paths to projects, and filter them to the matched groups. |
| 104 | projects = [manifest.paths[p] for p in paths] |
| 105 | projects = [p for p in projects if p.MatchesGroups(groups)] |
| 106 | |
| 107 | if gitc_manifest is not None: |
Mike Frysinger | 31067c0 | 2019-06-13 02:13:23 -0400 | [diff] [blame] | 108 | for path, proj in manifest.paths.items(): |
Dan Willemsen | 5ea32d1 | 2015-09-08 13:27:20 -0700 | [diff] [blame] | 109 | if not proj.MatchesGroups(groups): |
| 110 | continue |
| 111 | |
| 112 | if not proj.upstream and not git_config.IsId(proj.revisionExpr): |
| 113 | proj.upstream = proj.revisionExpr |
| 114 | |
David Pursehouse | eeff353 | 2020-02-12 11:24:10 +0900 | [diff] [blame] | 115 | if path not in gitc_manifest.paths: |
Dan Willemsen | 5ea32d1 | 2015-09-08 13:27:20 -0700 | [diff] [blame] | 116 | # Any new projects need their first revision, even if we weren't asked |
| 117 | # for them. |
| 118 | projects.append(proj) |
David Pursehouse | eeff353 | 2020-02-12 11:24:10 +0900 | [diff] [blame] | 119 | elif path not in paths: |
Dan Willemsen | 5ea32d1 | 2015-09-08 13:27:20 -0700 | [diff] [blame] | 120 | # And copy revisions from the previous manifest if we're not updating |
| 121 | # them now. |
| 122 | gitc_proj = gitc_manifest.paths[path] |
| 123 | if gitc_proj.old_revision: |
| 124 | proj.revisionExpr = None |
| 125 | proj.old_revision = gitc_proj.old_revision |
| 126 | else: |
| 127 | proj.revisionExpr = gitc_proj.revisionExpr |
| 128 | |
Simran Basi | bdb5271 | 2015-08-10 13:23:23 -0700 | [diff] [blame] | 129 | index = 0 |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 130 | while index < len(projects): |
Simran Basi | bdb5271 | 2015-08-10 13:23:23 -0700 | [diff] [blame] | 131 | _set_project_revisions( |
David Pursehouse | 54a4e60 | 2020-02-12 14:31:05 +0900 | [diff] [blame] | 132 | projects[index:(index + NUM_BATCH_RETRIEVE_REVISIONID)]) |
Simran Basi | bdb5271 | 2015-08-10 13:23:23 -0700 | [diff] [blame] | 133 | index += NUM_BATCH_RETRIEVE_REVISIONID |
Dan Willemsen | 5ea32d1 | 2015-09-08 13:27:20 -0700 | [diff] [blame] | 134 | |
| 135 | if gitc_manifest is not None: |
Mike Frysinger | 31067c0 | 2019-06-13 02:13:23 -0400 | [diff] [blame] | 136 | for path, proj in gitc_manifest.paths.items(): |
Dan Willemsen | 5ea32d1 | 2015-09-08 13:27:20 -0700 | [diff] [blame] | 137 | if proj.old_revision and path in paths: |
| 138 | # If we updated a project that has been started, keep the old-revision |
| 139 | # updated. |
| 140 | repo_proj = manifest.paths[path] |
| 141 | repo_proj.old_revision = repo_proj.revisionExpr |
| 142 | repo_proj.revisionExpr = None |
| 143 | |
| 144 | # Convert URLs from relative to absolute. |
Mike Frysinger | 31067c0 | 2019-06-13 02:13:23 -0400 | [diff] [blame] | 145 | for _name, remote in manifest.remotes.items(): |
Dan Willemsen | 5ea32d1 | 2015-09-08 13:27:20 -0700 | [diff] [blame] | 146 | remote.fetchUrl = remote.resolvedFetchUrl |
| 147 | |
Simran Basi | bdb5271 | 2015-08-10 13:23:23 -0700 | [diff] [blame] | 148 | # Save the manifest. |
Dan Willemsen | 5ea32d1 | 2015-09-08 13:27:20 -0700 | [diff] [blame] | 149 | save_manifest(manifest) |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 150 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 151 | |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 152 | def save_manifest(manifest, client_dir=None): |
| 153 | """Save the manifest file in the client_dir. |
| 154 | |
Mike Frysinger | 0a849b6 | 2020-12-11 03:26:42 -0500 | [diff] [blame^] | 155 | Args: |
| 156 | manifest: Manifest object to save. |
| 157 | client_dir: Client directory to save the manifest in. |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 158 | """ |
| 159 | if not client_dir: |
| 160 | client_dir = manifest.gitc_client_dir |
Simran Basi | bdb5271 | 2015-08-10 13:23:23 -0700 | [diff] [blame] | 161 | with open(os.path.join(client_dir, '.manifest'), 'w') as f: |
Dan Willemsen | 5ea32d1 | 2015-09-08 13:27:20 -0700 | [diff] [blame] | 162 | manifest.Save(f, groups=_manifest_groups(manifest)) |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 163 | # TODO(sbasi/jorg): Come up with a solution to remove the sleep below. |
| 164 | # Give the GITC filesystem time to register the manifest changes. |
Dan Willemsen | 5ea32d1 | 2015-09-08 13:27:20 -0700 | [diff] [blame] | 165 | time.sleep(3) |