Mercurial > hg > cc > cirrus_work
annotate lib/python/cc/ix.py @ 298:fdec28613df3 default
robotstxt now working?
| author | Henry S. Thompson <ht@inf.ed.ac.uk> |
|---|---|
| date | Mon, 05 May 2025 20:57:46 +0100 |
| parents | a4538bdfa92a |
| children | 4759d6ac4625 |
| rev | line source |
|---|---|
|
18
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
1 #!/usr/bin/env python3 |
| 41 | 2 '''Extract response records from Common Crawl WARC-format files |
|
18
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
3 given length, offset and filename triples. |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
4 Input one triple on command line, or |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
5 triples from stdin as tab-delimited lines |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
6 or complete cdx index lines. |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
7 In all cases by 'filename' is meant crawlid/segmentid/type/filename |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
8 |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
9 Note that if no output flag(s) is/are given, the whole WARC record will be output, more efficiently than would be the case if -whb is given.''' |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
10 |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
11 import sys, argparse, regex, os, shutil, io, gzip, time, shlex |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
12 from isal import igzip |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
13 from subprocess import Popen, PIPE |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
14 #import asyncio |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
15 |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
16 HACK_USAGE=regex.compile('\[-x\]\n\s*\[length\] \[offset\] \[filename\]') |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
17 BINOUT=sys.stdout.buffer |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
18 FPAT="/%s/%s/orig/%s/%s" |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
19 |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
20 CMD_PROC=None |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
21 TMPFILENAME=None |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
22 |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
23 class HackFormat(argparse.RawDescriptionHelpFormatter): |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
24 def format_help(self): |
| 234 | 25 global FOO |
|
18
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
26 FOO=argparse.RawDescriptionHelpFormatter.format_help(self) |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
27 return HACK_USAGE.sub('\n [ ( -x | length offset filename ) ]', |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
28 FOO) |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
29 |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
30 def process(options,buf,filename,offset,length,whole): |
|
21
cbac7dfe2f24
interpolate process0, support permanent subproc
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
18
diff
changeset
|
31 try: |
|
cbac7dfe2f24
interpolate process0, support permanent subproc
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
18
diff
changeset
|
32 process0(options,buf,filename,offset,length,whole) |
|
cbac7dfe2f24
interpolate process0, support permanent subproc
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
18
diff
changeset
|
33 except Exception as e: |
|
cbac7dfe2f24
interpolate process0, support permanent subproc
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
18
diff
changeset
|
34 if options.debug: |
|
cbac7dfe2f24
interpolate process0, support permanent subproc
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
18
diff
changeset
|
35 import traceback |
|
cbac7dfe2f24
interpolate process0, support permanent subproc
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
18
diff
changeset
|
36 traceback.print_exc(file=sys.stderr) |
|
cbac7dfe2f24
interpolate process0, support permanent subproc
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
18
diff
changeset
|
37 else: |
| 238 | 38 print("Process fail: %s, input:\n length=%s, offset=%s, file=%s"%(e, |
| 39 length, | |
| 40 offset, | |
| 41 filename), | |
| 42 file=sys.stderr) | |
|
21
cbac7dfe2f24
interpolate process0, support permanent subproc
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
18
diff
changeset
|
43 exit(3) |
|
cbac7dfe2f24
interpolate process0, support permanent subproc
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
18
diff
changeset
|
44 |
|
cbac7dfe2f24
interpolate process0, support permanent subproc
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
18
diff
changeset
|
45 def process0(options,buf,filename,offset,length,whole): |
|
cbac7dfe2f24
interpolate process0, support permanent subproc
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
18
diff
changeset
|
46 global TMPFILENAME, TMPFILE |
|
18
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
47 if options.save: |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
48 (tf,TMPFILENAME)=tempfile.mkstemp() |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
49 TMPFILE=open(tf,mode='wb') |
|
21
cbac7dfe2f24
interpolate process0, support permanent subproc
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
18
diff
changeset
|
50 if options.cmd and not options.process: |
|
cbac7dfe2f24
interpolate process0, support permanent subproc
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
18
diff
changeset
|
51 launch(options.cmd) |
|
18
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
52 process1(options,buf,filename,offset,length,whole) |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
53 if options.save: |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
54 TMPFILE.close() |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
55 if options.cmd: |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
56 _output_subproc(bytes(TMPFILENAME,'utf-8')) |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
57 _output_subproc(b"\n") |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
58 else: |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
59 BINOUT.write(bytes(TMPFILENAME,'utf-8')) |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
60 BINOUT.write(b"\n") |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
61 if options.cmd: |
|
21
cbac7dfe2f24
interpolate process0, support permanent subproc
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
18
diff
changeset
|
62 if not options.process: |
|
cbac7dfe2f24
interpolate process0, support permanent subproc
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
18
diff
changeset
|
63 windup(filename,options,length) |
|
18
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
64 if options.save: |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
65 os.unlink(TMPFILENAME) |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
66 TMPFILENAME=None |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
67 elif options.save: |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
68 print("%s will need to be deleted"%TMPFILENAME,file=sys.stderr) |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
69 TMPFILENAME=None |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
70 |
|
21
cbac7dfe2f24
interpolate process0, support permanent subproc
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
18
diff
changeset
|
71 def launch(cmd): |
|
cbac7dfe2f24
interpolate process0, support permanent subproc
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
18
diff
changeset
|
72 global CMD_PROC, BINOUT |
|
cbac7dfe2f24
interpolate process0, support permanent subproc
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
18
diff
changeset
|
73 CMD_PROC=Popen(shlex.split(cmd),stdin=PIPE,bufsize=0) |
|
cbac7dfe2f24
interpolate process0, support permanent subproc
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
18
diff
changeset
|
74 BINOUT=CMD_PROC.stdin |
|
cbac7dfe2f24
interpolate process0, support permanent subproc
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
18
diff
changeset
|
75 |
|
cbac7dfe2f24
interpolate process0, support permanent subproc
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
18
diff
changeset
|
76 def windup(length,offset,filename): |
|
cbac7dfe2f24
interpolate process0, support permanent subproc
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
18
diff
changeset
|
77 # Wind up subproc |
|
cbac7dfe2f24
interpolate process0, support permanent subproc
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
18
diff
changeset
|
78 BINOUT.close() |
|
cbac7dfe2f24
interpolate process0, support permanent subproc
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
18
diff
changeset
|
79 if CMD_PROC.wait()!=0: # could/should be async? |
|
cbac7dfe2f24
interpolate process0, support permanent subproc
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
18
diff
changeset
|
80 print("subproc of %s:%s:%s failed with %s"%(length,offset,filename, |
|
cbac7dfe2f24
interpolate process0, support permanent subproc
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
18
diff
changeset
|
81 CMD_PROC.returncode), |
|
cbac7dfe2f24
interpolate process0, support permanent subproc
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
18
diff
changeset
|
82 file=sys.stderr) |
|
cbac7dfe2f24
interpolate process0, support permanent subproc
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
18
diff
changeset
|
83 |
|
18
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
84 def _output_tmpfile(buf): |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
85 TMPFILE.write(buf) |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
86 |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
87 def _output_stdout(buf): |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
88 BINOUT.write(buf) |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
89 |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
90 def _output_subproc(buf): |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
91 toWrite=len(buf) |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
92 while toWrite>0: |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
93 toWrite -= BINOUT.write(buf) |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
94 |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
95 def process1(options,buf,filename,offset,length,whole): |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
96 root=options.root |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
97 rfn=root+filename |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
98 if root!="/beegfs/common_crawl": |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
99 # Support using ramdisk or other local disk as a faster cached |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
100 if not os.path.exists(rfn): |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
101 if not os.path.exists(os.path.dirname(rfn)): |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
102 os.makedirs(os.path.dirname(rfn)) |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
103 with io.FileIO('/beegfs/common_crawl'+filename,'r') as infile, \ |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
104 io.FileIO(rfn,'w') as outfile: |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
105 #shutil.copyfileobj(infile,outfile,128*1024*1024) |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
106 while True: |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
107 l=infile.readinto(buf) |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
108 if l==0: |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
109 break |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
110 outfile.write(memoryview(buf)[:l]) |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
111 file=open(rfn,'rb',0) |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
112 file.seek(offset) |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
113 bv=memoryview(buf)[:length] |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
114 nb=file.readinto(bv) |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
115 file.close() |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
116 if nb!=length: |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
117 raise ValueError("Chunk read losing: %s, got %s expected %s at %s"%(file.name, |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
118 nb,length,offset)) |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
119 if whole and options.zipped: |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
120 _output(bv) |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
121 return |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
122 gzip_chunk = io.BytesIO(bv) |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
123 uv=memoryview(buf)[length:] |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
124 with igzip.IGzipFile(fileobj=gzip_chunk) as gzip_fin: |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
125 ll=0 |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
126 while True: |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
127 l=gzip_fin.readinto(uv) |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
128 if not l: |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
129 break |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
130 ll+=l |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
131 cb=memoryview(uv)[:ll] |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
132 if whole: |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
133 _output(cb) |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
134 return |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
135 # Only output parts (0 = WARC header, 1 = HTTP header, 2 = body) that are wanted |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
136 state=0 |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
137 tr=None # Was this record truncated? |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
138 bl=None # for HTTP Content-Length for the length of the body? |
|
21
cbac7dfe2f24
interpolate process0, support permanent subproc
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
18
diff
changeset
|
139 # Could we make this faster by working purely within the cb memoryview? |
|
cbac7dfe2f24
interpolate process0, support permanent subproc
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
18
diff
changeset
|
140 # It would be messy, but avoid copying huge amounts |
|
cbac7dfe2f24
interpolate process0, support permanent subproc
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
18
diff
changeset
|
141 # The outer loop would just be something like |
|
cbac7dfe2f24
interpolate process0, support permanent subproc
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
18
diff
changeset
|
142 # clbv=memoryview(bytearray(b"Content-Length: ")) |
|
cbac7dfe2f24
interpolate process0, support permanent subproc
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
18
diff
changeset
|
143 # i=s=0 |
|
cbac7dfe2f24
interpolate process0, support permanent subproc
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
18
diff
changeset
|
144 # while i<ll: |
|
cbac7dfe2f24
interpolate process0, support permanent subproc
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
18
diff
changeset
|
145 # if cb[i]==10: # need to handle \r\n |
|
cbac7dfe2f24
interpolate process0, support permanent subproc
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
18
diff
changeset
|
146 # L=cb[s:i] |
|
cbac7dfe2f24
interpolate process0, support permanent subproc
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
18
diff
changeset
|
147 # s=i=i+1 |
|
cbac7dfe2f24
interpolate process0, support permanent subproc
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
18
diff
changeset
|
148 # if L[:16]==clbv: |
|
cbac7dfe2f24
interpolate process0, support permanent subproc
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
18
diff
changeset
|
149 # wl=int(L[16:]) |
|
cbac7dfe2f24
interpolate process0, support permanent subproc
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
18
diff
changeset
|
150 # else: |
|
cbac7dfe2f24
interpolate process0, support permanent subproc
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
18
diff
changeset
|
151 # i+=1 |
|
cbac7dfe2f24
interpolate process0, support permanent subproc
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
18
diff
changeset
|
152 # |
|
18
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
153 with io.BytesIO(cb) as clear_text: |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
154 for L in clear_text: |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
155 if state==0: |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
156 # WARC header |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
157 if L.startswith(b"Content-Length: "): |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
158 wl=int(L[16:].rstrip()) |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
159 elif L.startswith(b"WARC-Truncated: "): |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
160 tr=L[16:].rstrip() |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
161 tr="EMPTY" if tr=="" else tr |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
162 elif L==b"" or L.startswith(b"\r"): # for idempotency |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
163 # Blank line, WARC header is finished |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
164 if not (options.headers or options.body): |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
165 return |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
166 state=1 |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
167 # Note we preserve the empty line |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
168 if options.warc: |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
169 _output(L) |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
170 continue |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
171 if state==1: |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
172 # HTTP header |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
173 wl -= len(L) |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
174 if not (L==b"" or L.startswith(b"\r")): |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
175 # Non-blank, it's a header |
|
21
cbac7dfe2f24
interpolate process0, support permanent subproc
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
18
diff
changeset
|
176 (h,_,v)=L.partition(b": ") |
|
cbac7dfe2f24
interpolate process0, support permanent subproc
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
18
diff
changeset
|
177 if bl is None and (h==b"Content-Length"): |
|
cbac7dfe2f24
interpolate process0, support permanent subproc
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
18
diff
changeset
|
178 bl=int(v) |
|
18
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
179 if options.headers: |
|
21
cbac7dfe2f24
interpolate process0, support permanent subproc
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
18
diff
changeset
|
180 if isinstance(options.headers,dict): |
|
cbac7dfe2f24
interpolate process0, support permanent subproc
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
18
diff
changeset
|
181 if h in options.headers: |
|
cbac7dfe2f24
interpolate process0, support permanent subproc
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
18
diff
changeset
|
182 options.headers[h]=v |
|
cbac7dfe2f24
interpolate process0, support permanent subproc
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
18
diff
changeset
|
183 else: |
|
cbac7dfe2f24
interpolate process0, support permanent subproc
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
18
diff
changeset
|
184 _output(L) |
|
18
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
185 else: |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
186 # Blank line, HTTP header is finished |
|
21
cbac7dfe2f24
interpolate process0, support permanent subproc
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
18
diff
changeset
|
187 if isinstance(options.headers,dict): |
|
cbac7dfe2f24
interpolate process0, support permanent subproc
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
18
diff
changeset
|
188 _output(bytes(str(options.headers),'utf-8')) |
|
18
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
189 if not options.body: |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
190 return |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
191 if options.headers: |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
192 _output(L) |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
193 state=2 |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
194 # The above is just for sanity, because we do _not_ |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
195 # continue with the outer loop, |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
196 # since we can now block-output the entire rest of the |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
197 # input buffer. |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
198 if bl is not None: |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
199 if bl!=wl: |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
200 print("length mismatch: %s %s %s here: %s given: %s trunc: %s"%\ |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
201 (length,offset,filename,wl,bl,tr),file=sys.stderr) |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
202 # HTTP body |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
203 balance=clear_text.tell() |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
204 #print(balance,bl,wl,ll,ll-balance,file=sys.stderr) |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
205 # Output whatever is left |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
206 _output(cb[balance:balance+wl]) |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
207 return |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
208 |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
209 def main(): |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
210 global _output,TMPFILE,TMPFILENAME,tempfile |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
211 parser = argparse.ArgumentParser( |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
212 description='''Extract records from warc files given length, offset and file triples. |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
213 Input one triple on command line, or |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
214 triples from stdin as tab-delimited lines |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
215 or complete cdx index lines. |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
216 In all cases by 'filename' is meant crawlid/segmentid/type/filename''', |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
217 epilog='''Note that if no output flag(s) is/are given, |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
218 the whole WARC record will be output, more efficiently than |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
219 would be the case if all three flags were given.''', |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
220 add_help=False, |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
221 conflict_handler='resolve', |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
222 formatter_class=HackFormat |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
223 ) |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
224 fphelp=('format string for turning 4 filename components into a path, must contain %%s exactly 4 times,\ndefault is "%s"'%FPAT).replace('%s','%%s') |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
225 parser.add_argument('--help',help='Show help',action='help') |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
226 parser.add_argument('-d','--debug',help='Debug output',action='store_true') |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
227 parser.add_argument('-w','--warc',help='output WARC headers', |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
228 action='store_true') |
|
21
cbac7dfe2f24
interpolate process0, support permanent subproc
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
18
diff
changeset
|
229 parser.add_argument('-h','--headers',help='process HTTP headers: collect into dict with named values (,-separated) if arg present, else output', |
|
cbac7dfe2f24
interpolate process0, support permanent subproc
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
18
diff
changeset
|
230 nargs='?',default=None,const=True) |
|
18
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
231 parser.add_argument('-b','--body',help='output HTTP body', |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
232 action='store_true') |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
233 parser.add_argument('-c','--cmd',help='pipes each result thru CMD') |
|
21
cbac7dfe2f24
interpolate process0, support permanent subproc
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
18
diff
changeset
|
234 parser.add_argument('-p','--process',help='with -c, launches CMD only once', |
|
cbac7dfe2f24
interpolate process0, support permanent subproc
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
18
diff
changeset
|
235 action='store_true') |
|
18
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
236 parser.add_argument('-m','--module.function',help='module.function to call with a stream'), |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
237 parser.add_argument('-s','--save',action='store_true', |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
238 help="write to a temporary file and output the name") |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
239 parser.add_argument('-f','--fpath', |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
240 help=fphelp, |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
241 default=FPAT) |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
242 parser.add_argument('-r','--root',nargs='?', |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
243 help='File path root, create a copy there if necessary', |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
244 default='/beegfs/common_crawl'), |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
245 parser.add_argument('-z','--zipped', |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
246 help="output raw gzipped record, ignored if any of -bhw supplied", |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
247 action='store_true') |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
248 sg=parser.add_mutually_exclusive_group() |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
249 sg.add_argument('-x','--index', |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
250 help='take lines of triples from a cdx index file as input', |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
251 action='store_true') |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
252 sg.add_argument('length',type=int, |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
253 help='length in bytes of gzipped record', |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
254 nargs='?') |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
255 parser.add_argument('offset',type=int, |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
256 help='start position in bytes of gzipped record', |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
257 nargs='?') |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
258 parser.add_argument('filename', |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
259 help='pathname of gzipped Common Crawl WARC-format file', |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
260 nargs='?') |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
261 # Hack the order of optional and positional in the help output |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
262 parser._action_groups.sort(key=lambda g:g.title) |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
263 #parser.print_help() |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
264 pa=parser.parse_args(sys.argv[1:]) |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
265 #print(pa,file=sys.stderr) |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
266 if pa.length is not None: |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
267 # We have to enforce our own check.. |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
268 if pa.offset is None or pa.filename is None: |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
269 parser.error("length, offset and filename must all be supplied together") |
|
21
cbac7dfe2f24
interpolate process0, support permanent subproc
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
18
diff
changeset
|
270 if isinstance(pa.headers,str): |
|
cbac7dfe2f24
interpolate process0, support permanent subproc
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
18
diff
changeset
|
271 pa.headers=dict((bytes(k,'utf-8'),None) for k in pa.headers.split(',')) |
|
18
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
272 |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
273 buf=bytearray(128*1024*1024) |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
274 |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
275 whole=not (pa.warc or pa.headers or pa.body) |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
276 if pa.save: |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
277 _output=_output_tmpfile |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
278 import tempfile |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
279 elif pa.cmd: |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
280 _output = _output_subproc |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
281 else: |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
282 _output = _output_stdout |
|
21
cbac7dfe2f24
interpolate process0, support permanent subproc
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
18
diff
changeset
|
283 if pa.cmd and pa.process: |
|
cbac7dfe2f24
interpolate process0, support permanent subproc
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
18
diff
changeset
|
284 launch(pa.cmd) |
|
cbac7dfe2f24
interpolate process0, support permanent subproc
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
18
diff
changeset
|
285 # three different ways to process |
|
18
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
286 if pa.index: |
| 298 | 287 CDX=regex.compile('length": "([0-9]*)", "offset": "([0-9]*)", "filename": "crawl-data/([^/]*)/segments/([^/]*)/(warc|crawldiagnostics|robotstxt)/(.*\.gz)"') # robotstxt works? |
|
18
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
288 for l in sys.stdin: |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
289 m=CDX.search(l) |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
290 if m is None: |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
291 if l.find('/robotstxt/')>-1: |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
292 continue |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
293 print("index line problem: \"%s\""%l,file=sys.stderr,end='') |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
294 exit(2) |
|
21
cbac7dfe2f24
interpolate process0, support permanent subproc
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
18
diff
changeset
|
295 filename=pa.fpath%(m[3:7]) |
|
cbac7dfe2f24
interpolate process0, support permanent subproc
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
18
diff
changeset
|
296 process(pa,buf,filename, |
|
cbac7dfe2f24
interpolate process0, support permanent subproc
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
18
diff
changeset
|
297 int(offset:=m[2]),int(length:=m[1]),whole) |
|
18
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
298 elif pa.length is not None: |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
299 print(pa.filename,file=sys.stderr) |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
300 process(pa,buf,pa.fpath%tuple(pa.filename.split('/')), |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
301 pa.offset,pa.length,whole) |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
302 else: |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
303 print("Reading length, offset, filename tab-delimited triples from stdin...", |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
304 file=sys.stderr) |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
305 for l in sys.stdin: |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
306 try: |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
307 (length,offset,filename)=l.rstrip().split('\t') |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
308 length=int(length) |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
309 offset=int(offset) |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
310 except ValueError as e: |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
311 parser.error('Invalid input line: %s\n "%s"'%(e,l)) |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
312 process(pa,buf,pa.fpath%tuple(filename.split('/')), |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
313 offset,length,whole) |
|
21
cbac7dfe2f24
interpolate process0, support permanent subproc
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
18
diff
changeset
|
314 # processing done one way or another |
|
cbac7dfe2f24
interpolate process0, support permanent subproc
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
18
diff
changeset
|
315 if pa.cmd and pa.process: |
|
cbac7dfe2f24
interpolate process0, support permanent subproc
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
18
diff
changeset
|
316 windup(length,offset,filename) |
|
cbac7dfe2f24
interpolate process0, support permanent subproc
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
18
diff
changeset
|
317 # if pa.save and pa.process, deleting temp files is down to cmd |
|
18
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
318 if __name__ == "__main__": |
|
046dbe557911
write to tmp file implemented
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
319 main() |
