##// END OF EJS Templates
tests: update `f` helper script to work on Python 3
Augie Fackler -
r34271:3db2365d default
parent child Browse files
Show More
@@ -32,13 +32,22 b' import os'
32 import re
32 import re
33 import sys
33 import sys
34
34
35 # Python 3 adapters
36 ispy3 = (sys.version_info[0] >= 3)
37 if ispy3:
38 def iterbytes(s):
39 for i in range(len(s)):
40 yield s[i:i + 1]
41 else:
42 iterbytes = iter
43
35 def visit(opts, filenames, outfile):
44 def visit(opts, filenames, outfile):
36 """Process filenames in the way specified in opts, writing output to
45 """Process filenames in the way specified in opts, writing output to
37 outfile."""
46 outfile."""
38 for f in sorted(filenames):
47 for f in sorted(filenames):
39 isstdin = f == '-'
48 isstdin = f == '-'
40 if not isstdin and not os.path.lexists(f):
49 if not isstdin and not os.path.lexists(f):
41 outfile.write('%s: file not found\n' % f)
50 outfile.write(b'%s: file not found\n' % f.encode('utf-8'))
42 continue
51 continue
43 quiet = opts.quiet and not opts.recurse or isstdin
52 quiet = opts.quiet and not opts.recurse or isstdin
44 isdir = os.path.isdir(f)
53 isdir = os.path.isdir(f)
@@ -57,7 +66,7 b' def visit(opts, filenames, outfile):'
57 facts.append('link')
66 facts.append('link')
58 content = os.readlink(f)
67 content = os.readlink(f)
59 elif isstdin:
68 elif isstdin:
60 content = sys.stdin.read()
69 content = getattr(sys.stdin, 'buffer', sys.stdin).read()
61 if opts.size:
70 if opts.size:
62 facts.append('size=%s' % len(content))
71 facts.append('size=%s' % len(content))
63 elif isdir:
72 elif isdir:
@@ -87,19 +96,19 b' def visit(opts, filenames, outfile):'
87 h = hashlib.sha1(content)
96 h = hashlib.sha1(content)
88 facts.append('sha1=%s' % h.hexdigest()[:opts.bytes])
97 facts.append('sha1=%s' % h.hexdigest()[:opts.bytes])
89 if isstdin:
98 if isstdin:
90 outfile.write(', '.join(facts) + '\n')
99 outfile.write(b', '.join(facts) + b'\n')
91 elif facts:
100 elif facts:
92 outfile.write('%s: %s\n' % (f, ', '.join(facts)))
101 outfile.write(b'%s: %s\n' % (f.encode('utf-8'), b', '.join(facts)))
93 elif not quiet:
102 elif not quiet:
94 outfile.write('%s:\n' % f)
103 outfile.write(b'%s:\n' % f.encode('utf-8'))
95 if content is not None:
104 if content is not None:
96 chunk = content
105 chunk = content
97 if not islink:
106 if not islink:
98 if opts.lines:
107 if opts.lines:
99 if opts.lines >= 0:
108 if opts.lines >= 0:
100 chunk = ''.join(chunk.splitlines(True)[:opts.lines])
109 chunk = b''.join(chunk.splitlines(True)[:opts.lines])
101 else:
110 else:
102 chunk = ''.join(chunk.splitlines(True)[opts.lines:])
111 chunk = b''.join(chunk.splitlines(True)[opts.lines:])
103 if opts.bytes:
112 if opts.bytes:
104 if opts.bytes >= 0:
113 if opts.bytes >= 0:
105 chunk = chunk[:opts.bytes]
114 chunk = chunk[:opts.bytes]
@@ -108,18 +117,19 b' def visit(opts, filenames, outfile):'
108 if opts.hexdump:
117 if opts.hexdump:
109 for i in range(0, len(chunk), 16):
118 for i in range(0, len(chunk), 16):
110 s = chunk[i:i + 16]
119 s = chunk[i:i + 16]
111 outfile.write('%04x: %-47s |%s|\n' %
120 outfile.write(b'%04x: %-47s |%s|\n' %
112 (i, ' '.join('%02x' % ord(c) for c in s),
121 (i, b' '.join(
113 re.sub('[^ -~]', '.', s)))
122 b'%02x' % ord(c) for c in iterbytes(s)),
123 re.sub(b'[^ -~]', b'.', s)))
114 if opts.dump:
124 if opts.dump:
115 if not quiet:
125 if not quiet:
116 outfile.write('>>>\n')
126 outfile.write(b'>>>\n')
117 outfile.write(chunk)
127 outfile.write(chunk)
118 if not quiet:
128 if not quiet:
119 if chunk.endswith('\n'):
129 if chunk.endswith(b'\n'):
120 outfile.write('<<<\n')
130 outfile.write(b'<<<\n')
121 else:
131 else:
122 outfile.write('\n<<< no trailing newline\n')
132 outfile.write(b'\n<<< no trailing newline\n')
123 if opts.recurse and dirfiles:
133 if opts.recurse and dirfiles:
124 assert not isstdin
134 assert not isstdin
125 visit(opts, dirfiles, outfile)
135 visit(opts, dirfiles, outfile)
@@ -156,4 +166,4 b' if __name__ == "__main__":'
156 if not filenames:
166 if not filenames:
157 filenames = ['-']
167 filenames = ['-']
158
168
159 visit(opts, filenames, sys.stdout)
169 visit(opts, filenames, getattr(sys.stdout, 'buffer', sys.stdout))
General Comments 0
You need to be logged in to leave comments. Login now