-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
tool.py
406 lines (316 loc) · 13.1 KB
/
tool.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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
'''Archive process tools'''
# Copyright 2013 Christopher Foo <chris.foo@gmail.com>
# Licensed under GPLv3. See COPYING.txt for details.
from warcat import model, util, verify
import abc
import gzip
import http.client
import isodate
import itertools
import logging
import os.path
import shutil
import sys
import time
_logger = logging.getLogger(__name__)
THROBBER = [
'(= |',
'|= |',
'| = |',
'| = |',
'| =)',
'| =|',
'| = |',
'| = |',
]
# lol; so bouncy.
class VerifyProblem(ValueError):
def __init__(self, message, iso_section=None, major=True):
ValueError.__init__(self, message, iso_section, major)
@property
def message(self):
return self.args[0]
@property
def iso_section(self):
return self.args[1]
@property
def major(self):
return self.args[2]
class BaseIterateTool(metaclass=abc.ABCMeta):
'''Base class for iterating through records'''
def __init__(self, filenames, out_file=None, write_gzip=False,
force_read_gzip=None, read_record_ids=None, preserve_block=True,
out_dir=None, print_progress=False, keep_going=False):
if not out_file:
try:
out_file = sys.stdout.buffer
except AttributeError:
out_file = sys.stdout
self.filenames = filenames
self.out_file = out_file
self.force_read_gzip = force_read_gzip
self.write_gzip = write_gzip
self.current_filename = None
self.read_record_ids = read_record_ids
self.preserve_block = preserve_block
self.out_dir = out_dir
self.print_progress = print_progress
self.keep_going = keep_going
self.check_block_length = False
def preprocess(self):
pass
def postprocess(self):
pass
def process(self):
self.num_records = 0
throbber_iter = itertools.cycle(THROBBER)
progress_msg = ''
self.preprocess()
for filename in self.filenames:
self.record_order = 0
self.current_filename = filename
f = model.WARC.open(filename, force_gzip=self.force_read_gzip)
while True:
record, has_more = model.WARC.read_record(f,
preserve_block=self.preserve_block,
check_block_length=self.check_block_length)
skip = False
if self.read_record_ids:
if record.record_id not in self.read_record_ids:
skip = True
if skip:
_logger.debug('Skipping %s due to filter',
record.record_id)
else:
try:
self.action(record)
except Exception as e:
if self.keep_going:
_logger.exception('Error on record %s',
record.record_id)
else:
raise
if self.print_progress and self.num_records % 100 == 0:
s = next(throbber_iter)
sys.stderr.write('\b' * len(progress_msg))
progress_msg = '{} {} '.format(self.num_records, s)
sys.stderr.write(progress_msg)
sys.stderr.flush()
self.record_order += 1
self.num_records += 1
if not has_more:
break
f.close()
self.postprocess()
if self.print_progress:
sys.stderr.write('\nDone. {} records processed.\n'.format(
self.num_records))
@abc.abstractmethod
def action(self, record):
pass
class ListTool(BaseIterateTool):
def action(self, record):
print('Record:', record.record_id)
print(' Order:', self.num_records)
print(' File offset:', record.file_offset)
print(' Type:', record.warc_type)
print(' Date:', isodate.datetime_isoformat(record.date))
print(' Size:', record.content_length)
class ConcatTool(BaseIterateTool):
def preprocess(self):
self.bytes_written = 0
def action(self, record):
if self.write_gzip:
f = gzip.GzipFile(fileobj=self.out_file, mode='wb')
else:
f = self.out_file
for v in record.iter_bytes():
_logger.debug('Wrote %d bytes', len(v))
f.write(v)
self.bytes_written += len(v)
if self.write_gzip:
f.close()
if self.num_records % 1000 == 0:
_logger.info('Wrote %d records (%d bytes) so far',
self.num_records, self.bytes_written)
class SplitTool(BaseIterateTool):
def action(self, record):
record_filename = '{}.{:08d}.warc'.format(
util.strip_warc_extension(os.path.basename(self.current_filename)),
self.record_order)
record_filename = os.path.join(self.out_dir, record_filename)
if not os.path.exists(self.out_dir):
os.makedirs(self.out_dir, exist_ok=True)
if self.write_gzip:
record_filename += '.gz'
f = gzip.GzipFile(record_filename, mode='wb')
else:
f = open(record_filename, 'wb')
for v in record.iter_bytes():
_logger.debug('Wrote %d bytes', len(v))
f.write(v)
f.close()
if self.num_records % 1000 == 0:
_logger.info('Wrote %d records so far', self.num_records)
class ExtractTool(BaseIterateTool):
def action(self, record):
if record.warc_type != 'response':
return
if not isinstance(record.content_block, model.BlockWithPayload):
return
if not isinstance(record.content_block.fields, model.HTTPHeaders):
return
if not record.content_block.fields.status_code == http.client.OK:
return
url = record.header.fields['WARC-Target-URI']
binary_block = record.content_block.binary_block
file_obj = binary_block.get_file()
data = file_obj.read(binary_block.length)
response = util.parse_http_response(data)
path_list = util.split_url_to_filename(url)
path_list = util.truncate_filename_parts(path_list)
path = os.path.join(self.out_dir, *path_list)
dir_path = os.path.dirname(path)
if os.path.isdir(path):
path = util.append_index_filename(path)
_logger.debug('Extracting %s to %s', record.record_id, path)
util.rename_filename_dirs(path)
os.makedirs(dir_path, exist_ok=True)
try:
with open(path, 'wb') as f:
shutil.copyfileobj(response, f)
except http.client.IncompleteRead as error:
_logger.warning('Malformed HTTP response: %s', error)
with open(path, 'wb') as f:
f.write(error.partial)
last_modified_str = response.getheader('Last-Modified')
if last_modified_str:
try:
last_modified = util.parse_http_date(last_modified_str)
except ValueError:
pass
else:
timestamp = time.mktime(last_modified.utctimetuple())
os.utime(path, (time.time(), timestamp))
_logger.debug('Apply mtime %d to %s', timestamp, path)
_logger.info('Extracted %s to %s', record.record_id, path)
class VerifyTool(BaseIterateTool):
MANDATORY_FIELDS = ['WARC-Record-ID', 'Content-Length', 'WARC-Date',
'WARC-Type']
def preprocess(self):
self.record_ids = set()
self.problems = 0
self.check_block_length = True
def action(self, record):
verify_actions = [
self.verify_mandatory_fields,
# self.check_transfer_encoding,
self.verify_block_digest,
self.verify_payload_digest,
self.verify_id_uniqueness,
self.verify_id_no_whitespace,
self.verify_content_type,
self.verify_concurrent_to,
self.verify_refers_to,
self.verify_target_uri,
self.verify_filename,
self.verify_profile,
self.verify_segment_origin_id,
self.verify_segment_total_length,
]
for action in verify_actions:
try:
action(record)
except VerifyProblem:
self.problems += 1
_logger.exception('Record %s failed validation',
record.record_id)
def verify_block_digest(self, record):
if 'WARC-Block-Digest' in record.header.fields:
if not verify.verify_block_digest(record):
raise VerifyProblem('Bad block digest.', '5.8')
_logger.debug('Block digest ok')
def verify_payload_digest(self, record):
if 'WARC-Payload-Digest' in record.header.fields:
if not verify.verify_payload_digest(record):
raise VerifyProblem('Bad payload digest.', '5.9')
_logger.debug('Payload digest ok')
def verify_id_uniqueness(self, record):
if record.record_id in self.record_ids:
raise VerifyProblem('Duplicate record ID.')
self.record_ids.add(record.record_id)
def verify_id_no_whitespace(self, record):
if ' ' in record.record_id:
raise VerifyProblem('Whitespace in ID', '5.2')
def check_transfer_encoding(self, record):
if not isinstance(record.content_block, model.BlockWithPayload):
return
if 'Transfer-encoding' in record.content_block.fields:
raise VerifyProblem('Transfer-encoding found', '5.3.2', False)
def verify_mandatory_fields(self, record):
for name in self.MANDATORY_FIELDS:
if name not in record.header.fields:
raise VerifyProblem(
'Mandatory {} field is missing'.format(name))
def verify_content_type(self, record):
if record.warc_type != 'continuation':
return
if record.content_length \
and 'Content-Type' not in record.header.fields:
raise VerifyProblem('Content-Type should be specified', '5.6',
False)
def verify_concurrent_to(self, record):
if 'WARC-Concurrent-To' not in record.header.fields:
return
record_id = record.header.fields['WARC-Concurrent-To']
if record.warc_type in ('warcinfo', 'conversion', 'continuation'):
raise VerifyProblem('Unexpected WARC-Concurrent-To', '5.7')
if record_id not in self.record_ids:
raise VerifyProblem('Concurrent Record ID {} not seen yet'.format(
record_id), major=False)
def verify_refers_to(self, record):
if 'WARC-Refers-To' not in record.header.fields:
return
record_id = record.header.fields['WARC-Refers-To']
if record.warc_type in ('warcinfo', 'response', 'request',
'continuation'):
raise VerifyProblem('WARC-Refers-To field unexpected', '5.11')
if record_id not in self.record_ids:
raise VerifyProblem('Refer to record ID {} not seen yet'.format(
record_id), major=False)
def verify_target_uri(self, record):
uri = record.header.fields.get('WARC-Target-URI')
if not uri and record.warc_type in ('response', 'resource', 'request',
'revisit', 'conversion', 'continuation'):
raise VerifyProblem('Expected WARC-Target-URI', '5.12')
if uri and record.warc_type == 'warc_info':
raise VerifyProblem('Unexpected WARC-Target-URI', '5.12')
if uri and ' ' in uri:
raise VerifyProblem('Whitespace in URI', '5.12')
def verify_warcinfo_id(self, record):
record_id = record.header.fields.get('WARC-Warcinfo-ID')
if record_id and record.warc_type == 'warcinfo':
raise VerifyProblem('Unexpected WARC-Warcinfo-ID', '5.14')
if not record_id:
raise VerifyProblem('Expected WARC-Warcinfo-ID', '5.14', False)
def verify_filename(self, record):
if 'WARC-Filename' in record.header.fields \
and record.warc_type != 'warcinfo':
raise VerifyProblem('Unexpected WARC-Filename', '5.15')
def verify_profile(self, record):
if record.warc_type == 'revisit' \
and 'WARC-Profile' not in record.header.fields:
raise VerifyProblem('Expected WARC-Profile', '5.16')
def verify_segment_origin_id(self, record):
if record.warc_type == 'continuation':
if 'WARC-Segment-Origin-ID' not in record.header.fields:
raise VerifyProblem('Expected WARC-Segment-Origin-ID', '5.19')
elif 'WARC-Segment-Origin-ID' in record.header.fields:
raise VerifyProblem('Unexpected WARC-Segment-Origin-ID', '5.19')
def verify_segment_total_length(self, record):
if record.warc_type == 'continuation':
if 'WARC-Segment-Total-Length' not in record.header.fields:
raise VerifyProblem('Expected WARC-Segment-Total-Length',
'5.20')
elif 'WARC-Segment-Total-Length' in record.header.fields:
raise VerifyProblem('Unexpected WARC-Segment-Total-Length', '5.20')