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