forked from mk-fg/fgtk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pyseek
executable file
·55 lines (47 loc) · 1.76 KB
/
pyseek
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
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
from optparse import OptionParser
parser = OptionParser(usage='%prog [options] [ FROM(MiB) TO(MiB) ... ] PATH',
description=('Stream given file from/to specified offset.'
' Limits can be omitted or specified as a single dash for bof/eof.'))
parser.add_option('-b', '--bytes',
action='store_true', dest='bytes',
help='Use bytes (B) instead of MiB.')
parser.add_option('-c', '--chunk-size',
action='store', dest='bs', type='int', metavar='BYTES', default=8192,
help='Use atomic chunks of a given size for I/O.')
parser.add_option('-f',
action='store', type='str',
dest='ch_from', metavar='MiB',
help='Read stream from this point. Ignored if pairs are given as args.')
parser.add_option('-t',
action='store', type='str',
dest='ch_to', metavar='MiB',
help='Read stream to this point. Ignored if pairs are given as args.')
optz,argz = parser.parse_args()
import itertools as it, operator as op, functools as ft
from cStringIO import StringIO as sio
import os, sys
scale = 1024*1024 if not optz.bytes else 1
src = len(argz) % 2
if not src: parser.error('STDIN processing is not supported')
if len(argz) >= 2:
chunks = list( (argz[arg], argz[arg+1])
for arg in xrange(0, len(argz) - src, 2) )
else: chunks = [(optz.ch_from, optz.ch_to)]
src = open(argz[-1]) if src else sys.stdin # not really ;)
dst = sys.stdout # if not optz.buff else sio()
def read(src, dst, chunks, bs):
for pos,to in chunks:
try: pos = int(pos)*scale
except (TypeError, ValueError): pos = 0
try: to = int(to)*scale
except (TypeError, ValueError): to = 0
src.seek(pos, os.SEEK_SET)
chunk = True
while chunk:
chunk = min(to - pos, bs) if to else bs
chunk = src.read(chunk)
dst.write(chunk)
pos += len(chunk)
read(src, dst, chunks, optz.bs)