forked from WojciechMula/locatedb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
locatedb.py
322 lines (253 loc) · 6.57 KB
/
locatedb.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#!/usr/bin/env python
# -*- coding: iso-8859-2 -*-
# $Date: 2008-12-22 20:16:10 $, $Revision: 1.10 $
#
# Library/utility for reading and writing locatedb (v2.0) files.
# Read man locate, man locatedb for details.
#
# Author: Wojciech Mu³a
# e-mail: wojciech_mula@poczta.onet.pl
# www: http://0x80.pl
#
# License: BSD
#
__all__ = ["compress", "decompress"]
from os.path import join, commonprefix
from itertools import chain
def compress(paths):
"""Create locatedb file. Generator yields fragments of locatedb
file. Paths is any iterable object of strings."""
prev_path = ""
prev_n = 0
for path in chain(["LOCATE02"], paths):
comm = commonprefix([path, prev_path])
n = len(comm)
diff = n - prev_n
prev_n = n
prev_path = path
bytes = ""
# encode diff
if abs(diff) <= 127:
# diff in [-127..127]
bytes += chr(diff & 0xff)
else:
# diff larger
bytes += chr(0x80)
hilo = diff & 0xffff
bytes += chr((hilo >> 8) & 0xff)
bytes += chr(hilo & 0xff)
# write path fragment
bytes += path[n:]
# and finish one with null-char
bytes += "\x00"
yield bytes
def decompress_aux(file):
"""Decompress locatedb file (doesn't check if file is valid!).
Generator yields all paths, including first dummy-path
"LOCATE02". File have to be any file-like object that support
read."""
class Dummy: pass
curr = Dummy()
curr.buffer = ""
curr.idx = 0
def getc():
# XXX any improvements?
try:
c = curr.buffer[curr.idx]
curr.idx += 1
return c
except IndexError:
curr.buffer = file.read(8192)
curr.idx = 0
if not curr.buffer:
raise StopIteration
return getc()
current_path = ""
count = 0
while True:
diff = ord(getc())
# decoded diff
if diff == 0x80:
diff = ord(getc()) * 256 + ord(getc())
if diff >= 0x8000:
diff = -((~diff + 1) & 0xffff)
elif diff > 0x80:
diff = -((~diff + 1) & 0xff)
# read path fragment
path = ""
while True:
c = getc()
if c == "\x00":
break
else:
path += c
# get length of path prefix from previous iteration
count += diff
# and create real path
current_path = current_path[:count] + path
yield (count, diff, current_path)
def decompress(file):
"""Decompress locatedb file."""
iter = decompress_aux(file)
try:
first_entry = iter.next()
except StopIteration:
raise ValueError("Not a locate v.2 file")
if first_entry[2] != "LOCATE02":
raise ValueError("Not a locate v.2 file")
for item in iter:
yield item
usage_text = """\
Utility to read/write locatedb 2.0 files.
usage:
__name__ decompress infile
__name__ compress path outfile
__name__ compress-list infile outfile
__name__ join infile1 infile2 outfile
decompres - print on stdout decompressed contents of infile
compress - create locatedb file for given path
compress-list - create locatedb from arbitrary list of paths
that are readed from infile
join - join two locatedb files; no recoding is needed
If infile/outfile is "-" or "--" then file is associated with stdin/stdout.
"""
if __name__ == "__main__":
import os, os.path, sys
def usage(onerror=False):
name = sys.argv[0]
sys.stdout.write(usage_text.replace("__name__", name))
if onerror:
sys.exit(1)
else:
sys.exit(0)
def get_option(index, default=None):
try:
return sys.argv[index]
except IndexError:
if type(default) is not str:
usage(True)
else:
return default
def die(*info):
for text in info:
sys.stderr.write(str(text))
sys.stderr.write(" ")
else:
sys.stderr.write("\n")
sys.exit(1)
action = get_option(1).lower()
###
if action == "compress":
path = get_option(2)
outfile = get_option(3, "-")
if not os.path.exists(path):
die("Directory", path, "doesn't exists")
elif not os.path.isdir(path):
die("File", path, "is not directory")
if outfile in ["-", "--"]:
outfile = sys.stdout
elif os.path.exists(outfile):
die("File", outfile, "already exists")
else:
outfile = open(outfile, "wb")
def iter_dir_tree(root):
for dirname, directories, files in os.walk(root):
for file in files:
yield join(dirname, file)
for fragment in compress(iter_dir_tree(path)):
outfile.write(fragment)
###
elif action == "compress-list":
infile = get_option(2, "-")
outfile = get_option(3, "-")
if infile in ["-", "--"]:
infile = sys.stdin
else:
if not os.path.exists(infile):
die("File", infile, "doesn't exists")
infile = open(infile, "r")
if outfile in ["-", "--"]:
outfile = sys.stdout
else:
if os.path.exists(outfile):
die("File", outfile, "already exists")
outfile = open(outfile, "wb")
def iter_lines(file):
for line in file:
yield line[:-1]
for fragment in compress(iter_lines(infile)):
outfile.write(fragment)
###
elif action == "decompress":
infile = get_option(2, "-")
if infile in ["-", "--"]:
infile = sys.stdin
else:
if not os.path.exists(infile):
die("File", infile, "doesn't exists")
infile = open(infile, "rb")
for (_, _, path) in decompress(infile):
print path
###
elif action == "join":
infile1 = get_option(2)
infile2 = get_option(3, "-")
outfile = get_option(4, "-")
if not os.path.exists(infile1):
die("File", infile1, "doesn't exists")
infile1 = open(infile1, "rb")
if infile2 in ["-", "--"]:
infile2 = sys.stdin
else:
if not os.path.exists(infile2):
die("File", infile2, "doesn't exists")
infile2 = open(infile2, "rb")
if outfile in ["-", "--"]:
outfile = sys.stdout
else:
if os.path.exists(outfile):
die("File", outfile, "already exists")
outfile = open(outfile, "wb")
# get last count - a length of common prefix
count = None
for (count, _, _) in decompress(infile1):
pass
# 1. copy infile1
infile1.seek(0)
while True:
bytes = infile1.read(4096)
if not bytes:
break
else:
outfile.write(bytes)
# 2. put difference value -count
if count is not None:
diff = -count
bytes = ""
# encode diff
if abs(diff) <= 127:
# diff in [-127..127]
bytes += chr(diff & 0xff)
else:
# diff larger
bytes += chr(0x80)
hilo = diff & 0xffff
bytes += chr((hilo >> 8) & 0xff)
bytes += chr(hilo & 0xff)
else:
bytes = "\x00"
outfile.write(bytes)
# 3. copy infile2, but without first entry (LOCATE02) and without diff=0 of second entry
bytes = infile2.read(11)
if bytes != "\x00LOCATE02\x00\x00":
die(infile2.name, "is not a locate v 2.0 file")
while True:
bytes = infile2.read(4096)
if not bytes:
break
else:
outfile.write(bytes)
outfile.close()
else:
usage(True)
# vim: ts=4 sw=4 nowrap noexpandtab