1#!/usr/bin/python
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
24from . import warn_common as common
25
26
27def classify_warnings(args):
28  """Classify a list of warning lines.
29
30  Args:
31    args: dictionary {
32        'group': list of (warning, link),
33        'project_patterns': re.compile(project_list[p][1]),
34        'warn_patterns': list of warn_pattern,
35        'num_processes': number of processes being used for multiprocessing }
36  Returns:
37    results: a list of the classified warnings.
38  """
39  results = []
40  for line, link in args['group']:
41    common.classify_one_warning(line, link, results, args['project_patterns'],
42                                args['warn_patterns'])
43
44  # After the main work, ignore all other signals to a child process,
45  # to avoid bad warning/error messages from the exit clean-up process.
46  if args['num_processes'] > 1:
47    signal.signal(signal.SIGTERM, lambda *args: sys.exit(-signal.SIGTERM))
48  return results
49
50
51def create_and_launch_subprocesses(num_cpu, classify_warnings_fn, arg_groups,
52                                   group_results):
53  pool = multiprocessing.Pool(num_cpu)
54  for cpu in range(num_cpu):
55    proc_result = pool.map(classify_warnings_fn, arg_groups[cpu])
56    if proc_result is not None:
57      group_results.append(proc_result)
58  return group_results
59
60
61def main():
62  use_google3 = False
63  common.common_main(use_google3, create_and_launch_subprocesses,
64                     classify_warnings)
65
66
67if __name__ == '__main__':
68  main()
69