blob: 8df3d124bace07e13f87faa8d3698f0aa359d972 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001# SPDX-License-Identifier: GPL-2.0+
Doug Anderson21a19d72012-12-03 14:43:16 +00002# Copyright (c) 2012 The Chromium OS Authors.
Maxim Cournoyer8c042fb2022-12-20 00:28:46 -05003# Copyright (c) 2022 Maxim Cournoyer <maxim.cournoyer@savoirfairelinux.com>
Doug Anderson21a19d72012-12-03 14:43:16 +00004#
Doug Anderson21a19d72012-12-03 14:43:16 +00005
Doug Anderson21a19d72012-12-03 14:43:16 +00006import os
Maxim Cournoyer8c042fb2022-12-20 00:28:46 -05007import shlex
8import shutil
Doug Anderson21a19d72012-12-03 14:43:16 +00009
Maxim Cournoyer8c042fb2022-12-20 00:28:46 -050010from patman import gitutil
Simon Glass4583c002023-02-23 18:18:04 -070011from u_boot_pylib import command
Simon Glassbf776672020-04-17 18:09:04 -060012
Doug Anderson21a19d72012-12-03 14:43:16 +000013
Maxim Cournoyer8c042fb2022-12-20 00:28:46 -050014def find_get_maintainer(script_file_name):
15 """Try to find where `script_file_name` is.
Simon Glass156e6552020-06-07 06:45:48 -060016
Maxim Cournoyer8c042fb2022-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 Anderson21a19d72012-12-03 14:43:16 +000019 """
Maxim Cournoyer8c042fb2022-12-20 00:28:46 -050020 get_maintainer = shutil.which(script_file_name)
21 if get_maintainer:
22 return get_maintainer
Doug Anderson21a19d72012-12-03 14:43:16 +000023
Maxim Cournoyer8c042fb2022-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 Anderson21a19d72012-12-03 14:43:16 +000028
Doug Anderson21a19d72012-12-03 14:43:16 +000029
Maxim Cournoyer8c042fb2022-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 Anderson21a19d72012-12-03 14:43:16 +000038
39 Args:
Maxim Cournoyer8c042fb2022-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 Anderson21a19d72012-12-03 14:43:16 +000043
44 Returns:
45 A list of email addresses to CC to.
46 """
Maxim Cournoyer8c042fb2022-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 Anderson21a19d72012-12-03 14:43:16 +000054 if not get_maintainer:
55 if verbose:
Paul Burtona920a172016-09-27 16:03:50 +010056 print("WARNING: Couldn't find get_maintainer.pl")
Doug Anderson21a19d72012-12-03 14:43:16 +000057 return []
58
Maxim Cournoyer8c042fb2022-12-20 00:28:46 -050059 stdout = command.output(get_maintainer, *arguments, fname)
Stefan BrĂ¼nsd1ccaa42017-12-31 23:21:17 +010060 lines = stdout.splitlines()
Maxim Cournoyer8c042fb2022-12-20 00:28:46 -050061 return [x.replace('"', '') for x in lines]