##// END OF EJS Templates
py3: use open() instead of file() constructor...
Pulkit Goyal -
r32898:c425b678 default
parent child Browse files
Show More
@@ -1,159 +1,159 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 glob
28 import glob
29 import hashlib
29 import hashlib
30 import optparse
30 import optparse
31 import os
31 import os
32 import re
32 import re
33 import sys
33 import sys
34
34
35 def visit(opts, filenames, outfile):
35 def visit(opts, filenames, outfile):
36 """Process filenames in the way specified in opts, writing output to
36 """Process filenames in the way specified in opts, writing output to
37 outfile."""
37 outfile."""
38 for f in sorted(filenames):
38 for f in sorted(filenames):
39 isstdin = f == '-'
39 isstdin = f == '-'
40 if not isstdin and not os.path.lexists(f):
40 if not isstdin and not os.path.lexists(f):
41 outfile.write('%s: file not found\n' % f)
41 outfile.write('%s: file not found\n' % f)
42 continue
42 continue
43 quiet = opts.quiet and not opts.recurse or isstdin
43 quiet = opts.quiet and not opts.recurse or isstdin
44 isdir = os.path.isdir(f)
44 isdir = os.path.isdir(f)
45 islink = os.path.islink(f)
45 islink = os.path.islink(f)
46 isfile = os.path.isfile(f) and not islink
46 isfile = os.path.isfile(f) and not islink
47 dirfiles = None
47 dirfiles = None
48 content = None
48 content = None
49 facts = []
49 facts = []
50 if isfile:
50 if isfile:
51 if opts.type:
51 if opts.type:
52 facts.append('file')
52 facts.append('file')
53 if opts.hexdump or opts.dump or opts.md5:
53 if opts.hexdump or opts.dump or opts.md5:
54 content = file(f, 'rb').read()
54 content = open(f, 'rb').read()
55 elif islink:
55 elif islink:
56 if opts.type:
56 if opts.type:
57 facts.append('link')
57 facts.append('link')
58 content = os.readlink(f)
58 content = os.readlink(f)
59 elif isstdin:
59 elif isstdin:
60 content = sys.stdin.read()
60 content = sys.stdin.read()
61 if opts.size:
61 if opts.size:
62 facts.append('size=%s' % len(content))
62 facts.append('size=%s' % len(content))
63 elif isdir:
63 elif isdir:
64 if opts.recurse or opts.type:
64 if opts.recurse or opts.type:
65 dirfiles = glob.glob(f + '/*')
65 dirfiles = glob.glob(f + '/*')
66 facts.append('directory with %s files' % len(dirfiles))
66 facts.append('directory with %s files' % len(dirfiles))
67 elif opts.type:
67 elif opts.type:
68 facts.append('type unknown')
68 facts.append('type unknown')
69 if not isstdin:
69 if not isstdin:
70 stat = os.lstat(f)
70 stat = os.lstat(f)
71 if opts.size and not isdir:
71 if opts.size and not isdir:
72 facts.append('size=%s' % stat.st_size)
72 facts.append('size=%s' % stat.st_size)
73 if opts.mode and not islink:
73 if opts.mode and not islink:
74 facts.append('mode=%o' % (stat.st_mode & 0o777))
74 facts.append('mode=%o' % (stat.st_mode & 0o777))
75 if opts.links:
75 if opts.links:
76 facts.append('links=%s' % stat.st_nlink)
76 facts.append('links=%s' % stat.st_nlink)
77 if opts.newer:
77 if opts.newer:
78 # mtime might be in whole seconds so newer file might be same
78 # mtime might be in whole seconds so newer file might be same
79 if stat.st_mtime >= os.stat(opts.newer).st_mtime:
79 if stat.st_mtime >= os.stat(opts.newer).st_mtime:
80 facts.append('newer than %s' % opts.newer)
80 facts.append('newer than %s' % opts.newer)
81 else:
81 else:
82 facts.append('older than %s' % opts.newer)
82 facts.append('older than %s' % opts.newer)
83 if opts.md5 and content is not None:
83 if opts.md5 and content is not None:
84 h = hashlib.md5(content)
84 h = hashlib.md5(content)
85 facts.append('md5=%s' % h.hexdigest()[:opts.bytes])
85 facts.append('md5=%s' % h.hexdigest()[:opts.bytes])
86 if opts.sha1 and content is not None:
86 if opts.sha1 and content is not None:
87 h = hashlib.sha1(content)
87 h = hashlib.sha1(content)
88 facts.append('sha1=%s' % h.hexdigest()[:opts.bytes])
88 facts.append('sha1=%s' % h.hexdigest()[:opts.bytes])
89 if isstdin:
89 if isstdin:
90 outfile.write(', '.join(facts) + '\n')
90 outfile.write(', '.join(facts) + '\n')
91 elif facts:
91 elif facts:
92 outfile.write('%s: %s\n' % (f, ', '.join(facts)))
92 outfile.write('%s: %s\n' % (f, ', '.join(facts)))
93 elif not quiet:
93 elif not quiet:
94 outfile.write('%s:\n' % f)
94 outfile.write('%s:\n' % f)
95 if content is not None:
95 if content is not None:
96 chunk = content
96 chunk = content
97 if not islink:
97 if not islink:
98 if opts.lines:
98 if opts.lines:
99 if opts.lines >= 0:
99 if opts.lines >= 0:
100 chunk = ''.join(chunk.splitlines(True)[:opts.lines])
100 chunk = ''.join(chunk.splitlines(True)[:opts.lines])
101 else:
101 else:
102 chunk = ''.join(chunk.splitlines(True)[opts.lines:])
102 chunk = ''.join(chunk.splitlines(True)[opts.lines:])
103 if opts.bytes:
103 if opts.bytes:
104 if opts.bytes >= 0:
104 if opts.bytes >= 0:
105 chunk = chunk[:opts.bytes]
105 chunk = chunk[:opts.bytes]
106 else:
106 else:
107 chunk = chunk[opts.bytes:]
107 chunk = chunk[opts.bytes:]
108 if opts.hexdump:
108 if opts.hexdump:
109 for i in range(0, len(chunk), 16):
109 for i in range(0, len(chunk), 16):
110 s = chunk[i:i + 16]
110 s = chunk[i:i + 16]
111 outfile.write('%04x: %-47s |%s|\n' %
111 outfile.write('%04x: %-47s |%s|\n' %
112 (i, ' '.join('%02x' % ord(c) for c in s),
112 (i, ' '.join('%02x' % ord(c) for c in s),
113 re.sub('[^ -~]', '.', s)))
113 re.sub('[^ -~]', '.', s)))
114 if opts.dump:
114 if opts.dump:
115 if not quiet:
115 if not quiet:
116 outfile.write('>>>\n')
116 outfile.write('>>>\n')
117 outfile.write(chunk)
117 outfile.write(chunk)
118 if not quiet:
118 if not quiet:
119 if chunk.endswith('\n'):
119 if chunk.endswith('\n'):
120 outfile.write('<<<\n')
120 outfile.write('<<<\n')
121 else:
121 else:
122 outfile.write('\n<<< no trailing newline\n')
122 outfile.write('\n<<< no trailing newline\n')
123 if opts.recurse and dirfiles:
123 if opts.recurse and dirfiles:
124 assert not isstdin
124 assert not isstdin
125 visit(opts, dirfiles, outfile)
125 visit(opts, dirfiles, outfile)
126
126
127 if __name__ == "__main__":
127 if __name__ == "__main__":
128 parser = optparse.OptionParser("%prog [options] [filenames]")
128 parser = optparse.OptionParser("%prog [options] [filenames]")
129 parser.add_option("-t", "--type", action="store_true",
129 parser.add_option("-t", "--type", action="store_true",
130 help="show file type (file or directory)")
130 help="show file type (file or directory)")
131 parser.add_option("-m", "--mode", action="store_true",
131 parser.add_option("-m", "--mode", action="store_true",
132 help="show file mode")
132 help="show file mode")
133 parser.add_option("-l", "--links", action="store_true",
133 parser.add_option("-l", "--links", action="store_true",
134 help="show number of links")
134 help="show number of links")
135 parser.add_option("-s", "--size", action="store_true",
135 parser.add_option("-s", "--size", action="store_true",
136 help="show size of file")
136 help="show size of file")
137 parser.add_option("-n", "--newer", action="store",
137 parser.add_option("-n", "--newer", action="store",
138 help="check if file is newer (or same)")
138 help="check if file is newer (or same)")
139 parser.add_option("-r", "--recurse", action="store_true",
139 parser.add_option("-r", "--recurse", action="store_true",
140 help="recurse into directories")
140 help="recurse into directories")
141 parser.add_option("-S", "--sha1", action="store_true",
141 parser.add_option("-S", "--sha1", action="store_true",
142 help="show sha1 hash of the content")
142 help="show sha1 hash of the content")
143 parser.add_option("-M", "--md5", action="store_true",
143 parser.add_option("-M", "--md5", action="store_true",
144 help="show md5 hash of the content")
144 help="show md5 hash of the content")
145 parser.add_option("-D", "--dump", action="store_true",
145 parser.add_option("-D", "--dump", action="store_true",
146 help="dump file content")
146 help="dump file content")
147 parser.add_option("-H", "--hexdump", action="store_true",
147 parser.add_option("-H", "--hexdump", action="store_true",
148 help="hexdump file content")
148 help="hexdump file content")
149 parser.add_option("-B", "--bytes", type="int",
149 parser.add_option("-B", "--bytes", type="int",
150 help="number of characters to dump")
150 help="number of characters to dump")
151 parser.add_option("-L", "--lines", type="int",
151 parser.add_option("-L", "--lines", type="int",
152 help="number of lines to dump")
152 help="number of lines to dump")
153 parser.add_option("-q", "--quiet", action="store_true",
153 parser.add_option("-q", "--quiet", action="store_true",
154 help="no default output")
154 help="no default output")
155 (opts, filenames) = parser.parse_args(sys.argv[1:])
155 (opts, filenames) = parser.parse_args(sys.argv[1:])
156 if not filenames:
156 if not filenames:
157 filenames = ['-']
157 filenames = ['-']
158
158
159 visit(opts, filenames, sys.stdout)
159 visit(opts, filenames, sys.stdout)
General Comments 0
You need to be logged in to leave comments. Login now