-
Notifications
You must be signed in to change notification settings - Fork 177
/
pipermail.py
322 lines (239 loc) · 10.6 KB
/
pipermail.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
# -*- coding: utf-8 -*-
#
# Copyright (C) 2015-2020 Bitergia
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Authors:
# Santiago Dueñas <[email protected]>
# Germán Poo-Caamaño <[email protected]>
# Stephan Barth <[email protected]>
# Valerio Cosentino <[email protected]>
# Jesus M. Gonzalez-Barahona <[email protected]>
# Harshal Mittal <[email protected]>
#
# Note: some of this code was based on parts of the MailingListStats project
import datetime
import logging
import os
import bs4
import dateutil
import requests
from grimoirelab_toolkit.datetime import datetime_to_utc
from grimoirelab_toolkit.uris import urijoin
from .mbox import MBox, MailingList, CATEGORY_MESSAGE
from ...backend import (BackendCommand,
BackendCommandArgumentParser)
from ...utils import DEFAULT_DATETIME
PIPERMAIL_COMPRESSED_TYPES = ['.gz', '.bz2', '.zip',
'.tar', '.tar.gz', '.tar.bz2',
'.tgz', '.tbz']
PIPERMAIL_ACCEPTED_TYPES = ['.mbox', '.txt']
PIPERMAIL_TYPES = PIPERMAIL_COMPRESSED_TYPES + PIPERMAIL_ACCEPTED_TYPES
MOD_MBOX_THREAD_STR = "/thread"
logger = logging.getLogger(__name__)
class Pipermail(MBox):
"""Pipermail backend.
This class allows the fetch the email messages stored on a Pipermail
archiver. Initialize this class passing the URL where the archiver is
and the directory path where the mbox files will be fetched and
stored. The origin of the data will be set to the value of `url`.
:param url: URL to the Pipermail archiver
:param dirpath: directory path where the mboxes are stored
:param tag: label used to mark the data
:param archive: archive to store/retrieve items
:param ssl_verify: enable/disable SSL verification
"""
version = '1.0.0'
CATEGORIES = [CATEGORY_MESSAGE]
def __init__(self, url, dirpath, tag=None, archive=None, ssl_verify=True):
super().__init__(url, dirpath, tag=tag, archive=archive, ssl_verify=ssl_verify)
self.url = url
def fetch(self, category=CATEGORY_MESSAGE, from_date=DEFAULT_DATETIME):
"""Fetch the messages from the Pipermail archiver.
The method fetches the mbox files from a remote Pipermail
archiver and retrieves the messages stored on them.
:param category: the category of items to fetch
:param from_date: obtain messages since this date
:returns: a generator of messages
"""
items = super().fetch(category, from_date)
return items
def fetch_items(self, category, **kwargs):
"""Fetch the messages
:param category: the category of items to fetch
:param kwargs: backend arguments
:returns: a generator of items
"""
from_date = kwargs['from_date']
logger.info("Looking for messages from '%s' since %s",
self.url, str(from_date))
mailing_list = PipermailList(self.url, self.dirpath, self.ssl_verify)
mailing_list.fetch(from_date=from_date)
messages = self._fetch_and_parse_messages(mailing_list, from_date)
for message in messages:
yield message
logger.info("Fetch process completed")
@classmethod
def has_archiving(cls):
"""Returns whether it supports archiving items on the fetch process.
:returns: this backend does not support items archive
"""
return False
@classmethod
def has_resuming(cls):
"""Returns whether it supports to resume the fetch process.
:returns: this backend supports items resuming
"""
return True
class PipermailCommand(BackendCommand):
"""Class to run Pipermail backend from the command line."""
BACKEND = Pipermail
def _pre_init(self):
"""Initialize mailing lists directory path"""
if not self.parsed_args.mboxes_path:
base_path = os.path.expanduser('~/.perceval/mailinglists/')
dirpath = os.path.join(base_path, self.parsed_args.url)
else:
dirpath = self.parsed_args.mboxes_path
setattr(self.parsed_args, 'dirpath', dirpath)
@classmethod
def setup_cmd_parser(cls):
"""Returns the Pipermail argument parser."""
parser = BackendCommandArgumentParser(cls.BACKEND,
from_date=True,
ssl_verify=True)
# Optional arguments
group = parser.parser.add_argument_group('Pipermail arguments')
group.add_argument('--mboxes-path', dest='mboxes_path',
help="Path where mbox files will be stored")
# Required arguments
parser.parser.add_argument('url',
help="URL of the archiver")
return parser
class PipermailList(MailingList):
"""Manage mailing list archives stored by Pipermail archiver.
This class gives access to remote and local mboxes archives
from a mailing list stored by Pipermail. This class also allows
to keep them in sync.
:param url: URL to the Pipermail archiver for this list
:param dirpath: path to the local mboxes archives
:param ssl_verify: enable/disable SSL verification
"""
def __init__(self, url, dirpath, ssl_verify=True):
super().__init__(url, dirpath)
self.url = url
self.ssl_verify = ssl_verify
def fetch(self, from_date=DEFAULT_DATETIME):
"""Fetch the mbox files from the remote archiver.
Stores the archives in the path given during the initialization
of this object. Those archives which a not valid extension will
be ignored.
Pipermail archives usually have on their file names the date of
the archives stored following the schema year-month. When `from_date`
property is called, it will return the mboxes which their year
and month are equal or after that date.
:param from_date: fetch archives that store messages
equal or after the given date; only year and month values
are compared
:returns: a list of tuples, storing the links and paths of the
fetched archives
"""
logger.info("Downloading mboxes from '%s' to since %s",
self.url, str(from_date))
logger.debug("Storing mboxes in '%s'", self.dirpath)
from_date = datetime_to_utc(from_date)
r = requests.get(self.url, verify=self.ssl_verify)
r.raise_for_status()
links = self._parse_archive_links(r.text)
fetched = []
if not os.path.exists(self.dirpath):
os.makedirs(self.dirpath)
for link in links:
filename = os.path.basename(link)
mbox_dt = self._parse_date_from_filepath(filename)
if ((from_date.year == mbox_dt.year and
from_date.month == mbox_dt.month) or
from_date < mbox_dt):
filepath = os.path.join(self.dirpath, filename)
success = self._download_archive(link, filepath)
if success:
fetched.append((link, filepath))
logger.info("%s/%s MBoxes downloaded", len(fetched), len(links))
return fetched
@property
def mboxes(self):
"""Get the mboxes managed by this mailing list.
Returns the archives sorted by date in ascending order.
:returns: a list of `.MBoxArchive` objects
"""
archives = []
for mbox in super().mboxes:
dt = self._parse_date_from_filepath(mbox.filepath)
archives.append((dt, mbox))
archives.sort(key=lambda x: x[0])
return [a[1] for a in archives]
def _parse_archive_links(self, raw_html):
bs = bs4.BeautifulSoup(raw_html, 'html.parser')
candidates = [a['href'] for a in bs.find_all('a', href=True)]
links = []
for candidate in candidates:
# Links from Apache's 'mod_mbox' plugin contain
# trailing "/thread" substrings. Remove them to get
# the links where mbox files are stored.
if candidate.endswith(MOD_MBOX_THREAD_STR):
candidate = candidate[:-len(MOD_MBOX_THREAD_STR)]
# Ignore links with not recognized extension
ext1 = os.path.splitext(candidate)[-1]
ext2 = os.path.splitext(candidate.rstrip(ext1))[-1]
if ext1 in PIPERMAIL_TYPES or ext2 in PIPERMAIL_TYPES:
links.append(urijoin(self.url, candidate))
else:
logger.debug("Ignoring %s archive because its extension was not recognized",
candidate)
logger.debug("%s archives found", len(links))
return links
def _parse_date_from_filepath(self, filepath):
default_dt = datetime.datetime(2100, 1, 1,
tzinfo=dateutil.tz.tzutc())
try:
name = os.path.basename(filepath)
dt = dateutil.parser.parse(name, default=default_dt,
fuzzy=True)
except (AttributeError, TypeError, ValueError) as e:
dt = default_dt
logger.debug("Date of file %s not detected due to %s",
filepath, str(e))
logger.debug("Date set to default: %s", str(dt))
return dt
def _download_archive(self, url, filepath):
try:
r = requests.get(url, stream=True, verify=self.ssl_verify)
r.raise_for_status()
self._write_archive(r, filepath)
except requests.exceptions.HTTPError as e:
if e.response.status_code == 403:
logger.warning("Ignoring %s archive due to: %s", url, str(e))
return False
else:
raise e
except OSError as e:
logger.warning("Ignoring %s archive due to: %s", url, str(e))
return False
logger.debug("%s archive downloaded and stored in %s", url, filepath)
return True
@staticmethod
def _write_archive(r, filepath):
with open(filepath, 'wb') as fd:
fd.write(r.raw.read())