Mercurial > hg > cc > cirrus_home
annotate bin/ix.py @ 101:fbca56fabbac
(none)
| author | Henry S. Thompson <ht@inf.ed.ac.uk> |
|---|---|
| date | Mon, 19 Apr 2021 13:07:58 +0000 |
| parents | f7623dbd8eb5 |
| children | 61122560ae0c |
| 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 | 20 |
| 21 def process(options,buf,file,offset,length): | |
| 22 whole=not (options.warc or options.headers or options.body) | |
| 23 if whole: | |
| 24 file.seek(offset) | |
| 25 bv=memoryview(buf)[:length] | |
| 26 nb=file.readinto(bv) | |
| 27 if nb!=length: | |
| 28 print("losing",file.name,name,length,nb,file=sys.stderr) | |
| 29 exit(1) | |
| 30 sys.stdout.buffer.write(bv) | |
| 31 | |
| 32 def main(): | |
| 33 parser = argparse.ArgumentParser( | |
| 34 description='''Extract records from warc files given length, offset and file triples. | |
| 35 Input one triple on command line, or | |
| 36 triples from stdin as tab-delimited lines | |
| 37 or complete cdx index lines.''', | |
| 38 epilog='''Note that if no output flag(s) is/are given, | |
| 39 the whole WARC record will be output, more efficiently than | |
| 40 would be the case if all three flags were given.''', | |
| 41 add_help=False, | |
| 42 conflict_handler='resolve', | |
| 43 formatter_class=HackFormat | |
| 44 ) | |
|
97
2b880f2ce894
basic help format hacking works
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
94
diff
changeset
|
45 |
| 100 | 46 parser.add_argument('--help',help='Show help',action='help') |
| 47 parser.add_argument('-d','--debug',help='Debug output',action='store_true') | |
| 48 parser.add_argument('-w','--warc',help='output WARC headers', | |
| 49 action='store_true') | |
| 50 parser.add_argument('-h','--headers',help='output HTTP headers', | |
| 51 action='store_true') | |
| 52 parser.add_argument('-b','--body',help='output HTTP body', | |
| 53 action='store_true') | |
| 54 parser.add_argument('-c','--cmd',help='pipes each result thru CMD') | |
| 55 sg=parser.add_mutually_exclusive_group() | |
| 56 sg.add_argument('-x','--index', | |
| 57 help='take lines of triples from a cdx index file as input', | |
| 58 action='store_true') | |
| 59 sg.add_argument('length',type=int, | |
| 60 help='length in bytes of gzipped record', | |
| 61 nargs='?') | |
| 62 parser.add_argument('offset',type=int, | |
| 63 help='start position in bytes of gzipped record', | |
| 64 nargs='?') | |
| 65 parser.add_argument('filename', | |
| 66 help='name of gzipped Common Crawl WARC-format file', | |
| 67 nargs='?', | |
| 68 type=argparse.FileType('rb',0)) | |
| 69 # Hack the order of optional and positional in the help output | |
| 70 parser._action_groups.sort(key=lambda g:g.title) | |
| 71 #parser.print_help() | |
| 72 pa=parser.parse_args(sys.argv[1:]) | |
| 73 print(pa,file=sys.stderr) | |
| 74 if pa.length is not None: | |
| 75 # We have to enforce our own check.. | |
| 76 if pa.offset is None or pa.filename is None: | |
| 77 parser.error("length, offset and filename must all be supplied together") | |
| 78 | |
| 79 buf=bytearray(1024*1024) | |
| 80 | |
| 81 if pa.length is not None: | |
| 82 process(pa,buf,pa.filename,pa.offset,pa.length) | |
| 83 exit(0) | |
| 84 | |
| 85 if __name__ == "__main__": | |
| 86 main() |
