comparison tests/f @ 43691:47ef023d0165

black: blacken scripts These scripts weren't blackened. I found these as part of adding script checking to test-check-format.t. # skip-blame black Differential Revision: https://phab.mercurial-scm.org/D7446
author Gregory Szorc <gregory.szorc@gmail.com>
date Sat, 16 Nov 2019 11:53:47 -0800
parents da1629c7dda1
children c102b704edb5
comparison
equal deleted inserted replaced
43690:15cccbacd5ce 43691:47ef023d0165
32 import os 32 import os
33 import re 33 import re
34 import sys 34 import sys
35 35
36 # Python 3 adapters 36 # Python 3 adapters
37 ispy3 = (sys.version_info[0] >= 3) 37 ispy3 = sys.version_info[0] >= 3
38 if ispy3: 38 if ispy3:
39
39 def iterbytes(s): 40 def iterbytes(s):
40 for i in range(len(s)): 41 for i in range(len(s)):
41 yield s[i:i + 1] 42 yield s[i : i + 1]
43
44
42 else: 45 else:
43 iterbytes = iter 46 iterbytes = iter
47
44 48
45 def visit(opts, filenames, outfile): 49 def visit(opts, filenames, outfile):
46 """Process filenames in the way specified in opts, writing output to 50 """Process filenames in the way specified in opts, writing output to
47 outfile.""" 51 outfile."""
48 for f in sorted(filenames): 52 for f in sorted(filenames):
86 if opts.links: 90 if opts.links:
87 facts.append(b'links=%d' % stat.st_nlink) 91 facts.append(b'links=%d' % stat.st_nlink)
88 if opts.newer: 92 if opts.newer:
89 # mtime might be in whole seconds so newer file might be same 93 # mtime might be in whole seconds so newer file might be same
90 if stat.st_mtime >= os.stat(opts.newer).st_mtime: 94 if stat.st_mtime >= os.stat(opts.newer).st_mtime:
91 facts.append(b'newer than %s' % opts.newer.encode( 95 facts.append(
92 'utf8', 'replace')) 96 b'newer than %s' % opts.newer.encode('utf8', 'replace')
97 )
93 else: 98 else:
94 facts.append(b'older than %s' % opts.newer.encode( 99 facts.append(
95 'utf8', 'replace')) 100 b'older than %s' % opts.newer.encode('utf8', 'replace')
101 )
96 if opts.md5 and content is not None: 102 if opts.md5 and content is not None:
97 h = hashlib.md5(content) 103 h = hashlib.md5(content)
98 facts.append(b'md5=%s' % binascii.hexlify(h.digest())[:opts.bytes]) 104 facts.append(b'md5=%s' % binascii.hexlify(h.digest())[: opts.bytes])
99 if opts.sha1 and content is not None: 105 if opts.sha1 and content is not None:
100 h = hashlib.sha1(content) 106 h = hashlib.sha1(content)
101 facts.append(b'sha1=%s' % binascii.hexlify(h.digest())[:opts.bytes]) 107 facts.append(
108 b'sha1=%s' % binascii.hexlify(h.digest())[: opts.bytes]
109 )
102 if opts.sha256 and content is not None: 110 if opts.sha256 and content is not None:
103 h = hashlib.sha256(content) 111 h = hashlib.sha256(content)
104 facts.append(b'sha256=%s' % 112 facts.append(
105 binascii.hexlify(h.digest())[:opts.bytes]) 113 b'sha256=%s' % binascii.hexlify(h.digest())[: opts.bytes]
114 )
106 if isstdin: 115 if isstdin:
107 outfile.write(b', '.join(facts) + b'\n') 116 outfile.write(b', '.join(facts) + b'\n')
108 elif facts: 117 elif facts:
109 outfile.write(b'%s: %s\n' % (f.encode('utf-8'), b', '.join(facts))) 118 outfile.write(b'%s: %s\n' % (f.encode('utf-8'), b', '.join(facts)))
110 elif not quiet: 119 elif not quiet:
112 if content is not None: 121 if content is not None:
113 chunk = content 122 chunk = content
114 if not islink: 123 if not islink:
115 if opts.lines: 124 if opts.lines:
116 if opts.lines >= 0: 125 if opts.lines >= 0:
117 chunk = b''.join(chunk.splitlines(True)[:opts.lines]) 126 chunk = b''.join(chunk.splitlines(True)[: opts.lines])
118 else: 127 else:
119 chunk = b''.join(chunk.splitlines(True)[opts.lines:]) 128 chunk = b''.join(chunk.splitlines(True)[opts.lines :])
120 if opts.bytes: 129 if opts.bytes:
121 if opts.bytes >= 0: 130 if opts.bytes >= 0:
122 chunk = chunk[:opts.bytes] 131 chunk = chunk[: opts.bytes]
123 else: 132 else:
124 chunk = chunk[opts.bytes:] 133 chunk = chunk[opts.bytes :]
125 if opts.hexdump: 134 if opts.hexdump:
126 for i in range(0, len(chunk), 16): 135 for i in range(0, len(chunk), 16):
127 s = chunk[i:i + 16] 136 s = chunk[i : i + 16]
128 outfile.write(b'%04x: %-47s |%s|\n' % 137 outfile.write(
129 (i, b' '.join( 138 b'%04x: %-47s |%s|\n'
130 b'%02x' % ord(c) for c in iterbytes(s)), 139 % (
131 re.sub(b'[^ -~]', b'.', s))) 140 i,
141 b' '.join(b'%02x' % ord(c) for c in iterbytes(s)),
142 re.sub(b'[^ -~]', b'.', s),
143 )
144 )
132 if opts.dump: 145 if opts.dump:
133 if not quiet: 146 if not quiet:
134 outfile.write(b'>>>\n') 147 outfile.write(b'>>>\n')
135 outfile.write(chunk) 148 outfile.write(chunk)
136 if not quiet: 149 if not quiet:
140 outfile.write(b'\n<<< no trailing newline\n') 153 outfile.write(b'\n<<< no trailing newline\n')
141 if opts.recurse and dirfiles: 154 if opts.recurse and dirfiles:
142 assert not isstdin 155 assert not isstdin
143 visit(opts, dirfiles, outfile) 156 visit(opts, dirfiles, outfile)
144 157
158
145 if __name__ == "__main__": 159 if __name__ == "__main__":
146 parser = optparse.OptionParser("%prog [options] [filenames]") 160 parser = optparse.OptionParser("%prog [options] [filenames]")
147 parser.add_option("-t", "--type", action="store_true", 161 parser.add_option(
148 help="show file type (file or directory)") 162 "-t",
149 parser.add_option("-m", "--mode", action="store_true", 163 "--type",
150 help="show file mode") 164 action="store_true",
151 parser.add_option("-l", "--links", action="store_true", 165 help="show file type (file or directory)",
152 help="show number of links") 166 )
153 parser.add_option("-s", "--size", action="store_true", 167 parser.add_option(
154 help="show size of file") 168 "-m", "--mode", action="store_true", help="show file mode"
155 parser.add_option("-n", "--newer", action="store", 169 )
156 help="check if file is newer (or same)") 170 parser.add_option(
157 parser.add_option("-r", "--recurse", action="store_true", 171 "-l", "--links", action="store_true", help="show number of links"
158 help="recurse into directories") 172 )
159 parser.add_option("-S", "--sha1", action="store_true", 173 parser.add_option(
160 help="show sha1 hash of the content") 174 "-s", "--size", action="store_true", help="show size of file"
161 parser.add_option("", "--sha256", action="store_true", 175 )
162 help="show sha256 hash of the content") 176 parser.add_option(
163 parser.add_option("-M", "--md5", action="store_true", 177 "-n", "--newer", action="store", help="check if file is newer (or same)"
164 help="show md5 hash of the content") 178 )
165 parser.add_option("-D", "--dump", action="store_true", 179 parser.add_option(
166 help="dump file content") 180 "-r", "--recurse", action="store_true", help="recurse into directories"
167 parser.add_option("-H", "--hexdump", action="store_true", 181 )
168 help="hexdump file content") 182 parser.add_option(
169 parser.add_option("-B", "--bytes", type="int", 183 "-S",
170 help="number of characters to dump") 184 "--sha1",
171 parser.add_option("-L", "--lines", type="int", 185 action="store_true",
172 help="number of lines to dump") 186 help="show sha1 hash of the content",
173 parser.add_option("-q", "--quiet", action="store_true", 187 )
174 help="no default output") 188 parser.add_option(
189 "",
190 "--sha256",
191 action="store_true",
192 help="show sha256 hash of the content",
193 )
194 parser.add_option(
195 "-M", "--md5", action="store_true", help="show md5 hash of the content"
196 )
197 parser.add_option(
198 "-D", "--dump", action="store_true", help="dump file content"
199 )
200 parser.add_option(
201 "-H", "--hexdump", action="store_true", help="hexdump file content"
202 )
203 parser.add_option(
204 "-B", "--bytes", type="int", help="number of characters to dump"
205 )
206 parser.add_option(
207 "-L", "--lines", type="int", help="number of lines to dump"
208 )
209 parser.add_option(
210 "-q", "--quiet", action="store_true", help="no default output"
211 )
175 (opts, filenames) = parser.parse_args(sys.argv[1:]) 212 (opts, filenames) = parser.parse_args(sys.argv[1:])
176 if not filenames: 213 if not filenames:
177 filenames = ['-'] 214 filenames = ['-']
178 215
179 visit(opts, filenames, getattr(sys.stdout, 'buffer', sys.stdout)) 216 visit(opts, filenames, getattr(sys.stdout, 'buffer', sys.stdout))