annotate bin/ix.py @ 104:61122560ae0c

-x barely working
author Henry S. Thompson <ht@inf.ed.ac.uk>
date Mon, 19 Apr 2021 18:09:51 +0000
parents f7623dbd8eb5
children baf56ff538f8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
94
d60073ec798a just strugging with argparse
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
1 #!/usr/bin/env python
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
d60073ec798a just strugging with argparse
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
3 given length, offset and file triples.
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.
d60073ec798a just strugging with argparse
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
7
98
1a4c5fdc2923 help format hacking done
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 97
diff changeset
8 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
9
97
2b880f2ce894 basic help format hacking works
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 94
diff changeset
10 import sys, argparse, regex
2b880f2ce894 basic help format hacking works
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 94
diff changeset
11
98
1a4c5fdc2923 help format hacking done
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 97
diff changeset
12 HACK_USAGE=regex.compile('\[-x\]\n\s*\[length\] \[offset\] \[filename\]')
94
d60073ec798a just strugging with argparse
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
13
97
2b880f2ce894 basic help format hacking works
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 94
diff changeset
14 class HackFormat(argparse.RawDescriptionHelpFormatter):
2b880f2ce894 basic help format hacking works
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 94
diff changeset
15 def format_help(self):
2b880f2ce894 basic help format hacking works
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 94
diff changeset
16 global FOO
2b880f2ce894 basic help format hacking works
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 94
diff changeset
17 FOO=argparse.RawDescriptionHelpFormatter.format_help(self)
98
1a4c5fdc2923 help format hacking done
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 97
diff changeset
18 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
19 FOO)
100
f7623dbd8eb5 bare minimum working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 99
diff changeset
20
104
61122560ae0c -x barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 100
diff changeset
21 def process(options,buf,file,offset,length,whole):
100
f7623dbd8eb5 bare minimum working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 99
diff changeset
22 if whole:
f7623dbd8eb5 bare minimum working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 99
diff changeset
23 file.seek(offset)
f7623dbd8eb5 bare minimum working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 99
diff changeset
24 bv=memoryview(buf)[:length]
f7623dbd8eb5 bare minimum working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 99
diff changeset
25 nb=file.readinto(bv)
f7623dbd8eb5 bare minimum working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 99
diff changeset
26 if nb!=length:
104
61122560ae0c -x barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 100
diff changeset
27 print("losing",file.name,length,nb,file=sys.stderr)
100
f7623dbd8eb5 bare minimum working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 99
diff changeset
28 exit(1)
f7623dbd8eb5 bare minimum working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 99
diff changeset
29 sys.stdout.buffer.write(bv)
104
61122560ae0c -x barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 100
diff changeset
30 file.close()
100
f7623dbd8eb5 bare minimum working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 99
diff changeset
31
f7623dbd8eb5 bare minimum working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 99
diff changeset
32 def main():
f7623dbd8eb5 bare minimum working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 99
diff changeset
33 parser = argparse.ArgumentParser(
f7623dbd8eb5 bare minimum working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 99
diff changeset
34 description='''Extract records from warc files given length, offset and file triples.
f7623dbd8eb5 bare minimum working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 99
diff changeset
35 Input one triple on command line, or
f7623dbd8eb5 bare minimum working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 99
diff changeset
36 triples from stdin as tab-delimited lines
f7623dbd8eb5 bare minimum working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 99
diff changeset
37 or complete cdx index lines.''',
f7623dbd8eb5 bare minimum working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 99
diff changeset
38 epilog='''Note that if no output flag(s) is/are given,
f7623dbd8eb5 bare minimum working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 99
diff changeset
39 the whole WARC record will be output, more efficiently than
f7623dbd8eb5 bare minimum working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 99
diff changeset
40 would be the case if all three flags were given.''',
f7623dbd8eb5 bare minimum working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 99
diff changeset
41 add_help=False,
f7623dbd8eb5 bare minimum working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 99
diff changeset
42 conflict_handler='resolve',
f7623dbd8eb5 bare minimum working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 99
diff changeset
43 formatter_class=HackFormat
f7623dbd8eb5 bare minimum working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 99
diff changeset
44 )
97
2b880f2ce894 basic help format hacking works
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 94
diff changeset
45
100
f7623dbd8eb5 bare minimum working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 99
diff changeset
46 parser.add_argument('--help',help='Show help',action='help')
f7623dbd8eb5 bare minimum working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 99
diff changeset
47 parser.add_argument('-d','--debug',help='Debug output',action='store_true')
f7623dbd8eb5 bare minimum working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 99
diff changeset
48 parser.add_argument('-w','--warc',help='output WARC headers',
f7623dbd8eb5 bare minimum working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 99
diff changeset
49 action='store_true')
f7623dbd8eb5 bare minimum working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 99
diff changeset
50 parser.add_argument('-h','--headers',help='output HTTP headers',
f7623dbd8eb5 bare minimum working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 99
diff changeset
51 action='store_true')
f7623dbd8eb5 bare minimum working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 99
diff changeset
52 parser.add_argument('-b','--body',help='output HTTP body',
f7623dbd8eb5 bare minimum working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 99
diff changeset
53 action='store_true')
f7623dbd8eb5 bare minimum working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 99
diff changeset
54 parser.add_argument('-c','--cmd',help='pipes each result thru CMD')
f7623dbd8eb5 bare minimum working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 99
diff changeset
55 sg=parser.add_mutually_exclusive_group()
f7623dbd8eb5 bare minimum working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 99
diff changeset
56 sg.add_argument('-x','--index',
f7623dbd8eb5 bare minimum working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 99
diff changeset
57 help='take lines of triples from a cdx index file as input',
f7623dbd8eb5 bare minimum working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 99
diff changeset
58 action='store_true')
f7623dbd8eb5 bare minimum working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 99
diff changeset
59 sg.add_argument('length',type=int,
f7623dbd8eb5 bare minimum working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 99
diff changeset
60 help='length in bytes of gzipped record',
f7623dbd8eb5 bare minimum working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 99
diff changeset
61 nargs='?')
f7623dbd8eb5 bare minimum working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 99
diff changeset
62 parser.add_argument('offset',type=int,
f7623dbd8eb5 bare minimum working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 99
diff changeset
63 help='start position in bytes of gzipped record',
f7623dbd8eb5 bare minimum working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 99
diff changeset
64 nargs='?')
f7623dbd8eb5 bare minimum working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 99
diff changeset
65 parser.add_argument('filename',
f7623dbd8eb5 bare minimum working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 99
diff changeset
66 help='name of gzipped Common Crawl WARC-format file',
f7623dbd8eb5 bare minimum working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 99
diff changeset
67 nargs='?',
f7623dbd8eb5 bare minimum working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 99
diff changeset
68 type=argparse.FileType('rb',0))
f7623dbd8eb5 bare minimum working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 99
diff changeset
69 # Hack the order of optional and positional in the help output
f7623dbd8eb5 bare minimum working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 99
diff changeset
70 parser._action_groups.sort(key=lambda g:g.title)
f7623dbd8eb5 bare minimum working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 99
diff changeset
71 #parser.print_help()
f7623dbd8eb5 bare minimum working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 99
diff changeset
72 pa=parser.parse_args(sys.argv[1:])
104
61122560ae0c -x barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 100
diff changeset
73 #print(pa,file=sys.stderr)
100
f7623dbd8eb5 bare minimum working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 99
diff changeset
74 if pa.length is not None:
f7623dbd8eb5 bare minimum working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 99
diff changeset
75 # We have to enforce our own check..
f7623dbd8eb5 bare minimum working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 99
diff changeset
76 if pa.offset is None or pa.filename is None:
f7623dbd8eb5 bare minimum working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 99
diff changeset
77 parser.error("length, offset and filename must all be supplied together")
f7623dbd8eb5 bare minimum working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 99
diff changeset
78
f7623dbd8eb5 bare minimum working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 99
diff changeset
79 buf=bytearray(1024*1024)
f7623dbd8eb5 bare minimum working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 99
diff changeset
80
104
61122560ae0c -x barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 100
diff changeset
81 whole=not (pa.warc or pa.headers or pa.body)
100
f7623dbd8eb5 bare minimum working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 99
diff changeset
82 if pa.length is not None:
104
61122560ae0c -x barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 100
diff changeset
83 process(pa,buf,pa.filename,pa.offset,pa.length,whole)
100
f7623dbd8eb5 bare minimum working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 99
diff changeset
84 exit(0)
104
61122560ae0c -x barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 100
diff changeset
85 if pa.index:
61122560ae0c -x barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 100
diff changeset
86 CDX=regex.compile('length": "([0-9]*)", "offset": "([0-9]*)", "filename": "crawl-data/([^/]*)/segments/([^/]*)/warc/(.*\.gz)"')
61122560ae0c -x barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 100
diff changeset
87 for l in sys.stdin:
61122560ae0c -x barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 100
diff changeset
88 m=CDX.search(l)
61122560ae0c -x barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 100
diff changeset
89 if m is None:
61122560ae0c -x barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 100
diff changeset
90 print("index line problem: \"%s\""%l.lstrip(),file=sys.stderr)
61122560ae0c -x barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 100
diff changeset
91 exit(2)
61122560ae0c -x barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 100
diff changeset
92 f="%s/%s/%s"%(m[3],m[4],m[5])
61122560ae0c -x barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 100
diff changeset
93 process(pa,buf,open(f,'rb',0),int(m[2]),int(m[1]),whole)
61122560ae0c -x barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 100
diff changeset
94 exit(0)
100
f7623dbd8eb5 bare minimum working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 99
diff changeset
95 if __name__ == "__main__":
f7623dbd8eb5 bare minimum working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 99
diff changeset
96 main()