forked from atseanpaul/review-o-matic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpatchwork.py
147 lines (117 loc) · 3.6 KB
/
patchwork.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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import collections
import pathlib
import json
import re
import requests
import sys
import urllib
class PatchworkInlineComment(object):
def __init__(self):
self.context = collections.deque(maxlen=3)
self.comment = []
self.filename = None
self.line = None
def add_context(self, line):
self.context.append(line)
def has_context(self):
return bool(self.context)
def add_comment(self, line):
self.comment.append(line)
def has_comments(self):
return bool(self.comment)
def set_filename(self, filename):
self.filename = filename
def has_filename(self):
return bool(self.filename)
def set_line(self, line):
self.line = line
def has_line(self):
return self.line != None
def __str__(self):
ret = 'CONTEXT:\n--\n'
ret += '\n'.join(self.context)
ret += '\n'
ret += 'COMMENT:\n--\n'
ret += '\n'.join(self.comment)
ret += '\n'
return ret
def __repr__(self):
return self.__str__()
class PatchworkComment(object):
def __init__(self, rest):
self.id = rest['id']
self.url = rest['web_url']
self.name = rest['submitter']['name']
self.email = rest['submitter']['email']
self.inline_comments = []
self.__parse_comment(rest['content'])
def __parse_comment(self, content):
pat = re.compile('>[\s>]*(.*)')
lines = content.split('\n')
cur = PatchworkInlineComment()
for l in lines:
m = re.match(pat, l)
# skip empty lines
if not l or (m and not m.group(1)):
continue
# Found the end of a comment, save it and start cur over
if m and cur.has_comments():
# Only save comments with context, throw away top-posts
if cur.has_context():
self.inline_comments.append(cur)
cur = PatchworkInlineComment()
if m:
cur.add_context(m.group(1))
elif l.strip():
cur.add_comment(l)
if cur.has_context() and cur.has_comments():
self.inline_comments.append(cur)
def __str__(self):
ret = 'Comment:\n'
ret += ' ID: {}\n'.format(self.id)
ret += ' Author: {} <{}>\n'.format(self.name, self.email)
ret += ' Inline Comments:\n'
for c in self.inline_comments:
ret += str(c)
ret += '\n'
ret += '---\n'
return ret
def __repr__(self):
return self.__str__()
class PatchworkPatch(object):
# Whitelisted patchwork hosts
PATCHWORK_WHITELIST = [
'lore.kernel.org',
'patchwork.freedesktop.org',
'patchwork.kernel.org',
'patchwork.linuxtv.org',
'patchwork.ozlabs.org'
]
def __init__(self, url):
parsed = urllib.parse.urlparse(url)
m = re.match('/([a-z/]*)/([0-9]*)/?', parsed.path)
if not m or not m.group(2):
sys.stderr.write('ERROR: Malformed patchwork URL "%s"\n' % url)
raise ValueError('Invalid url')
if parsed.netloc not in self.PATCHWORK_WHITELIST:
sys.stderr.write('ERROR: Patchwork host not whitelisted "%s"\n' % url)
raise ValueError('Invalid host')
self.url = parsed
self.id = int(m.group(2))
self.patch = None
self.comments = []
def get_patch(self):
if not self.patch:
raw_path = str(pathlib.PurePath(self.url.path, 'raw'))
raw_url = self.url._replace(path=raw_path)
self.patch = requests.get(raw_url.geturl()).text
return self.patch
def get_comments(self):
if not self.comments:
comments_path = '/api/patches/{}/comments/'.format(self.id)
comments_url = self.url._replace(path=comments_path)
rest = requests.get(comments_url.geturl()).json()
for c in rest:
comment = PatchworkComment(c)
self.comments.append(comment)
return self.comments