aboutsummaryrefslogtreecommitdiff
blob: 07596c883b9c2364f77edd40b7d9e21377ada1af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/usr/bin/python
# Copyright 1999-2009 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2

# Written by Robert Buchholz <rbu@gentoo.org>

import sys
import os
import re
try:
    import xml.etree.ElementTree as et
except ImportError:
    import elementtree.ElementTree as et

PORTDIR = "/var/db/repos/gentoo"
HERDS = PORTDIR + "/metadata/herds.xml"
heXML = None

def uniq(seq):
    """ order preserving unique """
    seen = {}
    result = []
    for item in seq:
        if not item in seen:
            seen[item] = 1
            result.append(item)
    return result

def get_pkg_cat(string):
    """ returns a list with packages or categories found that exist in portdir """
    metadatadirs = []
    
    matches = re.findall(r"(?#start:   )(?:^|\s)[<>~=]*(?#\
                              cat:     )([A-Za-z0-9+_][A-Za-z0-9+_.-]*/(?#\
                              pnv:     )[A-Za-z0-9+_][A-Za-z0-9+_.:@-]*)", string)

    for name in matches:
        # remove versions at the end
        name = re.sub(r"(?#version:   )-[0-9.]+[a-z]?(?#\
                           additions: )(_(alpha|beta|pre|rc|p)[0-9]*)?(?#\
                           revisions: )(-r[0-9]*)?(?#\
                           usedeps:   )(\[[!=?A-Za-z0-9+_@-]+\])?(?#\
                           slot deps: )(:[A-Za-z0-9+_.-]*)?$", "", name)

        if os.path.isdir("%s/%s" % (PORTDIR, name)):
            metadatadirs.append(name)
        else:
            (cat, _) = name.split('/', 1)
            if os.path.isdir("%s/%s" % (PORTDIR, cat)):
                metadatadirs.append(cat)

    return metadatadirs

def get_maintainer_for(directory):
    """ returns a priority-sorted list of maintainers for a given CAT or CAT/PN """
    cc = []
    try:
        if not heXML:
            globals()['heXML'] = et.parse(HERDS)
        meXML = et.parse("%s/%s/metadata.xml" % (PORTDIR, directory))

        for elem in meXML.getiterator():
            if elem.tag == "herd":
                for thisherd in heXML.findall("/herd"):
                    if thisherd.findtext("name") == elem.text:
                        herdmail = thisherd.findtext("email")
                        if herdmail:
                            cc.append(herdmail)
            elif elem.tag == "maintainer":
                email = elem.findtext("email")
                if not email:
                    continue
                if elem.get('ignoreauto') == "1" and elem.get('role'):
                    if email in cc:
                        cc.remove(email)
                else:
                    cc.append(email)

    except Exception:
        pass
    return cc

def get_cc_from_string(string):
    """ returns an ordered list of bug assignees / ccs for an arbitrary string such as
        a bug title. the first element of the tuple (if present) is supposed to be the
        assignee of the bug. """
    
    ccs = []
    
    """ replace ':' in "category/packet:", as it will prevent finding the correct maintainer(s) """
    string=string.replace(':','')

    metadatadirs = get_pkg_cat(string)
    for dir in metadatadirs:
        ccs.extend(get_maintainer_for(dir))

    # remove dupes
    ccs = uniq(ccs)
    return ccs

def main():
    if len(sys.argv) < 1:
        return
    arg = " ".join(sys.argv[1:])

    ccs = get_cc_from_string(arg)

    if len(ccs) > 0:
        print "assign-to: %s" % (ccs[0])
        print "cc: %s" % (",".join(ccs[1:]))


if __name__ == "__main__":
    try:
        main()
    except KeyboardInterrupt:
        print '\n ! Exiting.'