forked from martinthomson/i-d-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup-codeowners.py
executable file
·59 lines (49 loc) · 1.61 KB
/
setup-codeowners.py
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
#!/usr/bin/env python3
import os
import subprocess
import sys
import xml.sax
class GetAuthorsEmail(xml.sax.handler.ContentHandler):
states = ["", "front", "author", "email"]
def __init__(self):
self.state = 0
self.email = ""
def startElement(self, tag, attributes):
if (
self.state + 1 < len(GetAuthorsEmail.states)
and GetAuthorsEmail.states[self.state + 1] == tag.lower()
):
self.state = self.state + 1
def characters(self, content):
if self.state + 1 == len(GetAuthorsEmail.states):
self.email = self.email + content
def endElement(self, tag):
if tag.lower() != GetAuthorsEmail.states[self.state]:
return
if self.state + 1 == len(GetAuthorsEmail.states):
print(f" {self.email.strip()}", end="")
self.email = ""
self.state = self.state - 1
@staticmethod
def get_emails(f):
parser = xml.sax.make_parser()
parser.setContentHandler(GetAuthorsEmail())
parser.parse(f)
print()
print("# Automatically generated CODEOWNERS")
print("# Regenerate with `make update-codeowners`")
if len(sys.argv) >= 2:
sink = open(os.devnull, "wb")
for f in sys.argv[1:]:
cmd = f"git ls-tree --name-only @ {f.rpartition('.')[0]}.* | head -1"
s = subprocess.check_output(
cmd,
shell=True,
universal_newlines=True,
encoding="utf-8",
stderr=sink,
)
print(s.strip(), end="")
GetAuthorsEmail.get_emails(f)
else:
GetAuthorsEmail.get_emails(sys.stdin)