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