-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathpatchwork.py
198 lines (161 loc) · 5.04 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import collections
import html
import pathlib
import json
import logging
import re
import requests
import sys
import urllib
logger = logging.getLogger('rom.patchwork')
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) and self.filename != '/dev/null'
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 PatchworkSeries(object):
def __init__(self, url):
self.url = url
def get_patch_subjects(self):
patch = requests.get(self.url.geturl()).text.replace('\n','')
pattern = '<a'
pattern += '\s+'
pattern += 'href='
pattern += '"/patch/([0-9]+)/.*?"'
pattern += '\s*>\s*'
pattern += '\[\S*\] (.*?)'
pattern += '</a>'
regex = re.compile(pattern, flags=(re.I | re.MULTILINE | re.DOTALL))
m = regex.findall(patch)
if not m or not len(m):
return None
ret = []
for s in m:
ret.append(html.unescape(s[1]))
return ret
class PatchworkPatch(object):
def __init__(self, allowlist, url):
self.allowlist = allowlist
self.parse_url(url)
# Handle redirects and update the url member with the result. This allows
# for better handling of msgid-based urls
resp = requests.get(self.url.geturl())
resp.raise_for_status()
if resp.history:
self.parse_url(resp.url)
def parse_url(self, url):
parsed = urllib.parse.urlparse(url.strip())
m = re.match('(.*?)/patch/([^/]*)/?', parsed.path)
if not m or not m.group(2):
logger.error('Malformed patchwork URL "{}"'.format(url))
raise ValueError('Invalid url')
found = False
for i in self.allowlist:
if parsed.netloc == i.host:
self.path_prefix = i.path
self.comments_supported = i.has_comments
found = True
break
if not found:
logger.error('Patchwork host not allowed "{}"'.format(url))
raise ValueError('Invalid host')
self.url = parsed
self.id = m.group(2)
self.patch = None
self.comments = []
def get_series(self):
patch = requests.get(self.url.geturl()).text
m = re.findall('a href="/series/([0-9]+)/"', patch)
if not m or not len(m):
return None
return PatchworkSeries(self.url._replace(path='/series/{}/'.format(m[0])))
def get_patch(self):
if not self.patch:
raw_path = pathlib.PurePath(self.url.path, 'raw')
raw_url = self.url._replace(path=str(raw_path))
resp = requests.get(raw_url.geturl())
resp.raise_for_status()
self.patch = resp.text
return self.patch
def get_comments(self):
if self.comments or not self.comments_supported:
return self.comments
comments_path = pathlib.PurePath(self.path_prefix,
'api/patches/{}/comments/'.format(self.id))
comments_url = self.url._replace(path=str(comments_path))
resp = requests.get(comments_url.geturl())
if resp.status_code != 200:
return None
rest = resp.json()
for c in rest:
comment = PatchworkComment(c)
self.comments.append(comment)
return self.comments