The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1 | # Copyright (C) 2008 The Android Open Source Project |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
Mike Frysinger | 69b4a9c | 2021-02-16 18:18:01 -0500 | [diff] [blame] | 15 | import functools |
| 16 | import io |
| 17 | import multiprocessing |
| 18 | |
| 19 | from command import DEFAULT_LOCAL_JOBS, PagedCommand, WORKER_BATCH_SIZE |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 20 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 21 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 22 | class Diff(PagedCommand): |
| 23 | common = True |
| 24 | helpSummary = "Show changes between commit and working tree" |
| 25 | helpUsage = """ |
| 26 | %prog [<project>...] |
pelya | d67872d | 2012-03-28 14:49:58 +0300 | [diff] [blame] | 27 | |
| 28 | The -u option causes '%prog' to generate diff output with file paths |
| 29 | relative to the repository root, so the output can be applied |
| 30 | to the Unix 'patch' command. |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 31 | """ |
Mike Frysinger | 69b4a9c | 2021-02-16 18:18:01 -0500 | [diff] [blame] | 32 | PARALLEL_JOBS = DEFAULT_LOCAL_JOBS |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 33 | |
pelya | d67872d | 2012-03-28 14:49:58 +0300 | [diff] [blame] | 34 | def _Options(self, p): |
pelya | d67872d | 2012-03-28 14:49:58 +0300 | [diff] [blame] | 35 | p.add_option('-u', '--absolute', |
| 36 | dest='absolute', action='store_true', |
| 37 | help='Paths are relative to the repository root') |
| 38 | |
Mike Frysinger | 69b4a9c | 2021-02-16 18:18:01 -0500 | [diff] [blame] | 39 | def _DiffHelper(self, absolute, project): |
| 40 | """Obtains the diff for a specific project. |
| 41 | |
| 42 | Args: |
| 43 | absolute: Paths are relative to the root. |
| 44 | project: Project to get status of. |
| 45 | |
| 46 | Returns: |
| 47 | The status of the project. |
| 48 | """ |
| 49 | buf = io.StringIO() |
| 50 | ret = project.PrintWorkTreeDiff(absolute, output_redir=buf) |
| 51 | return (ret, buf.getvalue()) |
| 52 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 53 | def Execute(self, opt, args): |
Mike Frysinger | 0a9265e | 2019-09-30 23:59:27 -0400 | [diff] [blame] | 54 | ret = 0 |
Mike Frysinger | 69b4a9c | 2021-02-16 18:18:01 -0500 | [diff] [blame] | 55 | all_projects = self.GetProjects(args) |
| 56 | |
| 57 | # NB: Multiprocessing is heavy, so don't spin it up for one job. |
| 58 | if len(all_projects) == 1 or opt.jobs == 1: |
| 59 | for project in all_projects: |
| 60 | if not project.PrintWorkTreeDiff(opt.absolute): |
| 61 | ret = 1 |
| 62 | else: |
| 63 | with multiprocessing.Pool(opt.jobs) as pool: |
| 64 | states = pool.imap(functools.partial(self._DiffHelper, opt.absolute), |
| 65 | all_projects, WORKER_BATCH_SIZE) |
| 66 | for (state, output) in states: |
| 67 | if output: |
| 68 | print(output, end='') |
| 69 | if not state: |
| 70 | ret = 1 |
| 71 | |
Mike Frysinger | 0a9265e | 2019-09-30 23:59:27 -0400 | [diff] [blame] | 72 | return ret |