blob: f7011be1e4969bfae326a4461c8268bc4389ce61 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001# SPDX-License-Identifier: GPL-2.0+
Doug Andersonc2c84bc2012-12-03 14:43:16 +00002# Copyright (c) 2012 The Chromium OS Authors.
Maxim Cournoyer3ef23e92022-12-20 00:28:46 -05003# Copyright (c) 2022 Maxim Cournoyer <maxim.cournoyer@savoirfairelinux.com>
Doug Andersonc2c84bc2012-12-03 14:43:16 +00004#
Doug Andersonc2c84bc2012-12-03 14:43:16 +00005
Doug Andersonc2c84bc2012-12-03 14:43:16 +00006import os
Maxim Cournoyer3ef23e92022-12-20 00:28:46 -05007import shlex
8import shutil
Doug Andersonc2c84bc2012-12-03 14:43:16 +00009
Simon Glassa997ea52020-04-17 18:09:04 -060010from patman import command
Maxim Cournoyer3ef23e92022-12-20 00:28:46 -050011from patman import gitutil
Simon Glassa997ea52020-04-17 18:09:04 -060012
Doug Andersonc2c84bc2012-12-03 14:43:16 +000013
Maxim Cournoyer3ef23e92022-12-20 00:28:46 -050014def find_get_maintainer(script_file_name):
15 """Try to find where `script_file_name` is.
Simon Glassc9fa8862020-06-07 06:45:48 -060016
Maxim Cournoyer3ef23e92022-12-20 00:28:46 -050017 It searches in PATH and falls back to a path relative to the top
18 of the current git repository.
Doug Andersonc2c84bc2012-12-03 14:43:16 +000019 """
Maxim Cournoyer3ef23e92022-12-20 00:28:46 -050020 get_maintainer = shutil.which(script_file_name)
21 if get_maintainer:
22 return get_maintainer
Doug Andersonc2c84bc2012-12-03 14:43:16 +000023
Maxim Cournoyer3ef23e92022-12-20 00:28:46 -050024 git_relative_script = os.path.join(gitutil.get_top_level(),
25 script_file_name)
26 if os.path.exists(git_relative_script):
27 return git_relative_script
Doug Andersonc2c84bc2012-12-03 14:43:16 +000028
Doug Andersonc2c84bc2012-12-03 14:43:16 +000029
Maxim Cournoyer3ef23e92022-12-20 00:28:46 -050030def get_maintainer(script_file_name, fname, verbose=False):
31 """Run `script_file_name` on a file.
32
33 `script_file_name` should be a get_maintainer.pl-like script that
34 takes a patch file name as an input and return the email addresses
35 of the associated maintainers to standard output, one per line.
36
37 If `script_file_name` does not exist we fail silently.
Doug Andersonc2c84bc2012-12-03 14:43:16 +000038
39 Args:
Maxim Cournoyer3ef23e92022-12-20 00:28:46 -050040 script_file_name: The file name of the get_maintainer.pl script
41 (or compatible).
42 fname: File name of the patch to process with get_maintainer.pl.
Doug Andersonc2c84bc2012-12-03 14:43:16 +000043
44 Returns:
45 A list of email addresses to CC to.
46 """
Maxim Cournoyer3ef23e92022-12-20 00:28:46 -050047 # Expand `script_file_name` into a file name and its arguments, if
48 # any.
49 cmd_args = shlex.split(script_file_name)
50 file_name = cmd_args[0]
51 arguments = cmd_args[1:]
52
53 get_maintainer = find_get_maintainer(file_name)
Doug Andersonc2c84bc2012-12-03 14:43:16 +000054 if not get_maintainer:
55 if verbose:
Paul Burtonc3931342016-09-27 16:03:50 +010056 print("WARNING: Couldn't find get_maintainer.pl")
Doug Andersonc2c84bc2012-12-03 14:43:16 +000057 return []
58
Maxim Cournoyer3ef23e92022-12-20 00:28:46 -050059 stdout = command.output(get_maintainer, *arguments, fname)
Stefan BrĂ¼ns5d3a5fa2017-12-31 23:21:17 +010060 lines = stdout.splitlines()
Maxim Cournoyer3ef23e92022-12-20 00:28:46 -050061 return [x.replace('"', '') for x in lines]