summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMike Frysinger <vapier@google.com>2021-02-16 15:01:39 -0500
committerMike Frysinger <vapier@google.com>2021-02-18 07:11:07 +0000
commit8c1e9e62a3214a7ab5feeb7ce194ca153cc8afb1 (patch)
treebb464a9a1f7603bf5fd929ea1fd968d363228675
parent84230009ee4282b947482f0d4fc4fe9e9ebc9e01 (diff)
downloadgit-repo-8c1e9e62a3214a7ab5feeb7ce194ca153cc8afb1.tar.gz
gitc_utils: rewrite to use multiprocessing
This is the only code in the tree that uses GitCommand asynchronously. Rewrite it to use multiprocessing.Pool as it makes the code a little bit easier to understand and simpler. Change-Id: I3ed3b037f24aa1e9dfe8eec9ec21815cdda7678a Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/297143 Tested-by: Mike Frysinger <vapier@google.com> Reviewed-by: Michael Mortensen <mmortensen@google.com>
-rw-r--r--gitc_utils.py50
1 files changed, 29 insertions, 21 deletions
diff --git a/gitc_utils.py b/gitc_utils.py
index 89cfbeca..a2786c9f 100644
--- a/gitc_utils.py
+++ b/gitc_utils.py
@@ -13,6 +13,7 @@
13# limitations under the License. 13# limitations under the License.
14 14
15import os 15import os
16import multiprocessing
16import platform 17import platform
17import re 18import re
18import sys 19import sys
@@ -35,6 +36,15 @@ def parse_clientdir(gitc_fs_path):
35 return wrapper.Wrapper().gitc_parse_clientdir(gitc_fs_path) 36 return wrapper.Wrapper().gitc_parse_clientdir(gitc_fs_path)
36 37
37 38
39def _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
38def _set_project_revisions(projects): 48def _set_project_revisions(projects):
39 """Sets the revisionExpr for a list of projects. 49 """Sets the revisionExpr for a list of projects.
40 50
@@ -47,22 +57,24 @@ def _set_project_revisions(projects):
47 """ 57 """
48 # 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
49 # revisionExpr and it is not already a commit id. 59 # revisionExpr and it is not already a commit id.
50 project_gitcmds = [( 60 with multiprocessing.Pool(NUM_BATCH_RETRIEVE_REVISIONID) as pool:
51 project, git_command.GitCommand(None, 61 results_iter = pool.imap_unordered(
52 ['ls-remote', 62 _get_project_revision,
53 project.remote.url, 63 ((i, project.remote.url, project.revisionExpr)
54 project.revisionExpr], 64 for i, project in enumerate(projects)
55 capture_stdout=True, cwd='/tmp')) 65 if not git_config.IsId(project.revisionExpr)),
56 for project in projects if not git_config.IsId(project.revisionExpr)] 66 chunksize=8)
57 for proj, gitcmd in project_gitcmds: 67 for (i, rc, revisionExpr) in results_iter:
58 if gitcmd.Wait(): 68 project = projects[i]
59 print('FATAL: Failed to retrieve revisionExpr for %s' % proj) 69 if rc:
60 sys.exit(1) 70 print('FATAL: Failed to retrieve revisionExpr for %s' % project.name)
61 revisionExpr = gitcmd.stdout.split('\t')[0] 71 pool.terminate()
62 if not revisionExpr: 72 sys.exit(1)
63 raise ManifestParseError('Invalid SHA-1 revision project %s (%s)' % 73 if not revisionExpr:
64 (proj.remote.url, proj.revisionExpr)) 74 pool.terminate()
65 proj.revisionExpr = revisionExpr 75 raise ManifestParseError('Invalid SHA-1 revision project %s (%s)' %
76 (project.remote.url, project.revisionExpr))
77 project.revisionExpr = revisionExpr
66 78
67 79
68def _manifest_groups(manifest): 80def _manifest_groups(manifest):
@@ -123,11 +135,7 @@ def generate_gitc_manifest(gitc_manifest, manifest, paths=None):
123 else: 135 else:
124 proj.revisionExpr = gitc_proj.revisionExpr 136 proj.revisionExpr = gitc_proj.revisionExpr
125 137
126 index = 0 138 _set_project_revisions(projects)
127 while index < len(projects):
128 _set_project_revisions(
129 projects[index:(index + NUM_BATCH_RETRIEVE_REVISIONID)])
130 index += NUM_BATCH_RETRIEVE_REVISIONID
131 139
132 if gitc_manifest is not None: 140 if gitc_manifest is not None:
133 for path, proj in gitc_manifest.paths.items(): 141 for path, proj in gitc_manifest.paths.items():