Chih-Hung Hsieh | 7cc0e15 | 2021-04-26 17:09:36 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Chih-Hung Hsieh | 888d143 | 2019-12-09 19:32:03 -0800 | [diff] [blame] | 2 | # |
| 3 | # Copyright (C) 2019 The Android Open Source Project |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | |
| 17 | """Simple wrapper to run warn_common with Python standard Pool.""" |
| 18 | |
| 19 | import multiprocessing |
Chih-Hung Hsieh | 5ae5519 | 2020-02-24 10:20:36 -0800 | [diff] [blame] | 20 | import signal |
| 21 | import sys |
Chih-Hung Hsieh | 888d143 | 2019-12-09 19:32:03 -0800 | [diff] [blame] | 22 | |
Chih-Hung Hsieh | 98b285d | 2021-04-28 14:49:32 -0700 | [diff] [blame] | 23 | # pylint:disable=relative-beyond-top-level,no-name-in-module |
| 24 | # suppress false positive of no-name-in-module warnings |
Chih-Hung Hsieh | 5ae5519 | 2020-02-24 10:20:36 -0800 | [diff] [blame] | 25 | from . import warn_common as common |
Chih-Hung Hsieh | 888d143 | 2019-12-09 19:32:03 -0800 | [diff] [blame] | 26 | |
| 27 | |
Chih-Hung Hsieh | 5ae5519 | 2020-02-24 10:20:36 -0800 | [diff] [blame] | 28 | def classify_warnings(args): |
| 29 | """Classify a list of warning lines. |
| 30 | |
| 31 | Args: |
| 32 | args: dictionary { |
| 33 | 'group': list of (warning, link), |
| 34 | 'project_patterns': re.compile(project_list[p][1]), |
| 35 | 'warn_patterns': list of warn_pattern, |
| 36 | 'num_processes': number of processes being used for multiprocessing } |
| 37 | Returns: |
| 38 | results: a list of the classified warnings. |
| 39 | """ |
| 40 | results = [] |
| 41 | for line, link in args['group']: |
| 42 | common.classify_one_warning(line, link, results, args['project_patterns'], |
| 43 | args['warn_patterns']) |
| 44 | |
| 45 | # After the main work, ignore all other signals to a child process, |
| 46 | # to avoid bad warning/error messages from the exit clean-up process. |
| 47 | if args['num_processes'] > 1: |
| 48 | signal.signal(signal.SIGTERM, lambda *args: sys.exit(-signal.SIGTERM)) |
| 49 | return results |
| 50 | |
| 51 | |
| 52 | def create_and_launch_subprocesses(num_cpu, classify_warnings_fn, arg_groups, |
| 53 | group_results): |
Chih-Hung Hsieh | 98b285d | 2021-04-28 14:49:32 -0700 | [diff] [blame] | 54 | """Fork num_cpu processes to classify warnings.""" |
Chih-Hung Hsieh | 888d143 | 2019-12-09 19:32:03 -0800 | [diff] [blame] | 55 | pool = multiprocessing.Pool(num_cpu) |
Chih-Hung Hsieh | 5ae5519 | 2020-02-24 10:20:36 -0800 | [diff] [blame] | 56 | for cpu in range(num_cpu): |
| 57 | proc_result = pool.map(classify_warnings_fn, arg_groups[cpu]) |
| 58 | if proc_result is not None: |
| 59 | group_results.append(proc_result) |
| 60 | return group_results |
Chih-Hung Hsieh | 888d143 | 2019-12-09 19:32:03 -0800 | [diff] [blame] | 61 | |
| 62 | |
| 63 | def main(): |
Chih-Hung Hsieh | 98b285d | 2021-04-28 14:49:32 -0700 | [diff] [blame] | 64 | """Old main() calls new common_main.""" |
Chih-Hung Hsieh | 5ae5519 | 2020-02-24 10:20:36 -0800 | [diff] [blame] | 65 | use_google3 = False |
| 66 | common.common_main(use_google3, create_and_launch_subprocesses, |
| 67 | classify_warnings) |
Chih-Hung Hsieh | 888d143 | 2019-12-09 19:32:03 -0800 | [diff] [blame] | 68 | |
| 69 | |
| 70 | if __name__ == '__main__': |
| 71 | main() |