##// END OF EJS Templates
tests: make 'f' utility import hashlib unconditionally...
Yuya Nishihara -
r29233:318534bb default
parent child Browse files
Show More
@@ -1,164 +1,159
1 1 #!/usr/bin/env python
2 2
3 3 """
4 4 Utility for inspecting files in various ways.
5 5
6 6 This tool is like the collection of tools found in a unix environment but are
7 7 cross platform and stable and suitable for our needs in the test suite.
8 8
9 9 This can be used instead of tools like:
10 10 [
11 11 dd
12 12 find
13 13 head
14 14 hexdump
15 15 ls
16 16 md5sum
17 17 readlink
18 18 sha1sum
19 19 stat
20 20 tail
21 21 test
22 22 readlink.py
23 23 md5sum.py
24 24 """
25 25
26 26 from __future__ import absolute_import
27 27
28 28 import glob
29 import hashlib
29 30 import optparse
30 31 import os
31 32 import re
32 33 import sys
33 34
34 35 def visit(opts, filenames, outfile):
35 36 """Process filenames in the way specified in opts, writing output to
36 37 outfile."""
37 38 for f in sorted(filenames):
38 39 isstdin = f == '-'
39 40 if not isstdin and not os.path.lexists(f):
40 41 outfile.write('%s: file not found\n' % f)
41 42 continue
42 43 quiet = opts.quiet and not opts.recurse or isstdin
43 44 isdir = os.path.isdir(f)
44 45 islink = os.path.islink(f)
45 46 isfile = os.path.isfile(f) and not islink
46 47 dirfiles = None
47 48 content = None
48 49 facts = []
49 50 if isfile:
50 51 if opts.type:
51 52 facts.append('file')
52 53 if opts.hexdump or opts.dump or opts.md5:
53 54 content = file(f, 'rb').read()
54 55 elif islink:
55 56 if opts.type:
56 57 facts.append('link')
57 58 content = os.readlink(f)
58 59 elif isstdin:
59 60 content = sys.stdin.read()
60 61 if opts.size:
61 62 facts.append('size=%s' % len(content))
62 63 elif isdir:
63 64 if opts.recurse or opts.type:
64 65 dirfiles = glob.glob(f + '/*')
65 66 facts.append('directory with %s files' % len(dirfiles))
66 67 elif opts.type:
67 68 facts.append('type unknown')
68 69 if not isstdin:
69 70 stat = os.lstat(f)
70 71 if opts.size and not isdir:
71 72 facts.append('size=%s' % stat.st_size)
72 73 if opts.mode and not islink:
73 74 facts.append('mode=%o' % (stat.st_mode & 0o777))
74 75 if opts.links:
75 76 facts.append('links=%s' % stat.st_nlink)
76 77 if opts.newer:
77 78 # mtime might be in whole seconds so newer file might be same
78 79 if stat.st_mtime >= os.stat(opts.newer).st_mtime:
79 80 facts.append('newer than %s' % opts.newer)
80 81 else:
81 82 facts.append('older than %s' % opts.newer)
82 83 if opts.md5 and content is not None:
83 try:
84 from hashlib import md5
85 except ImportError:
86 from md5 import md5
87 facts.append('md5=%s' % md5(content).hexdigest()[:opts.bytes])
84 h = hashlib.md5(content)
85 facts.append('md5=%s' % h.hexdigest()[:opts.bytes])
88 86 if opts.sha1 and content is not None:
89 try:
90 from hashlib import sha1
91 except ImportError:
92 from sha import sha as sha1
93 facts.append('sha1=%s' % sha1(content).hexdigest()[:opts.bytes])
87 h = hashlib.sha1(content)
88 facts.append('sha1=%s' % h.hexdigest()[:opts.bytes])
94 89 if isstdin:
95 90 outfile.write(', '.join(facts) + '\n')
96 91 elif facts:
97 92 outfile.write('%s: %s\n' % (f, ', '.join(facts)))
98 93 elif not quiet:
99 94 outfile.write('%s:\n' % f)
100 95 if content is not None:
101 96 chunk = content
102 97 if not islink:
103 98 if opts.lines:
104 99 if opts.lines >= 0:
105 100 chunk = ''.join(chunk.splitlines(True)[:opts.lines])
106 101 else:
107 102 chunk = ''.join(chunk.splitlines(True)[opts.lines:])
108 103 if opts.bytes:
109 104 if opts.bytes >= 0:
110 105 chunk = chunk[:opts.bytes]
111 106 else:
112 107 chunk = chunk[opts.bytes:]
113 108 if opts.hexdump:
114 109 for i in range(0, len(chunk), 16):
115 110 s = chunk[i:i + 16]
116 111 outfile.write('%04x: %-47s |%s|\n' %
117 112 (i, ' '.join('%02x' % ord(c) for c in s),
118 113 re.sub('[^ -~]', '.', s)))
119 114 if opts.dump:
120 115 if not quiet:
121 116 outfile.write('>>>\n')
122 117 outfile.write(chunk)
123 118 if not quiet:
124 119 if chunk.endswith('\n'):
125 120 outfile.write('<<<\n')
126 121 else:
127 122 outfile.write('\n<<< no trailing newline\n')
128 123 if opts.recurse and dirfiles:
129 124 assert not isstdin
130 125 visit(opts, dirfiles, outfile)
131 126
132 127 if __name__ == "__main__":
133 128 parser = optparse.OptionParser("%prog [options] [filenames]")
134 129 parser.add_option("-t", "--type", action="store_true",
135 130 help="show file type (file or directory)")
136 131 parser.add_option("-m", "--mode", action="store_true",
137 132 help="show file mode")
138 133 parser.add_option("-l", "--links", action="store_true",
139 134 help="show number of links")
140 135 parser.add_option("-s", "--size", action="store_true",
141 136 help="show size of file")
142 137 parser.add_option("-n", "--newer", action="store",
143 138 help="check if file is newer (or same)")
144 139 parser.add_option("-r", "--recurse", action="store_true",
145 140 help="recurse into directories")
146 141 parser.add_option("-S", "--sha1", action="store_true",
147 142 help="show sha1 hash of the content")
148 143 parser.add_option("-M", "--md5", action="store_true",
149 144 help="show md5 hash of the content")
150 145 parser.add_option("-D", "--dump", action="store_true",
151 146 help="dump file content")
152 147 parser.add_option("-H", "--hexdump", action="store_true",
153 148 help="hexdump file content")
154 149 parser.add_option("-B", "--bytes", type="int",
155 150 help="number of characters to dump")
156 151 parser.add_option("-L", "--lines", type="int",
157 152 help="number of lines to dump")
158 153 parser.add_option("-q", "--quiet", action="store_true",
159 154 help="no default output")
160 155 (opts, filenames) = parser.parse_args(sys.argv[1:])
161 156 if not filenames:
162 157 filenames = ['-']
163 158
164 159 visit(opts, filenames, sys.stdout)
General Comments 0
You need to be logged in to leave comments. Login now