1#!/usr/bin/env python3
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
19import multiprocessing
20import signal
21import sys
22
23# pylint:disable=relative-beyond-top-level,no-name-in-module
24# suppress false positive of no-name-in-module warnings
25from . import warn_common as common
26
27
28def 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
52def create_and_launch_subprocesses(num_cpu, classify_warnings_fn, arg_groups,
53                                   group_results):
54  """Fork num_cpu processes to classify warnings."""
55  pool = multiprocessing.Pool(num_cpu)
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
61
62
63def main():
64  """Old main() calls new common_main."""
65  use_google3 = False
66  common.common_main(use_google3, create_and_launch_subprocesses,
67                     classify_warnings)
68
69
70if __name__ == '__main__':
71  main()
72