comparison tests/f @ 34270:3db2365d43e4

tests: update `f` helper script to work on Python 3
author Augie Fackler <augie@google.com>
date Mon, 18 Sep 2017 13:37:32 -0400
parents c425b678df7c
children ea4d27aac557
comparison
equal deleted inserted replaced
34269:20f547806a4d 34270:3db2365d43e4
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 # 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)
45 islink = os.path.islink(f) 54 islink = os.path.islink(f)
46 isfile = os.path.isfile(f) and not islink 55 isfile = os.path.isfile(f) and not islink
55 elif islink: 64 elif islink:
56 if opts.type: 65 if opts.type:
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:
64 if opts.recurse or opts.type: 73 if opts.recurse or opts.type:
65 dirfiles = glob.glob(f + '/*') 74 dirfiles = glob.glob(f + '/*')
85 facts.append('md5=%s' % h.hexdigest()[:opts.bytes]) 94 facts.append('md5=%s' % h.hexdigest()[:opts.bytes])
86 if opts.sha1 and content is not None: 95 if opts.sha1 and content is not None:
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]
106 else: 115 else:
107 chunk = chunk[opts.bytes:] 116 chunk = chunk[opts.bytes:]
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)
126 136
127 if __name__ == "__main__": 137 if __name__ == "__main__":
154 help="no default output") 164 help="no default output")
155 (opts, filenames) = parser.parse_args(sys.argv[1:]) 165 (opts, filenames) = parser.parse_args(sys.argv[1:])
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))