1#!/usr/bin/env python3
2
3import argparse
4import getdeveloperlib
5import sys
6
7
8def parse_args():
9    parser = argparse.ArgumentParser()
10    parser.add_argument('patches', metavar='P', type=argparse.FileType('r'), nargs='*',
11                        help='list of patches (use - to read patches from stdin)')
12    parser.add_argument('-a', dest='architecture', action='store',
13                        help='find developers in charge of this architecture')
14    parser.add_argument('-p', dest='package', action='store',
15                        help='find developers in charge of this package')
16    parser.add_argument('-f', dest='files', nargs='*',
17                        help='find developers in charge of these files')
18    parser.add_argument('-c', dest='check', action='store_const',
19                        const=True, help='list files not handled by any developer')
20    parser.add_argument('-e', dest='email', action='store_const',
21                        const=True, help='only list affected developer email addresses')
22    parser.add_argument('-v', dest='validate', action='store_const',
23                        const=True, help='validate syntax of DEVELOPERS file')
24    parser.add_argument('-d', dest='filename', action='store', default=None,
25                        help='override the default DEVELOPERS file (for debug)')
26    return parser.parse_args()
27
28
29def __main__():
30    args = parse_args()
31
32    # Check that only one action is given
33    action = 0
34    if args.architecture is not None:
35        action += 1
36    if args.package is not None:
37        action += 1
38    if args.files:
39        action += 1
40    if args.check:
41        action += 1
42    if args.validate:
43        action += 1
44    if len(args.patches) != 0:
45        action += 1
46    if action > 1:
47        print("Cannot do more than one action")
48        return
49    if action == 0:
50        print("No action specified")
51        return
52
53    devs = getdeveloperlib.parse_developers(args.filename)
54    if devs is None:
55        sys.exit(1)
56
57    # Validation is done by parse_developers() above and we error out
58    # if the validation didn't work, so if we reach here, it means
59    # validation passed, so we can simply bail out in success.
60    if args.validate:
61        return
62
63    # Handle the check action
64    if args.check:
65        files = getdeveloperlib.check_developers(devs)
66        for f in files:
67            print(f)
68
69    # Handle the architecture action
70    if args.architecture is not None:
71        for dev in devs:
72            if args.architecture in dev.architectures:
73                print(dev.name)
74        return
75
76    # Handle the package action
77    if args.package is not None:
78        for dev in devs:
79            if args.package in dev.packages:
80                print(dev.name)
81        return
82
83    # Handle the files action
84    if args.files is not None:
85        for dev in devs:
86            for f in args.files:
87                if dev.hasfile(f):
88                    print(dev.name)
89                    break
90
91    # Handle the patches action
92    if len(args.patches) != 0:
93        (files, infras) = getdeveloperlib.analyze_patches(args.patches)
94        matching_devs = set()
95        for dev in devs:
96            # See if we have developers matching by package name
97            for f in files:
98                if dev.hasfile(f):
99                    matching_devs.add(dev.name)
100            # See if we have developers matching by package infra
101            for i in infras:
102                if i in dev.infras:
103                    matching_devs.add(dev.name)
104
105        if args.email:
106            for dev in matching_devs:
107                print(dev)
108        else:
109            result = "--to buildroot@buildroot.org"
110            for dev in matching_devs:
111                result += " --cc \"%s\"" % dev
112
113            if result != "":
114                print("git send-email %s" % result)
115
116
117__main__()
118