tests/f
author Anton Shestakov <av6@dwimlabs.net>
Thu, 26 Oct 2017 11:58:55 +0800
branchstable
changeset 34927 c2695cbb5fb1
parent 34270 3db2365d43e4
child 35471 ea4d27aac557
permissions -rwxr-xr-x
makefile: put format-c into .PHONY targets
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
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')
bead0c7b4f68 tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff changeset
    62
            if opts.hexdump or opts.dump or opts.md5:
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])
23860
bead0c7b4f68 tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff changeset
    98
        if isstdin:
34270
3db2365d43e4 tests: update `f` helper script to work on Python 3
Augie Fackler <augie@google.com>
parents: 32898
diff changeset
    99
            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
   100
        elif facts:
34270
3db2365d43e4 tests: update `f` helper script to work on Python 3
Augie Fackler <augie@google.com>
parents: 32898
diff changeset
   101
            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
   102
        elif not quiet:
34270
3db2365d43e4 tests: update `f` helper script to work on Python 3
Augie Fackler <augie@google.com>
parents: 32898
diff changeset
   103
            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
   104
        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
   105
            chunk = content
bead0c7b4f68 tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff changeset
   106
            if not islink:
bead0c7b4f68 tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff changeset
   107
                if opts.lines:
bead0c7b4f68 tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff changeset
   108
                    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
   109
                        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
   110
                    else:
34270
3db2365d43e4 tests: update `f` helper script to work on Python 3
Augie Fackler <augie@google.com>
parents: 32898
diff changeset
   111
                        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
   112
                if opts.bytes:
bead0c7b4f68 tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff changeset
   113
                    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
   114
                        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
   115
                    else:
bead0c7b4f68 tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff changeset
   116
                        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
   117
            if opts.hexdump:
bead0c7b4f68 tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff changeset
   118
                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
   119
                    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
   120
                    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
   121
                                  (i, b' '.join(
3db2365d43e4 tests: update `f` helper script to work on Python 3
Augie Fackler <augie@google.com>
parents: 32898
diff changeset
   122
                                      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
   123
                                   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
   124
            if opts.dump:
bead0c7b4f68 tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff changeset
   125
                if not quiet:
34270
3db2365d43e4 tests: update `f` helper script to work on Python 3
Augie Fackler <augie@google.com>
parents: 32898
diff changeset
   126
                    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
   127
                outfile.write(chunk)
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
                    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
   130
                        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
   131
                    else:
34270
3db2365d43e4 tests: update `f` helper script to work on Python 3
Augie Fackler <augie@google.com>
parents: 32898
diff changeset
   132
                        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
   133
        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
   134
            assert not isstdin
bead0c7b4f68 tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff changeset
   135
            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
   136
bead0c7b4f68 tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff changeset
   137
if __name__ == "__main__":
bead0c7b4f68 tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff changeset
   138
    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
   139
    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
   140
                      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
   141
    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
   142
                      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
   143
    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
   144
                      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
   145
    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
   146
                      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
   147
    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
   148
                      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
   149
    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
   150
                      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
   151
    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
   152
                      help="show sha1 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
   153
    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
   154
                      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
   155
    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
   156
                      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
   157
    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
   158
                      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
   159
    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
   160
                      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
   161
    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
   162
                      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
   163
    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
   164
                      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
   165
    (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
   166
    if not filenames:
bead0c7b4f68 tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff changeset
   167
        filenames = ['-']
bead0c7b4f68 tests: add 'f' tool for cross platform file operations in the tests
Mads Kiilerich <madski@unity3d.com>
parents:
diff changeset
   168
34270
3db2365d43e4 tests: update `f` helper script to work on Python 3
Augie Fackler <augie@google.com>
parents: 32898
diff changeset
   169
    visit(opts, filenames, getattr(sys.stdout, 'buffer', sys.stdout))