mirror of
https://github.com/facebook/sapling.git
synced 2024-10-07 07:17:55 +03:00
tests: pyfmt the 'f' utility
Summary: pyfmt it so changes can be formatted too. Reviewed By: DurhamG Differential Revision: D34835691 fbshipit-source-id: 97e31c5e0b514ec6651cb8e670473535db2a6802
This commit is contained in:
parent
f6a556ee7b
commit
8df6b9120d
136
eden/scm/tests/f
136
eden/scm/tests/f
@ -33,21 +33,25 @@ import re
|
||||
import sys
|
||||
|
||||
# Python 3 adapters
|
||||
ispy3 = (sys.version_info[0] >= 3)
|
||||
ispy3 = sys.version_info[0] >= 3
|
||||
if ispy3:
|
||||
|
||||
def iterbytes(s):
|
||||
for i in range(len(s)):
|
||||
yield s[i:i + 1]
|
||||
yield s[i : i + 1]
|
||||
|
||||
|
||||
else:
|
||||
iterbytes = iter
|
||||
|
||||
|
||||
def visit(opts, filenames, outfile):
|
||||
"""Process filenames in the way specified in opts, writing output to
|
||||
outfile."""
|
||||
for f in sorted(filenames):
|
||||
isstdin = f == '-'
|
||||
isstdin = f == "-"
|
||||
if not isstdin and not os.path.lexists(f):
|
||||
outfile.write(b'%s: file not found\n' % f.encode('utf-8'))
|
||||
outfile.write(b"%s: file not found\n" % f.encode("utf-8"))
|
||||
continue
|
||||
quiet = opts.quiet and not opts.recurse or isstdin
|
||||
isdir = os.path.isdir(f)
|
||||
@ -58,113 +62,119 @@ def visit(opts, filenames, outfile):
|
||||
facts = []
|
||||
if isfile:
|
||||
if opts.type:
|
||||
facts.append('file')
|
||||
facts.append("file")
|
||||
if opts.hexdump or opts.dump or opts.md5:
|
||||
with open(f, 'rb') as fp:
|
||||
with open(f, "rb") as fp:
|
||||
content = fp.read()
|
||||
elif islink:
|
||||
if opts.type:
|
||||
facts.append('link')
|
||||
facts.append("link")
|
||||
content = os.readlink(f).encode("utf-8")
|
||||
elif isstdin:
|
||||
content = getattr(sys.stdin, 'buffer', sys.stdin).read()
|
||||
content = getattr(sys.stdin, "buffer", sys.stdin).read()
|
||||
if opts.size:
|
||||
facts.append('size=%s' % len(content))
|
||||
facts.append("size=%s" % len(content))
|
||||
elif isdir:
|
||||
if opts.recurse or opts.type:
|
||||
dirfiles = glob.glob(f + '/*')
|
||||
facts.append('directory with %s files' % len(dirfiles))
|
||||
dirfiles = glob.glob(f + "/*")
|
||||
facts.append("directory with %s files" % len(dirfiles))
|
||||
elif opts.type:
|
||||
facts.append('type unknown')
|
||||
facts.append("type unknown")
|
||||
if not isstdin:
|
||||
stat = os.lstat(f)
|
||||
if opts.size and not isdir:
|
||||
facts.append('size=%s' % stat.st_size)
|
||||
facts.append("size=%s" % stat.st_size)
|
||||
if opts.mode and not islink:
|
||||
facts.append('mode=%o' % (stat.st_mode & 0o777))
|
||||
facts.append("mode=%o" % (stat.st_mode & 0o777))
|
||||
if opts.links:
|
||||
facts.append('links=%s' % stat.st_nlink)
|
||||
facts.append("links=%s" % stat.st_nlink)
|
||||
if opts.newer:
|
||||
# mtime might be in whole seconds so newer file might be same
|
||||
if stat.st_mtime >= os.stat(opts.newer).st_mtime:
|
||||
facts.append('newer than %s' % opts.newer)
|
||||
facts.append("newer than %s" % opts.newer)
|
||||
else:
|
||||
facts.append('older than %s' % opts.newer)
|
||||
facts.append("older than %s" % opts.newer)
|
||||
if opts.md5 and content is not None:
|
||||
h = hashlib.md5(content)
|
||||
facts.append('md5=%s' % h.hexdigest()[:opts.bytes])
|
||||
facts.append("md5=%s" % h.hexdigest()[: opts.bytes])
|
||||
if opts.sha1 and content is not None:
|
||||
h = hashlib.sha1(content)
|
||||
facts.append('sha1=%s' % h.hexdigest()[:opts.bytes])
|
||||
facts.append("sha1=%s" % h.hexdigest()[: opts.bytes])
|
||||
if isstdin:
|
||||
outfile.write(', '.join(facts).encode('utf-8') + b'\n')
|
||||
outfile.write(", ".join(facts).encode("utf-8") + b"\n")
|
||||
elif facts:
|
||||
outfile.write(b'%s: %s\n' % (f.encode('utf-8'), ', '.join(facts).encode('utf-8')))
|
||||
outfile.write(
|
||||
b"%s: %s\n" % (f.encode("utf-8"), ", ".join(facts).encode("utf-8"))
|
||||
)
|
||||
elif not quiet:
|
||||
outfile.write(b'%s:\n' % f.encode('utf-8'))
|
||||
outfile.write(b"%s:\n" % f.encode("utf-8"))
|
||||
if content is not None:
|
||||
chunk = content
|
||||
if not islink:
|
||||
if opts.lines:
|
||||
if opts.lines >= 0:
|
||||
chunk = b''.join(chunk.splitlines(True)[:opts.lines])
|
||||
chunk = b"".join(chunk.splitlines(True)[: opts.lines])
|
||||
else:
|
||||
chunk = b''.join(chunk.splitlines(True)[opts.lines:])
|
||||
chunk = b"".join(chunk.splitlines(True)[opts.lines :])
|
||||
if opts.bytes:
|
||||
if opts.bytes >= 0:
|
||||
chunk = chunk[:opts.bytes]
|
||||
chunk = chunk[: opts.bytes]
|
||||
else:
|
||||
chunk = chunk[opts.bytes:]
|
||||
chunk = chunk[opts.bytes :]
|
||||
if opts.hexdump:
|
||||
for i in range(0, len(chunk), 16):
|
||||
s = chunk[i:i + 16]
|
||||
outfile.write(b'%04x: %-47s |%s|\n' %
|
||||
(i, b' '.join(
|
||||
b'%02x' % ord(c) for c in iterbytes(s)),
|
||||
re.sub(b'[^ -~]', b'.', s)))
|
||||
s = chunk[i : i + 16]
|
||||
outfile.write(
|
||||
b"%04x: %-47s |%s|\n"
|
||||
% (
|
||||
i,
|
||||
b" ".join(b"%02x" % ord(c) for c in iterbytes(s)),
|
||||
re.sub(b"[^ -~]", b".", s),
|
||||
)
|
||||
)
|
||||
if opts.dump:
|
||||
if not quiet:
|
||||
outfile.write(b'>>>\n')
|
||||
outfile.write(b">>>\n")
|
||||
outfile.write(chunk)
|
||||
if not quiet:
|
||||
if chunk.endswith(b'\n'):
|
||||
outfile.write(b'<<<\n')
|
||||
if chunk.endswith(b"\n"):
|
||||
outfile.write(b"<<<\n")
|
||||
else:
|
||||
outfile.write(b'\n<<< no trailing newline\n')
|
||||
outfile.write(b"\n<<< no trailing newline\n")
|
||||
if opts.recurse and dirfiles:
|
||||
assert not isstdin
|
||||
visit(opts, dirfiles, outfile)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
parser = optparse.OptionParser("%prog [options] [filenames]")
|
||||
parser.add_option("-t", "--type", action="store_true",
|
||||
help="show file type (file or directory)")
|
||||
parser.add_option("-m", "--mode", action="store_true",
|
||||
help="show file mode")
|
||||
parser.add_option("-l", "--links", action="store_true",
|
||||
help="show number of links")
|
||||
parser.add_option("-s", "--size", action="store_true",
|
||||
help="show size of file")
|
||||
parser.add_option("-n", "--newer", action="store",
|
||||
help="check if file is newer (or same)")
|
||||
parser.add_option("-r", "--recurse", action="store_true",
|
||||
help="recurse into directories")
|
||||
parser.add_option("-S", "--sha1", action="store_true",
|
||||
help="show sha1 hash of the content")
|
||||
parser.add_option("-M", "--md5", action="store_true",
|
||||
help="show md5 hash of the content")
|
||||
parser.add_option("-D", "--dump", action="store_true",
|
||||
help="dump file content")
|
||||
parser.add_option("-H", "--hexdump", action="store_true",
|
||||
help="hexdump file content")
|
||||
parser.add_option("-B", "--bytes", type="int",
|
||||
help="number of characters to dump")
|
||||
parser.add_option("-L", "--lines", type="int",
|
||||
help="number of lines to dump")
|
||||
parser.add_option("-q", "--quiet", action="store_true",
|
||||
help="no default output")
|
||||
parser.add_option(
|
||||
"-t", "--type", action="store_true", help="show file type (file or directory)"
|
||||
)
|
||||
parser.add_option("-m", "--mode", action="store_true", help="show file mode")
|
||||
parser.add_option("-l", "--links", action="store_true", help="show number of links")
|
||||
parser.add_option("-s", "--size", action="store_true", help="show size of file")
|
||||
parser.add_option(
|
||||
"-n", "--newer", action="store", help="check if file is newer (or same)"
|
||||
)
|
||||
parser.add_option(
|
||||
"-r", "--recurse", action="store_true", help="recurse into directories"
|
||||
)
|
||||
parser.add_option(
|
||||
"-S", "--sha1", action="store_true", help="show sha1 hash of the content"
|
||||
)
|
||||
parser.add_option(
|
||||
"-M", "--md5", action="store_true", help="show md5 hash of the content"
|
||||
)
|
||||
parser.add_option("-D", "--dump", action="store_true", help="dump file content")
|
||||
parser.add_option(
|
||||
"-H", "--hexdump", action="store_true", help="hexdump file content"
|
||||
)
|
||||
parser.add_option("-B", "--bytes", type="int", help="number of characters to dump")
|
||||
parser.add_option("-L", "--lines", type="int", help="number of lines to dump")
|
||||
parser.add_option("-q", "--quiet", action="store_true", help="no default output")
|
||||
(opts, filenames) = parser.parse_args(sys.argv[1:])
|
||||
if not filenames:
|
||||
filenames = ['-']
|
||||
filenames = ["-"]
|
||||
|
||||
visit(opts, filenames, getattr(sys.stdout, 'buffer', sys.stdout))
|
||||
visit(opts, filenames, getattr(sys.stdout, "buffer", sys.stdout))
|
||||
|
Loading…
Reference in New Issue
Block a user