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 from __future__ import print_function | |
6 | |
7 import argparse | |
8 import os | |
9 import stat | |
10 import sys | |
11 import time | |
12 | |
13 from .writer import ArDefaultWriter | |
14 from .reader import ArDefaultReader | |
15 | |
16 | |
17 class Timing(object): | |
18 def __init__(self, every): | |
19 self.every = every | |
20 self.start = time.time() | |
21 self.filecount = 0 | |
22 self.lastreport = 0 | |
23 | |
24 def inc(self): | |
25 self.filecount += 1 | |
26 if (self.filecount - self.lastreport) >= self.every: | |
27 self.report() | |
28 | |
29 def report(self): | |
30 if self.every: | |
31 t = time.time()-self.start | |
32 print("Took %f for %i files == %f files/second" % ( | |
M-A Ruel
2016/06/09 21:50:43
use single quotes
mithro
2016/06/14 12:15:55
Done.
| |
33 t, self.filecount, self.filecount/t), file=sys.stderr) | |
34 self.lastreport = self.filecount | |
35 | |
36 def __del__(self): | |
M-A Ruel
2016/06/09 21:50:43
Use __exit__ instead, don't overide __del__
we al
mithro
2016/06/14 12:15:55
They are doing slightly different things, the Timi
| |
37 self.report() | |
38 | |
39 | |
40 def create_cmd(filename, dirs, read_ahead=0, timing=False, verbose=True): | |
41 arfile = ArDefaultWriter(filename) | |
42 timing = Timing(timing) | |
M-A Ruel
2016/06/09 21:50:43
e.g.:
with Timing() as t:
mithro
2016/06/14 12:15:55
Obsolete?
| |
43 for path in dirs: | |
44 for dirpath, child_dirs, filenames in os.walk(path): | |
45 # In-place sort the child_dirs so we walk in lexicographical order | |
46 child_dirs.sort() | |
47 | |
48 for fn in sorted(filenames): | |
49 fp = os.path.join(dirpath, fn) | |
50 | |
51 if verbose: | |
52 print(fp, file=sys.stderr) | |
M-A Ruel
2016/06/09 21:50:43
logging.info()
mithro
2016/06/14 12:15:55
Logging adds a bunch of extra stuff. We want just
| |
53 | |
54 with open(fp, 'r') as f: | |
M-A Ruel
2016/06/09 21:50:43
'rb' is important
mithro
2016/06/14 12:15:55
Done.
| |
55 # If a file is small, it is cheaper to just read the file rather than | |
56 # doing a stat | |
57 data = f.read(read_ahead) | |
58 if len(data) < read_ahead: | |
59 arfile.add(fp, data) | |
60 else: | |
61 size = os.stat(fp).st_size | |
62 arfile.header(fp, size) | |
63 arfile.write(data) | |
64 arfile.write(f) | |
65 | |
66 timing.inc() | |
67 | |
68 arfile.close() | |
69 | |
70 | |
71 def list_cmd(filename, timing=False, check=False): | |
72 arfile = ArDefaultReader(filename, check) | |
73 timing = Timing(timing) | |
74 for fp, _, _ in arfile: | |
75 print(fp) | |
76 timing.inc() | |
77 | |
78 | |
79 def extract_cmd( | |
80 filename, timing=False, verbose=False, blocksize=1024*64, check=False): | |
81 arfile = ArDefaultReader(filename, check) | |
82 timing = Timing(timing) | |
83 for fp, size, ifd in arfile: | |
84 if verbose: | |
85 print(fp, file=sys.stderr) | |
86 | |
87 start = ifd.tell() | |
88 try: | |
89 os.makedirs(os.path.dirname(fp)) | |
90 except OSError: | |
91 pass | |
92 | |
93 with open(fp, 'w') as ofd: | |
94 written = 0 | |
95 while written < size: | |
96 readsize = min(blocksize, size-written) | |
97 ofd.write(ifd.read(readsize)) | |
98 | |
99 end = ifd.tell() | |
100 assert end-start == size | |
101 | |
102 | |
103 def main(name, args): | |
104 parser = argparse.ArgumentParser( | |
105 prog=name, | |
106 description='Command line tool for creating and extracting ar files.') | |
M-A Ruel
2016/06/09 21:50:43
One thing I like is to put this as the module docs
mithro
2016/06/14 12:15:55
Done.
| |
107 subparsers = parser.add_subparsers( | |
108 dest='mode', help='sub-command help') | |
109 | |
110 # Create command | |
111 parser_create = subparsers.add_parser( | |
112 'create', help='Create a new ar file') | |
113 parser_create.add_argument( | |
114 "-v", "--verbose", | |
115 action="store_true", | |
116 help="Print file names to stderr while creating the archive") | |
117 parser_create.add_argument( | |
118 "-r", "--read-ahead", | |
119 type=int, default=1024*64, | |
120 help="Amount of data to read-ahead before doing a stat.") | |
121 parser_create.add_argument( | |
122 "-f", "--filename", | |
123 type=argparse.FileType('w'), default=sys.stdout, | |
M-A Ruel
2016/06/09 21:50:43
'wb' and 'rb' below
mithro
2016/06/14 12:15:55
Done.
| |
124 help="ar file to use") | |
125 parser_create.add_argument( | |
126 'dirs', nargs='+', help='Directory or file to add to the ar file') | |
127 | |
128 # List command | |
129 parser_list = subparsers.add_parser('list', help='List a new ar file') | |
130 | |
131 # Extract command | |
132 parser_extract = subparsers.add_parser( | |
133 'extract', help='Extract an existing ar file to current directory') | |
134 parser_extract.add_argument( | |
135 "-v", "--verbose", | |
136 action="store_true", | |
137 help='Output file names as extracted.') | |
138 parser_extract.add_argument( | |
139 "-f", "--file", | |
140 type=argparse.FileType('r'), default=sys.stdin, | |
141 help="ar file to use") | |
142 | |
143 # Add to output commands | |
144 for p in parser_list, parser_extract: | |
145 p.add_argument( | |
146 "-f", "--filename", | |
147 type=argparse.FileType('r'), default=sys.stdin, | |
148 help="ar file to use") | |
149 p.add_argument( | |
150 "--check", | |
151 action="store_true", | |
152 help='Check the ar was created by ArDefaultWriter') | |
153 | |
154 # Add to all commands | |
155 for p in parser_create, parser_list, parser_extract: | |
156 p.add_argument( | |
157 "-t", "--timing", | |
158 type=int, default=0, | |
159 help='Output timing information every N files.') | |
160 | |
161 args = parser.parse_args(args) | |
162 mode = eval("%s_cmd" % args.mode) | |
M-A Ruel
2016/06/09 21:50:43
mode = getattr(sys.modules[__name__], args.mode +
mithro
2016/06/14 12:15:55
Done.
| |
163 del args.mode | |
164 mode(**args.__dict__) | |
165 | |
166 | |
167 if __name__ == "__main__": | |
168 main("artool", sys.argv[1:]) | |
OLD | NEW |