From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755633Ab3CIGjX (ORCPT ); Sat, 9 Mar 2013 01:39:23 -0500 Received: from mail-gg0-f172.google.com ([209.85.161.172]:52872 "EHLO mail-gg0-f172.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752601Ab3CIGjW (ORCPT ); Sat, 9 Mar 2013 01:39:22 -0500 From: "Raphael S. Carvalho" Cc: linux-kernel@vger.kernel.org, "Raphael S. Carvalho" Subject: [PATCH 1/1] scripts/gen_sendmail.py: Script Added: Generate git send-email arguments automatically! Date: Sat, 9 Mar 2013 03:13:06 -0300 Message-Id: <1362809586-18477-1-git-send-email-raphael.scarv@gmail.com> X-Mailer: git-send-email 1.7.2.5 To: unlisted-recipients:; (no To-header on input) Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org I guess this script won't be merged upstream, but I think it could be useful for someone else. Description borrowed from the header: --------------- !# This script generates the git send-email automatically !# by looking at the output generated by scripts/get_maintainer.pl !# !# You can pass as many patch files as needed. !# Usage: python send-mail.py [option] ... Example of use: Passed as arguments one patch file and -v (verbose mode). --------------- raphaelsc@debian:~/kernel/linux$ scripts/gen_sendmail.py -v 0001-kernel-pid.c-Improve-flow-of-a-loop-inside-alloc_pid.patch git send-email --from "Raphael S. Carvalho " --to "Eric W. Biederman " --to "Andrew Morton " --to "Serge E. Hallyn " --to "Serge Hallyn " --to "David S. Miller " --cc "linux-kernel@vger.kernel.org" 0001-kernel-pid.c-Improve-flow-of-a-loop-inside-alloc_pid.patch * Statistics: Maintainer(s): 5, List(s): 1 --------------- Any bug reports or improvements are welcome! Signed-off-by: Raphael S. Carvalho --- scripts/gen_sendmail.py | 165 +++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 165 insertions(+), 0 deletions(-) create mode 100755 scripts/gen_sendmail.py diff --git a/scripts/gen_sendmail.py b/scripts/gen_sendmail.py new file mode 100755 index 0000000..921a8cc --- /dev/null +++ b/scripts/gen_sendmail.py @@ -0,0 +1,165 @@ +#! /usr/bin/env python +# +# Generate git send-email arguments (gen_sendmail.py) +# (c) 2013, Raphael S.Carvalho +# +# This script generates the git send-email automatically +# by looking at the output generated by scripts/get_maintainer.pl +# +# You can pass as many patch files as needed. +# Usage: python send-mail.py [options] ... +# +# Licensed under the terms of the GNU GPL License version 2 + +import commands +import sys +import StringIO +import getopt + +# Default definitions +GIT_SENDMAIL = "git send-email" +SCRIPT = "scripts/get_maintainer.pl" + + +def get_user_gitconfig(git_config): + get_name = "git config user.name" + get_email = "git config user.email" + + (stat, name) = commands.getstatusoutput(get_name) + if (stat != 0): + return (stat, get_name) + + (stat, email) = commands.getstatusoutput(get_email) + if (stat != 0): + return (stat, get_email) + + # Setup git config structure! + git_config['user_name'] = name + git_config['user_email'] = email + + return (0, None) + + +# Try to execute: get_maintainer.pl +def exec_get_maintainers(patch_name): + command = SCRIPT + ' ' + patch_name + (stat, output) = commands.getstatusoutput(command) + return (stat, output) + + +def find_maintainer(maintainers, email): + for m in maintainers: + if m['email'] == email: + return True + return False + + +# Get file and/or mail from each line, +# and build a simple maintainers database. +def build_list(buf): + maintainers = [] + + for line in iter(buf.readline, ""): + name = "" + email = "" + + pos = line.find("<") + if pos != -1: + name = line[: pos-1].replace('"', "") + pos2 = line.find("(") + email = line[pos : pos2-1] + else: + pos = line.find("(") + email = line[: pos-1] + + # If not find_maintainer, then add to the list. + if not find_maintainer(maintainers, email): + maintainer = {'name': name, 'email': email} + maintainers.append(maintainer) + + return maintainers + + +# Generates command from the built database. +def generate_gitmail_cmd(maintainers, git_config, args): + mnt_count = list_count = 0 + + print '%s --from "%s <%s>"' % \ + (GIT_SENDMAIL, \ + git_config['user_name'], git_config['user_email']), + + for m in maintainers: + if m['name'] != "": + print '--to "%s %s"' % (m['name'], m['email']), + mnt_count += 1 + else: + print '--cc "%s"' % m['email'], + list_count += 1 + for arg in args: + print arg, + + return (mnt_count, list_count) + + +def usage(program): + print 'Usage: %s [options] \n' \ + '-v --verbose: Print verbose messages.\n' \ + '-h --help: Print this information.\n\n' \ + 'This script must be installed in the directory' \ + ' scripts of linux tree!' % program, + sys.exit(2) + + +def main(argc, argv): + verbose = False + + try: + opts, args = getopt.getopt(argv[1:], 'hv', ['help', 'verbose']) + if not opts and argc < 2: + usage(argv[0]) + except getopt.GetoptError, e: + print e + usage(argv[0]) + + arg_count = 1 # program name: argv[0] + for opt, arg in opts: + arg_count += 1 + if opt in ('-h', '--help'): + usage(argv[0]) + elif opt in ('-v', '--verbose'): + verbose = True + else: + usage(argv[0]) + if (arg_count == argc): + usage(argv[0]) + + git_config = {'user_name': "", 'user_email': ""} + (stat, err_msg) = get_user_gitconfig(git_config) + if (stat != 0): + print "It wasn't possible to execute: %s" % err_msg + sys.exit(2) + + final_output = "" + args = argv[arg_count:] + # Get output regarding to the each patch file! + for arg in args: + (stat, output) = exec_get_maintainers(arg) + if (stat != 0): + print '%s: %s failed! Please, check the patch: %s,' \ + ' and make sure you "installed" this script' \ + ' in the *directory scripts* of Linux tree.' \ + % (argv[0], SCRIPT, arg) + sys.exit(2) + final_output += (output + '\n') + + buf = StringIO.StringIO(final_output) + (mnt_count, list_count) = \ + generate_gitmail_cmd(build_list(buf), git_config, args) + + if (verbose): + print "\n\n* Statistics: Maintainer(s): %d, List(s): %d" \ + % (mnt_count, list_count) + + +if __name__ == "__main__": + main(len(sys.argv), sys.argv) -- 1.7.2.5