tests/f
author Gregory Szorc <gregory.szorc@gmail.com>
Sat, 17 Feb 2018 11:19:52 -0700
changeset 36481 1fa35ca345a5
parent 36294 c69e78ef2b54
child 36954 0585337ea787
permissions -rwxr-xr-x
internals: document bundle2 format It seems like a good idea to have thorough documentation of the bundle2 data format, including the format of each part and the capabilities. The added documentation is far from complete. For example, we don't fully capture the semantics of each capability and part. But a start is better than nothing, which was pretty much where we were before. Differential Revision: https://phab.mercurial-scm.org/D2298
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
36294
c69e78ef2b54 py3: port f to Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 35476
diff changeset
    28
import binascii
29160
0362605b82cf py3: make tests/f use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 28046
diff changeset
    29
import glob
29233
318534bb5dfd tests: make 'f' utility import hashlib unconditionally
Yuya Nishihara <yuya@tcha.org>
parents: 29230
diff changeset
    30
import hashlib
29160
0362605b82cf py3: make tests/f use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 28046
diff changeset
    31
import optparse
0362605b82cf py3: make tests/f use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 28046
diff changeset
    32
import os
0362605b82cf py3: make tests/f use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 28046
diff changeset
    33
import re
0362605b82cf py3: make tests/f use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 28046
diff changeset
    34
import sys
23860
bead0c7b4f68 tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff changeset
    35
34277
3db2365d43e4 tests: update `f` helper script to work on Python 3
Augie Fackler <augie@google.com>
parents: 32916
diff changeset
    36
# Python 3 adapters
3db2365d43e4 tests: update `f` helper script to work on Python 3
Augie Fackler <augie@google.com>
parents: 32916
diff changeset
    37
ispy3 = (sys.version_info[0] >= 3)
3db2365d43e4 tests: update `f` helper script to work on Python 3
Augie Fackler <augie@google.com>
parents: 32916
diff changeset
    38
if ispy3:
3db2365d43e4 tests: update `f` helper script to work on Python 3
Augie Fackler <augie@google.com>
parents: 32916
diff changeset
    39
    def iterbytes(s):
3db2365d43e4 tests: update `f` helper script to work on Python 3
Augie Fackler <augie@google.com>
parents: 32916
diff changeset
    40
        for i in range(len(s)):
3db2365d43e4 tests: update `f` helper script to work on Python 3
Augie Fackler <augie@google.com>
parents: 32916
diff changeset
    41
            yield s[i:i + 1]
3db2365d43e4 tests: update `f` helper script to work on Python 3
Augie Fackler <augie@google.com>
parents: 32916
diff changeset
    42
else:
3db2365d43e4 tests: update `f` helper script to work on Python 3
Augie Fackler <augie@google.com>
parents: 32916
diff changeset
    43
    iterbytes = iter
3db2365d43e4 tests: update `f` helper script to work on Python 3
Augie Fackler <augie@google.com>
parents: 32916
diff changeset
    44
23860
bead0c7b4f68 tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff changeset
    45
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
    46
    """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
    47
    outfile."""
bead0c7b4f68 tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff changeset
    48
    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
    49
        isstdin = f == '-'
bead0c7b4f68 tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff changeset
    50
        if not isstdin and not os.path.lexists(f):
34277
3db2365d43e4 tests: update `f` helper script to work on Python 3
Augie Fackler <augie@google.com>
parents: 32916
diff changeset
    51
            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
    52
            continue
bead0c7b4f68 tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff changeset
    53
        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
    54
        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
    55
        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
    56
        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
    57
        dirfiles = None
bead0c7b4f68 tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff changeset
    58
        content = None
bead0c7b4f68 tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff changeset
    59
        facts = []
bead0c7b4f68 tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff changeset
    60
        if isfile:
bead0c7b4f68 tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff changeset
    61
            if opts.type:
36294
c69e78ef2b54 py3: port f to Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 35476
diff changeset
    62
                facts.append(b'file')
35476
c1f7037c2ded tests: teach `f` to handle sha256 checksums
Matt Harbison <matt_harbison@yahoo.com>
parents: 35475
diff changeset
    63
            if any((opts.hexdump, opts.dump, opts.md5, opts.sha1, opts.sha256)):
32916
c425b678df7c py3: use open() instead of file() constructor
Pulkit Goyal <7895pulkit@gmail.com>
parents: 29233
diff changeset
    64
                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
    65
        elif islink:
bead0c7b4f68 tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff changeset
    66
            if opts.type:
36294
c69e78ef2b54 py3: port f to Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 35476
diff changeset
    67
                facts.append(b'link')
23860
bead0c7b4f68 tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff changeset
    68
            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
    69
        elif isstdin:
34277
3db2365d43e4 tests: update `f` helper script to work on Python 3
Augie Fackler <augie@google.com>
parents: 32916
diff changeset
    70
            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
    71
            if opts.size:
36294
c69e78ef2b54 py3: port f to Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 35476
diff changeset
    72
                facts.append(b'size=%d' % len(content))
23860
bead0c7b4f68 tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff changeset
    73
        elif isdir:
bead0c7b4f68 tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff changeset
    74
            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
    75
                dirfiles = glob.glob(f + '/*')
36294
c69e78ef2b54 py3: port f to Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 35476
diff changeset
    76
                facts.append(b'directory with %d files' % len(dirfiles))
23860
bead0c7b4f68 tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff changeset
    77
        elif opts.type:
36294
c69e78ef2b54 py3: port f to Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 35476
diff changeset
    78
            facts.append(b'type unknown')
23860
bead0c7b4f68 tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff changeset
    79
        if not isstdin:
bead0c7b4f68 tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff changeset
    80
            stat = os.lstat(f)
23911
593a5cd709a2 tests: teach f not to report directory size
Matt Mackall <mpm@selenic.com>
parents: 23860
diff changeset
    81
            if opts.size and not isdir:
36294
c69e78ef2b54 py3: port f to Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 35476
diff changeset
    82
                facts.append(b'size=%d' % stat.st_size)
23912
7d0aa6269ece tests: teach f not to report symlink mode bits
Matt Mackall <mpm@selenic.com>
parents: 23911
diff changeset
    83
            if opts.mode and not islink:
36294
c69e78ef2b54 py3: port f to Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 35476
diff changeset
    84
                facts.append(b'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
    85
            if opts.links:
36294
c69e78ef2b54 py3: port f to Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 35476
diff changeset
    86
                facts.append(b'links=%s' % stat.st_nlink)
23860
bead0c7b4f68 tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff changeset
    87
            if opts.newer:
bead0c7b4f68 tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff changeset
    88
                # 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
    89
                if stat.st_mtime >= os.stat(opts.newer).st_mtime:
36294
c69e78ef2b54 py3: port f to Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 35476
diff changeset
    90
                    facts.append(b'newer than %s' % opts.newer)
23860
bead0c7b4f68 tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff changeset
    91
                else:
36294
c69e78ef2b54 py3: port f to Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 35476
diff changeset
    92
                    facts.append(b'older than %s' % opts.newer)
23860
bead0c7b4f68 tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff changeset
    93
        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
    94
            h = hashlib.md5(content)
36294
c69e78ef2b54 py3: port f to Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 35476
diff changeset
    95
            facts.append(b'md5=%s' % binascii.hexlify(h.digest())[:opts.bytes])
23860
bead0c7b4f68 tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff changeset
    96
        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
    97
            h = hashlib.sha1(content)
36294
c69e78ef2b54 py3: port f to Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 35476
diff changeset
    98
            facts.append(b'sha1=%s' % binascii.hexlify(h.digest())[:opts.bytes])
35476
c1f7037c2ded tests: teach `f` to handle sha256 checksums
Matt Harbison <matt_harbison@yahoo.com>
parents: 35475
diff changeset
    99
        if opts.sha256 and content is not None:
c1f7037c2ded tests: teach `f` to handle sha256 checksums
Matt Harbison <matt_harbison@yahoo.com>
parents: 35475
diff changeset
   100
            h = hashlib.sha256(content)
36294
c69e78ef2b54 py3: port f to Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 35476
diff changeset
   101
            facts.append(b'sha256=%s' %
c69e78ef2b54 py3: port f to Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 35476
diff changeset
   102
                         binascii.hexlify(h.digest())[:opts.bytes])
23860
bead0c7b4f68 tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff changeset
   103
        if isstdin:
34277
3db2365d43e4 tests: update `f` helper script to work on Python 3
Augie Fackler <augie@google.com>
parents: 32916
diff changeset
   104
            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
   105
        elif facts:
34277
3db2365d43e4 tests: update `f` helper script to work on Python 3
Augie Fackler <augie@google.com>
parents: 32916
diff changeset
   106
            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
   107
        elif not quiet:
34277
3db2365d43e4 tests: update `f` helper script to work on Python 3
Augie Fackler <augie@google.com>
parents: 32916
diff changeset
   108
            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
   109
        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
   110
            chunk = content
bead0c7b4f68 tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff changeset
   111
            if not islink:
bead0c7b4f68 tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff changeset
   112
                if opts.lines:
bead0c7b4f68 tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff changeset
   113
                    if opts.lines >= 0:
34277
3db2365d43e4 tests: update `f` helper script to work on Python 3
Augie Fackler <augie@google.com>
parents: 32916
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
                    else:
34277
3db2365d43e4 tests: update `f` helper script to work on Python 3
Augie Fackler <augie@google.com>
parents: 32916
diff changeset
   116
                        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
   117
                if opts.bytes:
bead0c7b4f68 tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff changeset
   118
                    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
   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
                    else:
bead0c7b4f68 tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff changeset
   121
                        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
   122
            if opts.hexdump:
bead0c7b4f68 tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff changeset
   123
                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
   124
                    s = chunk[i:i + 16]
34277
3db2365d43e4 tests: update `f` helper script to work on Python 3
Augie Fackler <augie@google.com>
parents: 32916
diff changeset
   125
                    outfile.write(b'%04x: %-47s |%s|\n' %
3db2365d43e4 tests: update `f` helper script to work on Python 3
Augie Fackler <augie@google.com>
parents: 32916
diff changeset
   126
                                  (i, b' '.join(
3db2365d43e4 tests: update `f` helper script to work on Python 3
Augie Fackler <augie@google.com>
parents: 32916
diff changeset
   127
                                      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: 32916
diff changeset
   128
                                   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
   129
            if opts.dump:
bead0c7b4f68 tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff changeset
   130
                if not quiet:
34277
3db2365d43e4 tests: update `f` helper script to work on Python 3
Augie Fackler <augie@google.com>
parents: 32916
diff changeset
   131
                    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
   132
                outfile.write(chunk)
bead0c7b4f68 tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff changeset
   133
                if not quiet:
34277
3db2365d43e4 tests: update `f` helper script to work on Python 3
Augie Fackler <augie@google.com>
parents: 32916
diff changeset
   134
                    if chunk.endswith(b'\n'):
3db2365d43e4 tests: update `f` helper script to work on Python 3
Augie Fackler <augie@google.com>
parents: 32916
diff changeset
   135
                        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
   136
                    else:
34277
3db2365d43e4 tests: update `f` helper script to work on Python 3
Augie Fackler <augie@google.com>
parents: 32916
diff changeset
   137
                        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
   138
        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
   139
            assert not isstdin
bead0c7b4f68 tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff changeset
   140
            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
   141
bead0c7b4f68 tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff changeset
   142
if __name__ == "__main__":
bead0c7b4f68 tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff changeset
   143
    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
   144
    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
   145
                      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
   146
    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
   147
                      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
   148
    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
   149
                      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
   150
    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
   151
                      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
   152
    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
   153
                      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
   154
    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
   155
                      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
   156
    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
   157
                      help="show sha1 hash of the content")
35476
c1f7037c2ded tests: teach `f` to handle sha256 checksums
Matt Harbison <matt_harbison@yahoo.com>
parents: 35475
diff changeset
   158
    parser.add_option("", "--sha256", action="store_true",
c1f7037c2ded tests: teach `f` to handle sha256 checksums
Matt Harbison <matt_harbison@yahoo.com>
parents: 35475
diff changeset
   159
                      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
   160
    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
   161
                      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
   162
    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
   163
                      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
   164
    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
   165
                      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
   166
    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
   167
                      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
   168
    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
   169
                      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
   170
    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
   171
                      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
   172
    (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
   173
    if not filenames:
bead0c7b4f68 tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff changeset
   174
        filenames = ['-']
bead0c7b4f68 tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff changeset
   175
34277
3db2365d43e4 tests: update `f` helper script to work on Python 3
Augie Fackler <augie@google.com>
parents: 32916
diff changeset
   176
    visit(opts, filenames, getattr(sys.stdout, 'buffer', sys.stdout))