diff options
| author | Mike Frysinger <vapier@google.com> | 2021-03-01 00:56:38 -0500 |
|---|---|---|
| committer | Mike Frysinger <vapier@google.com> | 2021-04-15 05:10:16 +0000 |
| commit | b5d075d04f1e555f85aad27e74f16073a50b2ae6 (patch) | |
| tree | b7342a0cd0a8d081cceb801b615bf8bbe1cc5647 /subcmds/abandon.py | |
| parent | b8bf291ddbe00731d441a34cbf1ec5b5f95f401b (diff) | |
| download | git-repo-b5d075d04f1e555f85aad27e74f16073a50b2ae6.tar.gz | |
command: add a helper for the parallel execution boilerplate
Now that we have a bunch of subcommands doing parallel execution, a
common pattern arises that we can factor out for most of them. We
leave forall alone as it's a bit too complicated atm to cut over.
Change-Id: I3617a4f7c66142bcd1ab030cb4cca698a65010ac
Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/301942
Tested-by: Mike Frysinger <vapier@google.com>
Reviewed-by: Chris Mcdonald <cjmcdonald@google.com>
Diffstat (limited to 'subcmds/abandon.py')
| -rw-r--r-- | subcmds/abandon.py | 26 |
1 files changed, 10 insertions, 16 deletions
diff --git a/subcmds/abandon.py b/subcmds/abandon.py index 1d22917e..c7c127d6 100644 --- a/subcmds/abandon.py +++ b/subcmds/abandon.py | |||
| @@ -15,10 +15,9 @@ | |||
| 15 | from collections import defaultdict | 15 | from collections import defaultdict |
| 16 | import functools | 16 | import functools |
| 17 | import itertools | 17 | import itertools |
| 18 | import multiprocessing | ||
| 19 | import sys | 18 | import sys |
| 20 | 19 | ||
| 21 | from command import Command, DEFAULT_LOCAL_JOBS, WORKER_BATCH_SIZE | 20 | from command import Command, DEFAULT_LOCAL_JOBS |
| 22 | from git_command import git | 21 | from git_command import git |
| 23 | from progress import Progress | 22 | from progress import Progress |
| 24 | 23 | ||
| @@ -52,9 +51,9 @@ It is equivalent to "git branch -D <branchname>". | |||
| 52 | else: | 51 | else: |
| 53 | args.insert(0, "'All local branches'") | 52 | args.insert(0, "'All local branches'") |
| 54 | 53 | ||
| 55 | def _ExecuteOne(self, opt, nb, project): | 54 | def _ExecuteOne(self, all_branches, nb, project): |
| 56 | """Abandon one project.""" | 55 | """Abandon one project.""" |
| 57 | if opt.all: | 56 | if all_branches: |
| 58 | branches = project.GetBranches() | 57 | branches = project.GetBranches() |
| 59 | else: | 58 | else: |
| 60 | branches = [nb] | 59 | branches = [nb] |
| @@ -72,7 +71,7 @@ It is equivalent to "git branch -D <branchname>". | |||
| 72 | success = defaultdict(list) | 71 | success = defaultdict(list) |
| 73 | all_projects = self.GetProjects(args[1:]) | 72 | all_projects = self.GetProjects(args[1:]) |
| 74 | 73 | ||
| 75 | def _ProcessResults(states): | 74 | def _ProcessResults(_pool, pm, states): |
| 76 | for (results, project) in states: | 75 | for (results, project) in states: |
| 77 | for branch, status in results.items(): | 76 | for branch, status in results.items(): |
| 78 | if status: | 77 | if status: |
| @@ -81,17 +80,12 @@ It is equivalent to "git branch -D <branchname>". | |||
| 81 | err[branch].append(project) | 80 | err[branch].append(project) |
| 82 | pm.update() | 81 | pm.update() |
| 83 | 82 | ||
| 84 | pm = Progress('Abandon %s' % nb, len(all_projects), quiet=opt.quiet) | 83 | self.ExecuteInParallel( |
| 85 | # NB: Multiprocessing is heavy, so don't spin it up for one job. | 84 | opt.jobs, |
| 86 | if len(all_projects) == 1 or opt.jobs == 1: | 85 | functools.partial(self._ExecuteOne, opt.all, nb), |
| 87 | _ProcessResults(self._ExecuteOne(opt, nb, x) for x in all_projects) | 86 | all_projects, |
| 88 | else: | 87 | callback=_ProcessResults, |
| 89 | with multiprocessing.Pool(opt.jobs) as pool: | 88 | output=Progress('Abandon %s' % (nb,), len(all_projects), quiet=opt.quiet)) |
| 90 | states = pool.imap_unordered( | ||
| 91 | functools.partial(self._ExecuteOne, opt, nb), all_projects, | ||
| 92 | chunksize=WORKER_BATCH_SIZE) | ||
| 93 | _ProcessResults(states) | ||
| 94 | pm.end() | ||
| 95 | 89 | ||
| 96 | width = max(itertools.chain( | 90 | width = max(itertools.chain( |
| 97 | [25], (len(x) for x in itertools.chain(success, err)))) | 91 | [25], (len(x) for x in itertools.chain(success, err)))) |
