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 | 250303b | 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 | |
Dan Willemsen | 250303b | 2015-09-08 13:27:20 -0700 | [diff] [blame] | 27 | from manifest_xml import GitcManifest |
Simran Basi | bdb5271 | 2015-08-10 13:23:23 -0700 | [diff] [blame] | 28 | |
Simran Basi | bdb5271 | 2015-08-10 13:23:23 -0700 | [diff] [blame] | 29 | GITC_FS_ROOT_DIR = '/gitc/manifest-rw/' |
| 30 | NUM_BATCH_RETRIEVE_REVISIONID = 300 |
| 31 | |
Simran Basi | 8ce5041 | 2015-08-28 14:25:44 -0700 | [diff] [blame] | 32 | def get_gitc_manifest_dir(): |
| 33 | return wrapper.Wrapper().get_gitc_manifest_dir() |
| 34 | |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 35 | def parse_clientdir(gitc_fs_path): |
| 36 | """Parse a path in the GITC FS and return its client name. |
| 37 | |
| 38 | @param gitc_fs_path: A subdirectory path within the GITC_FS_ROOT_DIR. |
| 39 | |
| 40 | @returns: The GITC client name |
| 41 | """ |
| 42 | if (gitc_fs_path == GITC_FS_ROOT_DIR or |
| 43 | not gitc_fs_path.startswith(GITC_FS_ROOT_DIR)): |
| 44 | return None |
| 45 | return gitc_fs_path.split(GITC_FS_ROOT_DIR)[1].split('/')[0] |
| 46 | |
Simran Basi | bdb5271 | 2015-08-10 13:23:23 -0700 | [diff] [blame] | 47 | def _set_project_revisions(projects): |
| 48 | """Sets the revisionExpr for a list of projects. |
| 49 | |
| 50 | Because of the limit of open file descriptors allowed, length of projects |
| 51 | should not be overly large. Recommend calling this function multiple times |
| 52 | with each call not exceeding NUM_BATCH_RETRIEVE_REVISIONID projects. |
| 53 | |
| 54 | @param projects: List of project objects to set the revionExpr for. |
| 55 | """ |
| 56 | # Retrieve the commit id for each project based off of it's current |
| 57 | # revisionExpr and it is not already a commit id. |
| 58 | project_gitcmds = [( |
| 59 | project, git_command.GitCommand(None, |
| 60 | ['ls-remote', |
| 61 | project.remote.url, |
| 62 | project.revisionExpr], |
| 63 | capture_stdout=True, cwd='/tmp')) |
| 64 | for project in projects if not git_config.IsId(project.revisionExpr)] |
| 65 | for proj, gitcmd in project_gitcmds: |
| 66 | if gitcmd.Wait(): |
David Pursehouse | 022a1d4 | 2015-08-20 16:41:04 +0900 | [diff] [blame] | 67 | print('FATAL: Failed to retrieve revisionExpr for %s' % proj) |
Simran Basi | bdb5271 | 2015-08-10 13:23:23 -0700 | [diff] [blame] | 68 | sys.exit(1) |
| 69 | proj.revisionExpr = gitcmd.stdout.split('\t')[0] |
| 70 | |
Dan Willemsen | 250303b | 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 | |
| 77 | @param manifest: The XmlManifest object |
| 78 | """ |
| 79 | mp = manifest.manifestProject |
| 80 | groups = mp.config.GetString('manifest.groups') |
| 81 | if not groups: |
| 82 | groups = 'default,platform-' + platform.system().lower() |
| 83 | return groups |
| 84 | |
| 85 | def generate_gitc_manifest(repodir, client_name, gitc_manifest, repo_manifest_file, paths=None): |
Simran Basi | bdb5271 | 2015-08-10 13:23:23 -0700 | [diff] [blame] | 86 | """Generate a manifest for shafsd to use for this GITC client. |
| 87 | |
Dan Willemsen | 250303b | 2015-09-08 13:27:20 -0700 | [diff] [blame] | 88 | @param repodir: The repo directory |
| 89 | @param client_name: The gitc client name |
| 90 | @param gitc_manifest: Current gitc manifest, or None if there isn't one yet |
| 91 | @param repo_manifest_file: The file used by the main repo manifest |
| 92 | @param paths: List of project paths we want to update. |
Simran Basi | bdb5271 | 2015-08-10 13:23:23 -0700 | [diff] [blame] | 93 | """ |
Dan Willemsen | 250303b | 2015-09-08 13:27:20 -0700 | [diff] [blame] | 94 | manifest = GitcManifest(repodir, client_name) |
| 95 | manifest.Override(repo_manifest_file) |
| 96 | |
Simran Basi | bdb5271 | 2015-08-10 13:23:23 -0700 | [diff] [blame] | 97 | print('Generating GITC Manifest by fetching revision SHAs for each ' |
| 98 | 'project.') |
Dan Willemsen | 250303b | 2015-09-08 13:27:20 -0700 | [diff] [blame] | 99 | if paths is None: |
| 100 | paths = manifest.paths.keys() |
| 101 | |
| 102 | groups = [x for x in re.split(r'[,\s]+', _manifest_groups(manifest)) if x] |
| 103 | |
| 104 | # Convert the paths to projects, and filter them to the matched groups. |
| 105 | projects = [manifest.paths[p] for p in paths] |
| 106 | projects = [p for p in projects if p.MatchesGroups(groups)] |
| 107 | |
| 108 | if gitc_manifest is not None: |
| 109 | for path, proj in manifest.paths.iteritems(): |
| 110 | if not proj.MatchesGroups(groups): |
| 111 | continue |
| 112 | |
| 113 | if not proj.upstream and not git_config.IsId(proj.revisionExpr): |
| 114 | proj.upstream = proj.revisionExpr |
| 115 | |
| 116 | if not path in gitc_manifest.paths: |
| 117 | # Any new projects need their first revision, even if we weren't asked |
| 118 | # for them. |
| 119 | projects.append(proj) |
| 120 | elif not path in paths: |
| 121 | # And copy revisions from the previous manifest if we're not updating |
| 122 | # them now. |
| 123 | gitc_proj = gitc_manifest.paths[path] |
| 124 | if gitc_proj.old_revision: |
| 125 | proj.revisionExpr = None |
| 126 | proj.old_revision = gitc_proj.old_revision |
| 127 | else: |
| 128 | proj.revisionExpr = gitc_proj.revisionExpr |
| 129 | |
Simran Basi | bdb5271 | 2015-08-10 13:23:23 -0700 | [diff] [blame] | 130 | index = 0 |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 131 | while index < len(projects): |
Simran Basi | bdb5271 | 2015-08-10 13:23:23 -0700 | [diff] [blame] | 132 | _set_project_revisions( |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 133 | projects[index:(index+NUM_BATCH_RETRIEVE_REVISIONID)]) |
Simran Basi | bdb5271 | 2015-08-10 13:23:23 -0700 | [diff] [blame] | 134 | index += NUM_BATCH_RETRIEVE_REVISIONID |
Dan Willemsen | 250303b | 2015-09-08 13:27:20 -0700 | [diff] [blame] | 135 | |
| 136 | if gitc_manifest is not None: |
| 137 | for path, proj in gitc_manifest.paths.iteritems(): |
| 138 | if proj.old_revision and path in paths: |
| 139 | # If we updated a project that has been started, keep the old-revision |
| 140 | # updated. |
| 141 | repo_proj = manifest.paths[path] |
| 142 | repo_proj.old_revision = repo_proj.revisionExpr |
| 143 | repo_proj.revisionExpr = None |
| 144 | |
| 145 | # Convert URLs from relative to absolute. |
| 146 | for name, remote in manifest.remotes.iteritems(): |
| 147 | remote.fetchUrl = remote.resolvedFetchUrl |
| 148 | |
Simran Basi | bdb5271 | 2015-08-10 13:23:23 -0700 | [diff] [blame] | 149 | # Save the manifest. |
Dan Willemsen | 250303b | 2015-09-08 13:27:20 -0700 | [diff] [blame] | 150 | save_manifest(manifest) |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 151 | |
| 152 | def save_manifest(manifest, client_dir=None): |
| 153 | """Save the manifest file in the client_dir. |
| 154 | |
| 155 | @param client_dir: Client directory to save the manifest in. |
| 156 | @param manifest: Manifest object to save. |
| 157 | """ |
| 158 | if not client_dir: |
| 159 | client_dir = manifest.gitc_client_dir |
Simran Basi | bdb5271 | 2015-08-10 13:23:23 -0700 | [diff] [blame] | 160 | with open(os.path.join(client_dir, '.manifest'), 'w') as f: |
Dan Willemsen | 250303b | 2015-09-08 13:27:20 -0700 | [diff] [blame] | 161 | manifest.Save(f, groups=_manifest_groups(manifest)) |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 162 | # TODO(sbasi/jorg): Come up with a solution to remove the sleep below. |
| 163 | # Give the GITC filesystem time to register the manifest changes. |
Dan Willemsen | 250303b | 2015-09-08 13:27:20 -0700 | [diff] [blame] | 164 | time.sleep(3) |