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