-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchunker.py
150 lines (130 loc) · 4.82 KB
/
chunker.py
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import os
import argparse
import glob
class Chunker:
def __init__(self, path, dest, chunksize='1000M', delim=None, lines=None):
self.path = path
self.dest = dest
self.chunksize = human2bytes(chunksize)
self.delim = delim
self.lines = lines
self.fn = os.path.basename(self.path)
self.name, self.ext = os.path.splitext(self.fn)
if os.path.exists(dest):
if os.listdir(dest):
raise IOError("Destination folder not empty.")
else:
os.mkdir(dest)
self.stream()
self.files = glob.glob(os.path.join(dest, '*'))
def stream(self):
if self.delim is not None:
self.stream_delim()
else:
self.stream_lines()
def stream_delim(self):
i = 0
fout = open(os.path.join(self.dest, '%s.%05d%s' % (self.name, i, self.ext)), 'w')
with open(self.path, 'r') as inf:
for l in inf:
if self.delim in l:
# check filesize, potentially increase i
fout.flush()
size = os.fstat(fout.fileno()).st_size
if size >= self.chunksize:
fout.close()
i += 1
fout = open(os.path.join(self.dest, '%s.%05d%s' % (self.name, i, self.ext)), 'w')
fout.write(l)
else:
fout.write(l)
else:
fout.write(l)
fout.close()
def stream_lines(self):
i = 0
fout = open(os.path.join(self.dest, '%s.%05d%s' % (self.name, i, self.ext)), 'w')
with open(self.path, 'r') as inf:
for j, l in enumerate(inf):
if j % self.lines == 0:
# check filesize, potentially increase i
fout.flush()
size = os.fstat(fout.fileno()).st_size
if size >= self.chunksize:
fout.close()
i += 1
fout = open(os.path.join(self.dest, '%s.%05d%s' % (self.name, i, self.ext)), 'w')
fout.write(l)
else:
fout.write(l)
else:
fout.write(l)
fout.close()
def human2bytes(s):
"""
Attempts to guess the string format based on default symbols
set and return the corresponding bytes as an integer.
When unable to recognize the format ValueError is raised.
>>> human2bytes('0 B')
0
>>> human2bytes('1 K')
1024
>>> human2bytes('1 M')
1048576
>>> human2bytes('1 Gi')
1073741824
>>> human2bytes('1 tera')
1099511627776
>>> human2bytes('0.5kilo')
512
>>> human2bytes('0.1 byte')
0
>>> human2bytes('1 k') # k is an alias for K
1024
>>> human2bytes('12 foo')
Traceback (most recent call last):
...
ValueError: can't interpret '12 foo'
"""
SYMBOLS = {
'customary': ('B', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'),
'customary_ext': ('byte', 'kilo', 'mega', 'giga', 'tera', 'peta', 'exa',
'zetta', 'iotta'),
'iec': ('Bi', 'Ki', 'Mi', 'Gi', 'Ti', 'Pi', 'Ei', 'Zi', 'Yi'),
'iec_ext': ('byte', 'kibi', 'mebi', 'gibi', 'tebi', 'pebi', 'exbi',
'zebi', 'yobi'),
}
init = s
num = ""
while s and s[0:1].isdigit() or s[0:1] == '.':
num += s[0]
s = s[1:]
num = float(num)
letter = s.strip()
for name, sset in SYMBOLS.items():
if letter in sset:
break
else:
if letter == 'k':
# treat 'k' as an alias for 'K' as per: http://goo.gl/kTQMs
sset = SYMBOLS['customary']
letter = letter.upper()
else:
raise ValueError("can't interpret %r" % init)
prefix = {sset[0]: 1}
for i, s in enumerate(sset[1:]):
prefix[s] = 1 << (i + 1) * 10
return int(num * prefix[letter])
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Split input file into pieces.')
# required
parser.add_argument('infile', help='Path to input file.')
parser.add_argument('outfolder', help='Path to output folder.')
# flags
parser.add_argument('-c', '--chunksize', default='1000M', help='Approximate size of file chunks.')
# required flag
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument('-d', '--delimiter', help='Delimiter for preserving text groups.')
group.add_argument('-l', '--lines', type=int, help='Number of lines to be considered a text group.')
args = parser.parse_args()
c = Chunker(args.infile, args.outfolder, chunksize=args.chunksize, delim=args.delimiter, lines=args.lines)