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