Mercurial > hg > cc > cirrus_home
annotate bin/ix.py @ 108:9e5b117dc461
using Popen to run igzip (also not great)
author | Henry S. Thompson <ht@inf.ed.ac.uk> |
---|---|
date | Thu, 22 Apr 2021 19:06:55 +0000 |
parents | 007f35b9df9c |
children | 15abf4aab307 |
rev | line source |
---|---|
105
baf56ff538f8
convert to rich directory structure per 2019-35
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
104
diff
changeset
|
1 #!/usr/bin/env python3 |
94
d60073ec798a
just strugging with argparse
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
2 '''Extract request records from Common Crawl WARC-format files |
105
baf56ff538f8
convert to rich directory structure per 2019-35
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
104
diff
changeset
|
3 given length, offset and filename triples. |
94
d60073ec798a
just strugging with argparse
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
4 Input one triple on command line, or |
d60073ec798a
just strugging with argparse
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
5 triples from stdin as tab-delimited lines |
d60073ec798a
just strugging with argparse
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
6 or complete cdx index lines. |
105
baf56ff538f8
convert to rich directory structure per 2019-35
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
104
diff
changeset
|
7 In all cases by 'filename' is meant crawlid/segmentid/filename |
94
d60073ec798a
just strugging with argparse
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
8 |
98
1a4c5fdc2923
help format hacking done
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
97
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.''' |
94
d60073ec798a
just strugging with argparse
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
10 |
108
9e5b117dc461
using Popen to run igzip (also not great)
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
107
diff
changeset
|
11 import sys, argparse, regex, os, shutil, io, gzip, time |
9e5b117dc461
using Popen to run igzip (also not great)
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
107
diff
changeset
|
12 #from isal import igzip |
9e5b117dc461
using Popen to run igzip (also not great)
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
107
diff
changeset
|
13 from subprocess import Popen, PIPE |
9e5b117dc461
using Popen to run igzip (also not great)
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
107
diff
changeset
|
14 #import asyncio |
97
2b880f2ce894
basic help format hacking works
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
94
diff
changeset
|
15 |
98
1a4c5fdc2923
help format hacking done
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
97
diff
changeset
|
16 HACK_USAGE=regex.compile('\[-x\]\n\s*\[length\] \[offset\] \[filename\]') |
105
baf56ff538f8
convert to rich directory structure per 2019-35
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
104
diff
changeset
|
17 BINOUT=sys.stdout.buffer |
107
007f35b9df9c
added support for copying to/using /dev/shm or /tmp
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
106
diff
changeset
|
18 FPAT="/%s/%s/orig/warc/%s" |
94
d60073ec798a
just strugging with argparse
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
19 |
97
2b880f2ce894
basic help format hacking works
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
94
diff
changeset
|
20 class HackFormat(argparse.RawDescriptionHelpFormatter): |
2b880f2ce894
basic help format hacking works
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
94
diff
changeset
|
21 def format_help(self): |
2b880f2ce894
basic help format hacking works
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
94
diff
changeset
|
22 global FOO |
2b880f2ce894
basic help format hacking works
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
94
diff
changeset
|
23 FOO=argparse.RawDescriptionHelpFormatter.format_help(self) |
98
1a4c5fdc2923
help format hacking done
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
97
diff
changeset
|
24 return HACK_USAGE.sub('\n [ ( -x | length offset filename ) ]', |
1a4c5fdc2923
help format hacking done
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
97
diff
changeset
|
25 FOO) |
100 | 26 |
107
007f35b9df9c
added support for copying to/using /dev/shm or /tmp
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
106
diff
changeset
|
27 def process(options,buf,root,filename,offset,length,whole): |
007f35b9df9c
added support for copying to/using /dev/shm or /tmp
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
106
diff
changeset
|
28 rfn=root+filename |
007f35b9df9c
added support for copying to/using /dev/shm or /tmp
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
106
diff
changeset
|
29 if root!="/beegfs/common_crawl": |
007f35b9df9c
added support for copying to/using /dev/shm or /tmp
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
106
diff
changeset
|
30 if not os.path.exists(rfn): |
007f35b9df9c
added support for copying to/using /dev/shm or /tmp
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
106
diff
changeset
|
31 if not os.path.exists(os.path.dirname(rfn)): |
007f35b9df9c
added support for copying to/using /dev/shm or /tmp
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
106
diff
changeset
|
32 os.makedirs(os.path.dirname(rfn)) |
108
9e5b117dc461
using Popen to run igzip (also not great)
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
107
diff
changeset
|
33 with io.FileIO('/beegfs/common_crawl'+filename,'r') as infile, \ |
9e5b117dc461
using Popen to run igzip (also not great)
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
107
diff
changeset
|
34 io.FileIO(rfn,'w') as outfile: |
9e5b117dc461
using Popen to run igzip (also not great)
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
107
diff
changeset
|
35 #shutil.copyfileobj(infile,outfile,128*1024*1024) |
9e5b117dc461
using Popen to run igzip (also not great)
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
107
diff
changeset
|
36 while True: |
9e5b117dc461
using Popen to run igzip (also not great)
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
107
diff
changeset
|
37 l=infile.readinto(buf) |
9e5b117dc461
using Popen to run igzip (also not great)
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
107
diff
changeset
|
38 if l==0: |
9e5b117dc461
using Popen to run igzip (also not great)
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
107
diff
changeset
|
39 break |
9e5b117dc461
using Popen to run igzip (also not great)
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
107
diff
changeset
|
40 outfile.write(memoryview(buf)[:l]) |
107
007f35b9df9c
added support for copying to/using /dev/shm or /tmp
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
106
diff
changeset
|
41 file=open(rfn,'rb',0) |
100 | 42 if whole: |
108
9e5b117dc461
using Popen to run igzip (also not great)
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
107
diff
changeset
|
43 # try external unzip using Popen |
100 | 44 file.seek(offset) |
45 bv=memoryview(buf)[:length] | |
46 nb=file.readinto(bv) | |
47 if nb!=length: | |
104 | 48 print("losing",file.name,length,nb,file=sys.stderr) |
108
9e5b117dc461
using Popen to run igzip (also not great)
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
107
diff
changeset
|
49 if options.zipped: |
9e5b117dc461
using Popen to run igzip (also not great)
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
107
diff
changeset
|
50 BINOUT.write(bv) |
9e5b117dc461
using Popen to run igzip (also not great)
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
107
diff
changeset
|
51 else: |
9e5b117dc461
using Popen to run igzip (also not great)
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
107
diff
changeset
|
52 #gzip_chunk = io.BytesIO(bv) |
9e5b117dc461
using Popen to run igzip (also not great)
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
107
diff
changeset
|
53 uv=memoryview(buf)[length:] |
9e5b117dc461
using Popen to run igzip (also not great)
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
107
diff
changeset
|
54 #clear_bytes=io.BytesIO(uv) |
9e5b117dc461
using Popen to run igzip (also not great)
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
107
diff
changeset
|
55 p = Popen(["/lustre/home/dc007/hst/gentoo/usr/bin/igzip", |
9e5b117dc461
using Popen to run igzip (also not great)
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
107
diff
changeset
|
56 "-dc"], |
9e5b117dc461
using Popen to run igzip (also not great)
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
107
diff
changeset
|
57 stdin=PIPE, |
9e5b117dc461
using Popen to run igzip (also not great)
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
107
diff
changeset
|
58 stdout=None) |
9e5b117dc461
using Popen to run igzip (also not great)
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
107
diff
changeset
|
59 p.stdin.write(bv) |
9e5b117dc461
using Popen to run igzip (also not great)
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
107
diff
changeset
|
60 p.stdin.close() |
9e5b117dc461
using Popen to run igzip (also not great)
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
107
diff
changeset
|
61 res=p.wait() |
9e5b117dc461
using Popen to run igzip (also not great)
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
107
diff
changeset
|
62 if res!=0: |
9e5b117dc461
using Popen to run igzip (also not great)
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
107
diff
changeset
|
63 print('pipe failed',res,p.stderr.decode()) |
9e5b117dc461
using Popen to run igzip (also not great)
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
107
diff
changeset
|
64 exit(2) |
9e5b117dc461
using Popen to run igzip (also not great)
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
107
diff
changeset
|
65 file.close() |
9e5b117dc461
using Popen to run igzip (also not great)
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
107
diff
changeset
|
66 return |
9e5b117dc461
using Popen to run igzip (also not great)
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
107
diff
changeset
|
67 with igzip.IGzipFile(fileobj=gzip_chunk) as gzip_fin: |
9e5b117dc461
using Popen to run igzip (also not great)
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
107
diff
changeset
|
68 while True: |
9e5b117dc461
using Popen to run igzip (also not great)
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
107
diff
changeset
|
69 l=gzip_fin.readinto(uv) |
9e5b117dc461
using Popen to run igzip (also not great)
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
107
diff
changeset
|
70 if not l: |
9e5b117dc461
using Popen to run igzip (also not great)
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
107
diff
changeset
|
71 break |
9e5b117dc461
using Popen to run igzip (also not great)
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
107
diff
changeset
|
72 BINOUT.write(memoryview(uv)[:l]) |
104 | 73 file.close() |
100 | 74 |
75 def main(): | |
76 parser = argparse.ArgumentParser( | |
77 description='''Extract records from warc files given length, offset and file triples. | |
78 Input one triple on command line, or | |
79 triples from stdin as tab-delimited lines | |
105
baf56ff538f8
convert to rich directory structure per 2019-35
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
104
diff
changeset
|
80 or complete cdx index lines. |
baf56ff538f8
convert to rich directory structure per 2019-35
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
104
diff
changeset
|
81 In all cases by 'filename' is meant crawlid/segmentid/filename''', |
100 | 82 epilog='''Note that if no output flag(s) is/are given, |
83 the whole WARC record will be output, more efficiently than | |
84 would be the case if all three flags were given.''', | |
85 add_help=False, | |
86 conflict_handler='resolve', | |
87 formatter_class=HackFormat | |
88 ) | |
97
2b880f2ce894
basic help format hacking works
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
94
diff
changeset
|
89 |
100 | 90 parser.add_argument('--help',help='Show help',action='help') |
91 parser.add_argument('-d','--debug',help='Debug output',action='store_true') | |
92 parser.add_argument('-w','--warc',help='output WARC headers', | |
93 action='store_true') | |
94 parser.add_argument('-h','--headers',help='output HTTP headers', | |
95 action='store_true') | |
96 parser.add_argument('-b','--body',help='output HTTP body', | |
97 action='store_true') | |
98 parser.add_argument('-c','--cmd',help='pipes each result thru CMD') | |
107
007f35b9df9c
added support for copying to/using /dev/shm or /tmp
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
106
diff
changeset
|
99 parser.add_argument('-r','--root',nargs='?', |
007f35b9df9c
added support for copying to/using /dev/shm or /tmp
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
106
diff
changeset
|
100 help='File path root, create a copy there if necessary', |
007f35b9df9c
added support for copying to/using /dev/shm or /tmp
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
106
diff
changeset
|
101 default='/beegfs/common_crawl'), |
108
9e5b117dc461
using Popen to run igzip (also not great)
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
107
diff
changeset
|
102 parser.add_argument('-z','--zipped', |
9e5b117dc461
using Popen to run igzip (also not great)
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
107
diff
changeset
|
103 help="output raw gzipped record, ignored if any of -bhw supplied", |
9e5b117dc461
using Popen to run igzip (also not great)
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
107
diff
changeset
|
104 action='store_true') |
100 | 105 sg=parser.add_mutually_exclusive_group() |
106 sg.add_argument('-x','--index', | |
107 help='take lines of triples from a cdx index file as input', | |
108 action='store_true') | |
109 sg.add_argument('length',type=int, | |
110 help='length in bytes of gzipped record', | |
111 nargs='?') | |
112 parser.add_argument('offset',type=int, | |
113 help='start position in bytes of gzipped record', | |
114 nargs='?') | |
115 parser.add_argument('filename', | |
105
baf56ff538f8
convert to rich directory structure per 2019-35
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
104
diff
changeset
|
116 help='pathname of gzipped Common Crawl WARC-format file', |
baf56ff538f8
convert to rich directory structure per 2019-35
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
104
diff
changeset
|
117 nargs='?') |
100 | 118 # Hack the order of optional and positional in the help output |
119 parser._action_groups.sort(key=lambda g:g.title) | |
120 #parser.print_help() | |
121 pa=parser.parse_args(sys.argv[1:]) | |
106
815b33c3254a
working with -x and rich directory structure
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
105
diff
changeset
|
122 #print(pa,file=sys.stderr) |
100 | 123 if pa.length is not None: |
124 # We have to enforce our own check.. | |
125 if pa.offset is None or pa.filename is None: | |
126 parser.error("length, offset and filename must all be supplied together") | |
127 | |
108
9e5b117dc461
using Popen to run igzip (also not great)
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
107
diff
changeset
|
128 buf=bytearray(128*1024*1024) |
100 | 129 |
104 | 130 whole=not (pa.warc or pa.headers or pa.body) |
100 | 131 if pa.length is not None: |
107
007f35b9df9c
added support for copying to/using /dev/shm or /tmp
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
106
diff
changeset
|
132 process(pa,buf,pa.root,FPAT%list(pa.filename.split('/')), |
007f35b9df9c
added support for copying to/using /dev/shm or /tmp
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
106
diff
changeset
|
133 pa.offset,pa.length,whole) |
100 | 134 exit(0) |
104 | 135 if pa.index: |
136 CDX=regex.compile('length": "([0-9]*)", "offset": "([0-9]*)", "filename": "crawl-data/([^/]*)/segments/([^/]*)/warc/(.*\.gz)"') | |
137 for l in sys.stdin: | |
138 m=CDX.search(l) | |
139 if m is None: | |
140 print("index line problem: \"%s\""%l.lstrip(),file=sys.stderr) | |
141 exit(2) | |
105
baf56ff538f8
convert to rich directory structure per 2019-35
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
104
diff
changeset
|
142 f=FPAT%(m[3:6]) |
107
007f35b9df9c
added support for copying to/using /dev/shm or /tmp
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
106
diff
changeset
|
143 process(pa,buf,pa.root,f, |
007f35b9df9c
added support for copying to/using /dev/shm or /tmp
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
106
diff
changeset
|
144 int(m[2]),int(m[1]),whole) |
104 | 145 exit(0) |
100 | 146 if __name__ == "__main__": |
147 main() |