| 1 |
#!/usr/bin/env python3 |
| 2 |
|
| 3 |
""" |
| 4 |
Utility for inspecting files in various ways. |
| 5 |
|
| 6 |
This tool is like the collection of tools found in a unix environment but are |
| 7 |
cross platform and stable and suitable for our needs in the test suite. |
| 8 |
|
| 9 |
This can be used instead of tools like: |
| 10 |
[ |
| 11 |
dd |
| 12 |
find |
| 13 |
head |
| 14 |
hexdump |
| 15 |
ls |
| 16 |
md5sum |
| 17 |
readlink |
| 18 |
sha1sum |
| 19 |
stat |
| 20 |
tail |
| 21 |
test |
| 22 |
readlink.py |
| 23 |
md5sum.py |
| 24 |
""" |
| 25 |
|
| 26 |
|
| 27 |
import binascii |
| 28 |
import glob |
| 29 |
import hashlib |
| 30 |
import optparse |
| 31 |
import os |
| 32 |
import re |
| 33 |
import sys |
| 34 |
|
| 35 |
|
| 36 |
def iterbytes(s): |
| 37 |
for i in range(len(s)): |
| 38 |
yield s[i : i + 1] |
| 39 |
|
| 40 |
|
| 41 |
def visit(opts, filenames, outfile): |
| 42 |
"""Process filenames in the way specified in opts, writing output to |
| 43 |
outfile.""" |
| 44 |
for f in sorted(filenames): |
| 45 |
isstdin = f == '-' |
| 46 |
if not isstdin and not os.path.lexists(f): |
| 47 |
outfile.write(b'%s: file not found\n' % f.encode('utf-8')) |
| 48 |
continue |
| 49 |
quiet = opts.quiet and not opts.recurse or isstdin |
| 50 |
isdir = os.path.isdir(f) |
| 51 |
islink = os.path.islink(f) |
| 52 |
isfile = os.path.isfile(f) and not islink |
| 53 |
dirfiles = None |
| 54 |
content = None |
| 55 |
facts = [] |
| 56 |
if isfile: |
| 57 |
if opts.type: |
| 58 |
facts.append(b'file') |
| 59 |
needs_reading = ( |
| 60 |
opts.hexdump, |
| 61 |
opts.dump, |
| 62 |
opts.md5, |
| 63 |
opts.sha1, |
| 64 |
opts.raw_sha1, |
| 65 |
opts.sha256, |
| 66 |
) |
| 67 |
|
| 68 |
if any(needs_reading): |
| 69 |
with open(f, 'rb') as fobj: |
| 70 |
content = fobj.read() |
| 71 |
elif islink: |
| 72 |
if opts.type: |
| 73 |
facts.append(b'link') |
| 74 |
content = os.readlink(f).encode('utf8') |
| 75 |
elif isstdin: |
| 76 |
content = getattr(sys.stdin, 'buffer', sys.stdin).read() |
| 77 |
if opts.size: |
| 78 |
facts.append(b'size=%d' % len(content)) |
| 79 |
elif isdir: |
| 80 |
if opts.recurse or opts.type: |
| 81 |
dirfiles = glob.glob(f + '/*') |
| 82 |
facts.append(b'directory with %d files' % len(dirfiles)) |
| 83 |
elif opts.type: |
| 84 |
facts.append(b'type unknown') |
| 85 |
if not isstdin: |
| 86 |
stat = os.lstat(f) |
| 87 |
if opts.size and not isdir: |
| 88 |
facts.append(b'size=%d' % stat.st_size) |
| 89 |
if opts.mode and not islink: |
| 90 |
facts.append(b'mode=%o' % (stat.st_mode & 0o777)) |
| 91 |
if opts.links: |
| 92 |
facts.append(b'links=%d' % stat.st_nlink) |
| 93 |
if opts.newer: |
| 94 |
# mtime might be in whole seconds so newer file might be same |
| 95 |
if stat.st_mtime >= os.stat(opts.newer).st_mtime: |
| 96 |
facts.append( |
| 97 |
b'newer than %s' % opts.newer.encode('utf8', 'replace') |
| 98 |
) |
| 99 |
else: |
| 100 |
facts.append( |
| 101 |
b'older than %s' % opts.newer.encode('utf8', 'replace') |
| 102 |
) |
| 103 |
if opts.line_count and content is not None: |
| 104 |
facts.append(b'line-count=%d' % len(content.splitlines())) |
| 105 |
if opts.md5 and content is not None: |
| 106 |
h = hashlib.md5(content) |
| 107 |
facts.append(b'md5=%s' % binascii.hexlify(h.digest())[: opts.bytes]) |
| 108 |
if opts.raw_sha1 and content is not None: |
| 109 |
h = hashlib.sha1(content) |
| 110 |
facts.append(b'raw-sha1=%s' % h.digest()[: opts.bytes]) |
| 111 |
if opts.sha1 and content is not None: |
| 112 |
h = hashlib.sha1(content) |
| 113 |
facts.append( |
| 114 |
b'sha1=%s' % binascii.hexlify(h.digest())[: opts.bytes] |
| 115 |
) |
| 116 |
if opts.sha256 and content is not None: |
| 117 |
h = hashlib.sha256(content) |
| 118 |
facts.append( |
| 119 |
b'sha256=%s' % binascii.hexlify(h.digest())[: opts.bytes] |
| 120 |
) |
| 121 |
if isstdin: |
| 122 |
outfile.write(b', '.join(facts) + b'\n') |
| 123 |
elif facts: |
| 124 |
outfile.write(b'%s: %s\n' % (f.encode('utf-8'), b', '.join(facts))) |
| 125 |
elif not quiet: |
| 126 |
outfile.write(b'%s:\n' % f.encode('utf-8')) |
| 127 |
if content is not None: |
| 128 |
chunk = content |
| 129 |
if not islink: |
| 130 |
if opts.lines: |
| 131 |
if opts.lines >= 0: |
| 132 |
chunk = b''.join(chunk.splitlines(True)[: opts.lines]) |
| 133 |
else: |
| 134 |
chunk = b''.join(chunk.splitlines(True)[opts.lines :]) |
| 135 |
if opts.bytes: |
| 136 |
if opts.bytes >= 0: |
| 137 |
chunk = chunk[: opts.bytes] |
| 138 |
else: |
| 139 |
chunk = chunk[opts.bytes :] |
| 140 |
if opts.hexdump: |
| 141 |
for i in range(0, len(chunk), 16): |
| 142 |
s = chunk[i : i + 16] |
| 143 |
outfile.write( |
| 144 |
b'%04x: %-47s |%s|\n' |
| 145 |
% ( |
| 146 |
i, |
| 147 |
b' '.join(b'%02x' % ord(c) for c in iterbytes(s)), |
| 148 |
re.sub(b'[^ -~]', b'.', s), |
| 149 |
) |
| 150 |
) |
| 151 |
if opts.dump: |
| 152 |
if not quiet: |
| 153 |
outfile.write(b'>>>\n') |
| 154 |
outfile.write(chunk) |
| 155 |
if not quiet: |
| 156 |
if chunk.endswith(b'\n'): |
| 157 |
outfile.write(b'<<<\n') |
| 158 |
else: |
| 159 |
outfile.write(b'\n<<< no trailing newline\n') |
| 160 |
if opts.recurse and dirfiles: |
| 161 |
assert not isstdin |
| 162 |
visit(opts, dirfiles, outfile) |
| 163 |
|
| 164 |
|
| 165 |
if __name__ == "__main__": |
| 166 |
parser = optparse.OptionParser("%prog [options] [filenames]") |
| 167 |
parser.add_option( |
| 168 |
"-t", |
| 169 |
"--type", |
| 170 |
action="store_true", |
| 171 |
help="show file type (file or directory)", |
| 172 |
) |
| 173 |
parser.add_option( |
| 174 |
"-m", "--mode", action="store_true", help="show file mode" |
| 175 |
) |
| 176 |
parser.add_option( |
| 177 |
"-l", "--links", action="store_true", help="show number of links" |
| 178 |
) |
| 179 |
parser.add_option( |
| 180 |
"-s", "--size", action="store_true", help="show size of file" |
| 181 |
) |
| 182 |
parser.add_option( |
| 183 |
"", "--line-count", action="store_true", help="show line count of file" |
| 184 |
) |
| 185 |
parser.add_option( |
| 186 |
"-n", "--newer", action="store", help="check if file is newer (or same)" |
| 187 |
) |
| 188 |
parser.add_option( |
| 189 |
"-r", "--recurse", action="store_true", help="recurse into directories" |
| 190 |
) |
| 191 |
parser.add_option( |
| 192 |
"-S", |
| 193 |
"--sha1", |
| 194 |
action="store_true", |
| 195 |
help="show sha1 hash of the content", |
| 196 |
) |
| 197 |
parser.add_option( |
| 198 |
"", |
| 199 |
"--raw-sha1", |
| 200 |
action="store_true", |
| 201 |
help="show raw bytes of the sha1 hash of the content", |
| 202 |
) |
| 203 |
parser.add_option( |
| 204 |
"", |
| 205 |
"--sha256", |
| 206 |
action="store_true", |
| 207 |
help="show sha256 hash of the content", |
| 208 |
) |
| 209 |
parser.add_option( |
| 210 |
"-M", "--md5", action="store_true", help="show md5 hash of the content" |
| 211 |
) |
| 212 |
parser.add_option( |
| 213 |
"-D", "--dump", action="store_true", help="dump file content" |
| 214 |
) |
| 215 |
parser.add_option( |
| 216 |
"-H", "--hexdump", action="store_true", help="hexdump file content" |
| 217 |
) |
| 218 |
parser.add_option( |
| 219 |
"-B", "--bytes", type="int", help="number of characters to dump" |
| 220 |
) |
| 221 |
parser.add_option( |
| 222 |
"-L", "--lines", type="int", help="number of lines to dump" |
| 223 |
) |
| 224 |
parser.add_option( |
| 225 |
"-q", "--quiet", action="store_true", help="no default output" |
| 226 |
) |
| 227 |
(opts, filenames) = parser.parse_args(sys.argv[1:]) |
| 228 |
if not filenames: |
| 229 |
filenames = ['-'] |
| 230 |
|
| 231 |
visit(opts, filenames, getattr(sys.stdout, 'buffer', sys.stdout)) |