diff options
Diffstat (limited to 'gitc_utils.py')
| -rw-r--r-- | gitc_utils.py | 101 |
1 files changed, 51 insertions, 50 deletions
diff --git a/gitc_utils.py b/gitc_utils.py index b47e181c..486bbeb0 100644 --- a/gitc_utils.py +++ b/gitc_utils.py | |||
| @@ -1,5 +1,3 @@ | |||
| 1 | # -*- coding:utf-8 -*- | ||
| 2 | # | ||
| 3 | # Copyright (C) 2015 The Android Open Source Project | 1 | # Copyright (C) 2015 The Android Open Source Project |
| 4 | # | 2 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); | 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| @@ -14,8 +12,8 @@ | |||
| 14 | # See the License for the specific language governing permissions and | 12 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. | 13 | # limitations under the License. |
| 16 | 14 | ||
| 17 | from __future__ import print_function | ||
| 18 | import os | 15 | import os |
| 16 | import multiprocessing | ||
| 19 | import platform | 17 | import platform |
| 20 | import re | 18 | import re |
| 21 | import sys | 19 | import sys |
| @@ -29,12 +27,24 @@ from error import ManifestParseError | |||
| 29 | 27 | ||
| 30 | NUM_BATCH_RETRIEVE_REVISIONID = 32 | 28 | NUM_BATCH_RETRIEVE_REVISIONID = 32 |
| 31 | 29 | ||
| 30 | |||
| 32 | def get_gitc_manifest_dir(): | 31 | def get_gitc_manifest_dir(): |
| 33 | return wrapper.Wrapper().get_gitc_manifest_dir() | 32 | return wrapper.Wrapper().get_gitc_manifest_dir() |
| 34 | 33 | ||
| 34 | |||
| 35 | def parse_clientdir(gitc_fs_path): | 35 | def parse_clientdir(gitc_fs_path): |
| 36 | return wrapper.Wrapper().gitc_parse_clientdir(gitc_fs_path) | 36 | return wrapper.Wrapper().gitc_parse_clientdir(gitc_fs_path) |
| 37 | 37 | ||
| 38 | |||
| 39 | def _get_project_revision(args): | ||
| 40 | """Worker for _set_project_revisions to lookup one project remote.""" | ||
| 41 | (i, url, expr) = args | ||
| 42 | gitcmd = git_command.GitCommand( | ||
| 43 | None, ['ls-remote', url, expr], capture_stdout=True, cwd='/tmp') | ||
| 44 | rc = gitcmd.Wait() | ||
| 45 | return (i, rc, gitcmd.stdout.split('\t', 1)[0]) | ||
| 46 | |||
| 47 | |||
| 38 | def _set_project_revisions(projects): | 48 | def _set_project_revisions(projects): |
| 39 | """Sets the revisionExpr for a list of projects. | 49 | """Sets the revisionExpr for a list of projects. |
| 40 | 50 | ||
| @@ -42,47 +52,38 @@ def _set_project_revisions(projects): | |||
| 42 | should not be overly large. Recommend calling this function multiple times | 52 | should not be overly large. Recommend calling this function multiple times |
| 43 | with each call not exceeding NUM_BATCH_RETRIEVE_REVISIONID projects. | 53 | with each call not exceeding NUM_BATCH_RETRIEVE_REVISIONID projects. |
| 44 | 54 | ||
| 45 | @param projects: List of project objects to set the revionExpr for. | 55 | Args: |
| 56 | projects: List of project objects to set the revionExpr for. | ||
| 46 | """ | 57 | """ |
| 47 | # Retrieve the commit id for each project based off of it's current | 58 | # Retrieve the commit id for each project based off of it's current |
| 48 | # revisionExpr and it is not already a commit id. | 59 | # revisionExpr and it is not already a commit id. |
| 49 | project_gitcmds = [( | 60 | with multiprocessing.Pool(NUM_BATCH_RETRIEVE_REVISIONID) as pool: |
| 50 | project, git_command.GitCommand(None, | 61 | results_iter = pool.imap_unordered( |
| 51 | ['ls-remote', | 62 | _get_project_revision, |
| 52 | project.remote.url, | 63 | ((i, project.remote.url, project.revisionExpr) |
| 53 | project.revisionExpr], | 64 | for i, project in enumerate(projects) |
| 54 | capture_stdout=True, cwd='/tmp')) | 65 | if not git_config.IsId(project.revisionExpr)), |
| 55 | for project in projects if not git_config.IsId(project.revisionExpr)] | 66 | chunksize=8) |
| 56 | for proj, gitcmd in project_gitcmds: | 67 | for (i, rc, revisionExpr) in results_iter: |
| 57 | if gitcmd.Wait(): | 68 | project = projects[i] |
| 58 | print('FATAL: Failed to retrieve revisionExpr for %s' % proj) | 69 | if rc: |
| 59 | sys.exit(1) | 70 | print('FATAL: Failed to retrieve revisionExpr for %s' % project.name) |
| 60 | revisionExpr = gitcmd.stdout.split('\t')[0] | 71 | pool.terminate() |
| 61 | if not revisionExpr: | 72 | sys.exit(1) |
| 62 | raise ManifestParseError('Invalid SHA-1 revision project %s (%s)' % | 73 | if not revisionExpr: |
| 63 | (proj.remote.url, proj.revisionExpr)) | 74 | pool.terminate() |
| 64 | proj.revisionExpr = revisionExpr | 75 | raise ManifestParseError('Invalid SHA-1 revision project %s (%s)' % |
| 65 | 76 | (project.remote.url, project.revisionExpr)) | |
| 66 | def _manifest_groups(manifest): | 77 | project.revisionExpr = revisionExpr |
| 67 | """Returns the manifest group string that should be synced | 78 | |
| 68 | |||
| 69 | This is the same logic used by Command.GetProjects(), which is used during | ||
| 70 | repo sync | ||
| 71 | |||
| 72 | @param manifest: The XmlManifest object | ||
| 73 | """ | ||
| 74 | mp = manifest.manifestProject | ||
| 75 | groups = mp.config.GetString('manifest.groups') | ||
| 76 | if not groups: | ||
| 77 | groups = 'default,platform-' + platform.system().lower() | ||
| 78 | return groups | ||
| 79 | 79 | ||
| 80 | def generate_gitc_manifest(gitc_manifest, manifest, paths=None): | 80 | def generate_gitc_manifest(gitc_manifest, manifest, paths=None): |
| 81 | """Generate a manifest for shafsd to use for this GITC client. | 81 | """Generate a manifest for shafsd to use for this GITC client. |
| 82 | 82 | ||
| 83 | @param gitc_manifest: Current gitc manifest, or None if there isn't one yet. | 83 | Args: |
| 84 | @param manifest: A GitcManifest object loaded with the current repo manifest. | 84 | gitc_manifest: Current gitc manifest, or None if there isn't one yet. |
| 85 | @param paths: List of project paths we want to update. | 85 | manifest: A GitcManifest object loaded with the current repo manifest. |
| 86 | paths: List of project paths we want to update. | ||
| 86 | """ | 87 | """ |
| 87 | 88 | ||
| 88 | print('Generating GITC Manifest by fetching revision SHAs for each ' | 89 | print('Generating GITC Manifest by fetching revision SHAs for each ' |
| @@ -90,7 +91,7 @@ def generate_gitc_manifest(gitc_manifest, manifest, paths=None): | |||
| 90 | if paths is None: | 91 | if paths is None: |
| 91 | paths = list(manifest.paths.keys()) | 92 | paths = list(manifest.paths.keys()) |
| 92 | 93 | ||
| 93 | groups = [x for x in re.split(r'[,\s]+', _manifest_groups(manifest)) if x] | 94 | groups = [x for x in re.split(r'[,\s]+', manifest.GetGroupsStr()) if x] |
| 94 | 95 | ||
| 95 | # Convert the paths to projects, and filter them to the matched groups. | 96 | # Convert the paths to projects, and filter them to the matched groups. |
| 96 | projects = [manifest.paths[p] for p in paths] | 97 | projects = [manifest.paths[p] for p in paths] |
| @@ -104,11 +105,11 @@ def generate_gitc_manifest(gitc_manifest, manifest, paths=None): | |||
| 104 | if not proj.upstream and not git_config.IsId(proj.revisionExpr): | 105 | if not proj.upstream and not git_config.IsId(proj.revisionExpr): |
| 105 | proj.upstream = proj.revisionExpr | 106 | proj.upstream = proj.revisionExpr |
| 106 | 107 | ||
| 107 | if not path in gitc_manifest.paths: | 108 | if path not in gitc_manifest.paths: |
| 108 | # Any new projects need their first revision, even if we weren't asked | 109 | # Any new projects need their first revision, even if we weren't asked |
| 109 | # for them. | 110 | # for them. |
| 110 | projects.append(proj) | 111 | projects.append(proj) |
| 111 | elif not path in paths: | 112 | elif path not in paths: |
| 112 | # And copy revisions from the previous manifest if we're not updating | 113 | # And copy revisions from the previous manifest if we're not updating |
| 113 | # them now. | 114 | # them now. |
| 114 | gitc_proj = gitc_manifest.paths[path] | 115 | gitc_proj = gitc_manifest.paths[path] |
| @@ -118,11 +119,7 @@ def generate_gitc_manifest(gitc_manifest, manifest, paths=None): | |||
| 118 | else: | 119 | else: |
| 119 | proj.revisionExpr = gitc_proj.revisionExpr | 120 | proj.revisionExpr = gitc_proj.revisionExpr |
| 120 | 121 | ||
| 121 | index = 0 | 122 | _set_project_revisions(projects) |
| 122 | while index < len(projects): | ||
| 123 | _set_project_revisions( | ||
| 124 | projects[index:(index+NUM_BATCH_RETRIEVE_REVISIONID)]) | ||
| 125 | index += NUM_BATCH_RETRIEVE_REVISIONID | ||
| 126 | 123 | ||
| 127 | if gitc_manifest is not None: | 124 | if gitc_manifest is not None: |
| 128 | for path, proj in gitc_manifest.paths.items(): | 125 | for path, proj in gitc_manifest.paths.items(): |
| @@ -140,16 +137,20 @@ def generate_gitc_manifest(gitc_manifest, manifest, paths=None): | |||
| 140 | # Save the manifest. | 137 | # Save the manifest. |
| 141 | save_manifest(manifest) | 138 | save_manifest(manifest) |
| 142 | 139 | ||
| 140 | |||
| 143 | def save_manifest(manifest, client_dir=None): | 141 | def save_manifest(manifest, client_dir=None): |
| 144 | """Save the manifest file in the client_dir. | 142 | """Save the manifest file in the client_dir. |
| 145 | 143 | ||
| 146 | @param client_dir: Client directory to save the manifest in. | 144 | Args: |
| 147 | @param manifest: Manifest object to save. | 145 | manifest: Manifest object to save. |
| 146 | client_dir: Client directory to save the manifest in. | ||
| 148 | """ | 147 | """ |
| 149 | if not client_dir: | 148 | if not client_dir: |
| 150 | client_dir = manifest.gitc_client_dir | 149 | manifest_file = manifest.manifestFile |
| 151 | with open(os.path.join(client_dir, '.manifest'), 'w') as f: | 150 | else: |
| 152 | manifest.Save(f, groups=_manifest_groups(manifest)) | 151 | manifest_file = os.path.join(client_dir, '.manifest') |
| 152 | with open(manifest_file, 'w') as f: | ||
| 153 | manifest.Save(f, groups=manifest.GetGroupsStr()) | ||
| 153 | # TODO(sbasi/jorg): Come up with a solution to remove the sleep below. | 154 | # TODO(sbasi/jorg): Come up with a solution to remove the sleep below. |
| 154 | # Give the GITC filesystem time to register the manifest changes. | 155 | # Give the GITC filesystem time to register the manifest changes. |
| 155 | time.sleep(3) | 156 | time.sleep(3) |
