OLD | NEW |
---|---|
(Empty) | |
1 # Copyright 2016 The LUCI Authors. All rights reserved. | |
2 # Use of this source code is governed under the Apache License, Version 2.0 | |
3 # that can be found in the LICENSE file. | |
4 | |
5 import os | |
6 import struct | |
7 | |
8 from .writer import ArDefaultWriter | |
9 | |
10 def assert_eq(e, a): | |
11 """ | |
12 >>> assert_eq(1, 1) | |
13 >>> assert_eq(1, 2) | |
14 Traceback (most recent call last): | |
15 ... | |
16 AssertionError: 1 (1) != 2 (2) | |
17 >>> assert_eq(1, "1") | |
18 Traceback (most recent call last): | |
19 ... | |
20 AssertionError: 1 (1) != 1 ('1') | |
21 """ | |
22 assert e == a, "%s (%r) != %s (%r)" % (e, e, a, a) | |
23 | |
24 | |
25 class ArDefaultReader(object): | |
26 """Read an ar archive created with default values.""" | |
27 | |
28 # BSD Variant AR file | |
29 | |
30 def __init__(self, ibuf, check=False): | |
31 self.check = check | |
32 self.ibuf = ibuf | |
33 assert self.ibuf.read(8) == "!<arch>\n" | |
34 | |
35 def __iter__(self): | |
36 # We generate the default values because comparing strings is cheaper then | |
37 # parsing them. | |
38 check = self.check | |
M-A Ruel
2016/06/09 21:50:44
why?
mithro
2016/06/14 12:15:55
Speed. Looking up check in self repeatedly is quit
M-A Ruel
2016/06/14 13:26:16
We do not use python -O in practice so I don't kno
| |
39 if check: | |
40 dmodtime = "%-6i" % ArDefaultWriter.DEFAULT_MODTIME | |
41 duid = "%-6i" % ArDefaultWriter.DEFAULT_USER | |
42 dgid = "%-6i" % ArDefaultWriter.DEFAULT_GROUP | |
43 dmode = "%-6i" % ArDefaultWriter.DEFAULT_MODE | |
44 | |
45 while True: | |
46 header = self.ibuf.read(60) | |
47 if header == '': | |
M-A Ruel
2016/06/09 21:50:44
if not header:
mithro
2016/06/14 12:15:55
Done.
| |
48 return | |
49 | |
50 name, modtime, uid, gid, mode, size, magic = struct.unpack( | |
51 "16s 12s 6s 6s 8s 10s 2s", header) | |
52 | |
53 assert name.startswith("#1/"), name | |
M-A Ruel
2016/06/09 21:50:44
use a real check + raise ValueError()
mithro
2016/06/14 12:15:55
Python -O causes these asserts to totally disappea
| |
54 filename_size = int(name[3:]) | |
55 if check: | |
56 assert_eq(dmodtime, modtime) | |
M-A Ruel
2016/06/09 21:50:44
same
mithro
2016/06/14 12:15:55
See above.
| |
57 assert_eq(duid, uid) | |
58 assert_eq(dgid, gid) | |
59 assert_eq(dmode, mode) | |
60 data_size = int(size) | |
61 assert_eq("\x60\n", magic) | |
62 | |
63 file_size = data_size - filename_size | |
64 fp = self.ibuf.read(filename_size) | |
65 | |
66 start = self.ibuf.tell() | |
67 yield fp, file_size, self.ibuf | |
68 end = self.ibuf.tell() | |
69 | |
70 read = end - start | |
71 # If the reader didn't touch the input buffer, seek past the file. | |
72 if read == 0: | |
73 self.ibuf.seek(file_size, os.SEEK_CUR) | |
74 else: | |
75 assert read == file_size | |
76 | |
77 if data_size % 2 != 0: | |
78 padding = self.ibuf.read(1) | |
79 assert padding == "\n" | |
80 | |
81 | |
82 if __name__ == "__main__": | |
83 import doctest | |
84 doctest.testmod() | |
OLD | NEW |