author | Gregory Szorc <gregory.szorc@gmail.com> |
Tue, 20 Feb 2018 19:09:01 -0800 | |
branch | stable |
changeset 36752 | bbd4027b019b |
parent 35472 | c1f7037c2ded |
child 36261 | c69e78ef2b54 |
permissions | -rwxr-xr-x |
23860
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
1 |
#!/usr/bin/env python |
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
2 |
|
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
3 |
""" |
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
4 |
Utility for inspecting files in various ways. |
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
5 |
|
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
6 |
This tool is like the collection of tools found in a unix environment but are |
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
7 |
cross platform and stable and suitable for our needs in the test suite. |
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
8 |
|
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
9 |
This can be used instead of tools like: |
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
10 |
[ |
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
11 |
dd |
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
12 |
find |
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
13 |
head |
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
14 |
hexdump |
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
15 |
ls |
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
16 |
md5sum |
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
17 |
readlink |
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
18 |
sha1sum |
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
19 |
stat |
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
20 |
tail |
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
21 |
test |
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
22 |
readlink.py |
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
23 |
md5sum.py |
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
24 |
""" |
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
25 |
|
29160
0362605b82cf
py3: make tests/f use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents:
28046
diff
changeset
|
26 |
from __future__ import absolute_import |
0362605b82cf
py3: make tests/f use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents:
28046
diff
changeset
|
27 |
|
0362605b82cf
py3: make tests/f use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents:
28046
diff
changeset
|
28 |
import glob |
29233
318534bb5dfd
tests: make 'f' utility import hashlib unconditionally
Yuya Nishihara <yuya@tcha.org>
parents:
29230
diff
changeset
|
29 |
import hashlib |
29160
0362605b82cf
py3: make tests/f use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents:
28046
diff
changeset
|
30 |
import optparse |
0362605b82cf
py3: make tests/f use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents:
28046
diff
changeset
|
31 |
import os |
0362605b82cf
py3: make tests/f use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents:
28046
diff
changeset
|
32 |
import re |
0362605b82cf
py3: make tests/f use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents:
28046
diff
changeset
|
33 |
import sys |
23860
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
34 |
|
34270
3db2365d43e4
tests: update `f` helper script to work on Python 3
Augie Fackler <augie@google.com>
parents:
32898
diff
changeset
|
35 |
# Python 3 adapters |
3db2365d43e4
tests: update `f` helper script to work on Python 3
Augie Fackler <augie@google.com>
parents:
32898
diff
changeset
|
36 |
ispy3 = (sys.version_info[0] >= 3) |
3db2365d43e4
tests: update `f` helper script to work on Python 3
Augie Fackler <augie@google.com>
parents:
32898
diff
changeset
|
37 |
if ispy3: |
3db2365d43e4
tests: update `f` helper script to work on Python 3
Augie Fackler <augie@google.com>
parents:
32898
diff
changeset
|
38 |
def iterbytes(s): |
3db2365d43e4
tests: update `f` helper script to work on Python 3
Augie Fackler <augie@google.com>
parents:
32898
diff
changeset
|
39 |
for i in range(len(s)): |
3db2365d43e4
tests: update `f` helper script to work on Python 3
Augie Fackler <augie@google.com>
parents:
32898
diff
changeset
|
40 |
yield s[i:i + 1] |
3db2365d43e4
tests: update `f` helper script to work on Python 3
Augie Fackler <augie@google.com>
parents:
32898
diff
changeset
|
41 |
else: |
3db2365d43e4
tests: update `f` helper script to work on Python 3
Augie Fackler <augie@google.com>
parents:
32898
diff
changeset
|
42 |
iterbytes = iter |
3db2365d43e4
tests: update `f` helper script to work on Python 3
Augie Fackler <augie@google.com>
parents:
32898
diff
changeset
|
43 |
|
23860
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
44 |
def visit(opts, filenames, outfile): |
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
45 |
"""Process filenames in the way specified in opts, writing output to |
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
46 |
outfile.""" |
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
47 |
for f in sorted(filenames): |
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
48 |
isstdin = f == '-' |
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
49 |
if not isstdin and not os.path.lexists(f): |
34270
3db2365d43e4
tests: update `f` helper script to work on Python 3
Augie Fackler <augie@google.com>
parents:
32898
diff
changeset
|
50 |
outfile.write(b'%s: file not found\n' % f.encode('utf-8')) |
23860
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
51 |
continue |
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
52 |
quiet = opts.quiet and not opts.recurse or isstdin |
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
53 |
isdir = os.path.isdir(f) |
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
54 |
islink = os.path.islink(f) |
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
55 |
isfile = os.path.isfile(f) and not islink |
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
56 |
dirfiles = None |
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
57 |
content = None |
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
58 |
facts = [] |
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
59 |
if isfile: |
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
60 |
if opts.type: |
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
61 |
facts.append('file') |
35472
c1f7037c2ded
tests: teach `f` to handle sha256 checksums
Matt Harbison <matt_harbison@yahoo.com>
parents:
35471
diff
changeset
|
62 |
if any((opts.hexdump, opts.dump, opts.md5, opts.sha1, opts.sha256)): |
32898
c425b678df7c
py3: use open() instead of file() constructor
Pulkit Goyal <7895pulkit@gmail.com>
parents:
29233
diff
changeset
|
63 |
content = open(f, 'rb').read() |
23860
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
64 |
elif islink: |
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
65 |
if opts.type: |
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
66 |
facts.append('link') |
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
67 |
content = os.readlink(f) |
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
68 |
elif isstdin: |
34270
3db2365d43e4
tests: update `f` helper script to work on Python 3
Augie Fackler <augie@google.com>
parents:
32898
diff
changeset
|
69 |
content = getattr(sys.stdin, 'buffer', sys.stdin).read() |
23860
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
70 |
if opts.size: |
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
71 |
facts.append('size=%s' % len(content)) |
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
72 |
elif isdir: |
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
73 |
if opts.recurse or opts.type: |
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
74 |
dirfiles = glob.glob(f + '/*') |
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
75 |
facts.append('directory with %s files' % len(dirfiles)) |
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
76 |
elif opts.type: |
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
77 |
facts.append('type unknown') |
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
78 |
if not isstdin: |
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
79 |
stat = os.lstat(f) |
23911
593a5cd709a2
tests: teach f not to report directory size
Matt Mackall <mpm@selenic.com>
parents:
23860
diff
changeset
|
80 |
if opts.size and not isdir: |
23860
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
81 |
facts.append('size=%s' % stat.st_size) |
23912
7d0aa6269ece
tests: teach f not to report symlink mode bits
Matt Mackall <mpm@selenic.com>
parents:
23911
diff
changeset
|
82 |
if opts.mode and not islink: |
28046
742cf5b979ec
f: use modern octal number formatting
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
28044
diff
changeset
|
83 |
facts.append('mode=%o' % (stat.st_mode & 0o777)) |
23860
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
84 |
if opts.links: |
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
85 |
facts.append('links=%s' % stat.st_nlink) |
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
86 |
if opts.newer: |
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
87 |
# mtime might be in whole seconds so newer file might be same |
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
88 |
if stat.st_mtime >= os.stat(opts.newer).st_mtime: |
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
89 |
facts.append('newer than %s' % opts.newer) |
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
90 |
else: |
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
91 |
facts.append('older than %s' % opts.newer) |
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
92 |
if opts.md5 and content is not None: |
29233
318534bb5dfd
tests: make 'f' utility import hashlib unconditionally
Yuya Nishihara <yuya@tcha.org>
parents:
29230
diff
changeset
|
93 |
h = hashlib.md5(content) |
318534bb5dfd
tests: make 'f' utility import hashlib unconditionally
Yuya Nishihara <yuya@tcha.org>
parents:
29230
diff
changeset
|
94 |
facts.append('md5=%s' % h.hexdigest()[:opts.bytes]) |
23860
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
95 |
if opts.sha1 and content is not None: |
29233
318534bb5dfd
tests: make 'f' utility import hashlib unconditionally
Yuya Nishihara <yuya@tcha.org>
parents:
29230
diff
changeset
|
96 |
h = hashlib.sha1(content) |
318534bb5dfd
tests: make 'f' utility import hashlib unconditionally
Yuya Nishihara <yuya@tcha.org>
parents:
29230
diff
changeset
|
97 |
facts.append('sha1=%s' % h.hexdigest()[:opts.bytes]) |
35472
c1f7037c2ded
tests: teach `f` to handle sha256 checksums
Matt Harbison <matt_harbison@yahoo.com>
parents:
35471
diff
changeset
|
98 |
if opts.sha256 and content is not None: |
c1f7037c2ded
tests: teach `f` to handle sha256 checksums
Matt Harbison <matt_harbison@yahoo.com>
parents:
35471
diff
changeset
|
99 |
h = hashlib.sha256(content) |
c1f7037c2ded
tests: teach `f` to handle sha256 checksums
Matt Harbison <matt_harbison@yahoo.com>
parents:
35471
diff
changeset
|
100 |
facts.append('sha256=%s' % h.hexdigest()[:opts.bytes]) |
23860
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
101 |
if isstdin: |
34270
3db2365d43e4
tests: update `f` helper script to work on Python 3
Augie Fackler <augie@google.com>
parents:
32898
diff
changeset
|
102 |
outfile.write(b', '.join(facts) + b'\n') |
23860
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
103 |
elif facts: |
34270
3db2365d43e4
tests: update `f` helper script to work on Python 3
Augie Fackler <augie@google.com>
parents:
32898
diff
changeset
|
104 |
outfile.write(b'%s: %s\n' % (f.encode('utf-8'), b', '.join(facts))) |
23860
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
105 |
elif not quiet: |
34270
3db2365d43e4
tests: update `f` helper script to work on Python 3
Augie Fackler <augie@google.com>
parents:
32898
diff
changeset
|
106 |
outfile.write(b'%s:\n' % f.encode('utf-8')) |
23860
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
107 |
if content is not None: |
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
108 |
chunk = content |
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
109 |
if not islink: |
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
110 |
if opts.lines: |
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
111 |
if opts.lines >= 0: |
34270
3db2365d43e4
tests: update `f` helper script to work on Python 3
Augie Fackler <augie@google.com>
parents:
32898
diff
changeset
|
112 |
chunk = b''.join(chunk.splitlines(True)[:opts.lines]) |
23860
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
113 |
else: |
34270
3db2365d43e4
tests: update `f` helper script to work on Python 3
Augie Fackler <augie@google.com>
parents:
32898
diff
changeset
|
114 |
chunk = b''.join(chunk.splitlines(True)[opts.lines:]) |
23860
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
115 |
if opts.bytes: |
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
116 |
if opts.bytes >= 0: |
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
117 |
chunk = chunk[:opts.bytes] |
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
118 |
else: |
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
119 |
chunk = chunk[opts.bytes:] |
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
120 |
if opts.hexdump: |
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
121 |
for i in range(0, len(chunk), 16): |
28044
e4f70e79a65f
f: add whitespace around operator
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
26950
diff
changeset
|
122 |
s = chunk[i:i + 16] |
34270
3db2365d43e4
tests: update `f` helper script to work on Python 3
Augie Fackler <augie@google.com>
parents:
32898
diff
changeset
|
123 |
outfile.write(b'%04x: %-47s |%s|\n' % |
3db2365d43e4
tests: update `f` helper script to work on Python 3
Augie Fackler <augie@google.com>
parents:
32898
diff
changeset
|
124 |
(i, b' '.join( |
3db2365d43e4
tests: update `f` helper script to work on Python 3
Augie Fackler <augie@google.com>
parents:
32898
diff
changeset
|
125 |
b'%02x' % ord(c) for c in iterbytes(s)), |
3db2365d43e4
tests: update `f` helper script to work on Python 3
Augie Fackler <augie@google.com>
parents:
32898
diff
changeset
|
126 |
re.sub(b'[^ -~]', b'.', s))) |
23860
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
127 |
if opts.dump: |
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
128 |
if not quiet: |
34270
3db2365d43e4
tests: update `f` helper script to work on Python 3
Augie Fackler <augie@google.com>
parents:
32898
diff
changeset
|
129 |
outfile.write(b'>>>\n') |
23860
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
130 |
outfile.write(chunk) |
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
131 |
if not quiet: |
34270
3db2365d43e4
tests: update `f` helper script to work on Python 3
Augie Fackler <augie@google.com>
parents:
32898
diff
changeset
|
132 |
if chunk.endswith(b'\n'): |
3db2365d43e4
tests: update `f` helper script to work on Python 3
Augie Fackler <augie@google.com>
parents:
32898
diff
changeset
|
133 |
outfile.write(b'<<<\n') |
23860
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
134 |
else: |
34270
3db2365d43e4
tests: update `f` helper script to work on Python 3
Augie Fackler <augie@google.com>
parents:
32898
diff
changeset
|
135 |
outfile.write(b'\n<<< no trailing newline\n') |
23860
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
136 |
if opts.recurse and dirfiles: |
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
137 |
assert not isstdin |
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
138 |
visit(opts, dirfiles, outfile) |
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
139 |
|
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
140 |
if __name__ == "__main__": |
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
141 |
parser = optparse.OptionParser("%prog [options] [filenames]") |
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
142 |
parser.add_option("-t", "--type", action="store_true", |
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
143 |
help="show file type (file or directory)") |
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
144 |
parser.add_option("-m", "--mode", action="store_true", |
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
145 |
help="show file mode") |
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
146 |
parser.add_option("-l", "--links", action="store_true", |
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
147 |
help="show number of links") |
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
148 |
parser.add_option("-s", "--size", action="store_true", |
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
149 |
help="show size of file") |
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
150 |
parser.add_option("-n", "--newer", action="store", |
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
151 |
help="check if file is newer (or same)") |
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
152 |
parser.add_option("-r", "--recurse", action="store_true", |
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
153 |
help="recurse into directories") |
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
154 |
parser.add_option("-S", "--sha1", action="store_true", |
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
155 |
help="show sha1 hash of the content") |
35472
c1f7037c2ded
tests: teach `f` to handle sha256 checksums
Matt Harbison <matt_harbison@yahoo.com>
parents:
35471
diff
changeset
|
156 |
parser.add_option("", "--sha256", action="store_true", |
c1f7037c2ded
tests: teach `f` to handle sha256 checksums
Matt Harbison <matt_harbison@yahoo.com>
parents:
35471
diff
changeset
|
157 |
help="show sha256 hash of the content") |
23860
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
158 |
parser.add_option("-M", "--md5", action="store_true", |
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
159 |
help="show md5 hash of the content") |
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
160 |
parser.add_option("-D", "--dump", action="store_true", |
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
161 |
help="dump file content") |
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
162 |
parser.add_option("-H", "--hexdump", action="store_true", |
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
163 |
help="hexdump file content") |
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
164 |
parser.add_option("-B", "--bytes", type="int", |
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
165 |
help="number of characters to dump") |
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
166 |
parser.add_option("-L", "--lines", type="int", |
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
167 |
help="number of lines to dump") |
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
168 |
parser.add_option("-q", "--quiet", action="store_true", |
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
169 |
help="no default output") |
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
170 |
(opts, filenames) = parser.parse_args(sys.argv[1:]) |
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
171 |
if not filenames: |
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
172 |
filenames = ['-'] |
bead0c7b4f68
tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff
changeset
|
173 |
|
34270
3db2365d43e4
tests: update `f` helper script to work on Python 3
Augie Fackler <augie@google.com>
parents:
32898
diff
changeset
|
174 |
visit(opts, filenames, getattr(sys.stdout, 'buffer', sys.stdout)) |