changeset 23860:bead0c7b4f68

tests: add 'f' tool for cross platform file operations in the tests This tool is like the collection of tools found in a unix environment but are cross platform and stable and suitable for our needs in the test suite. The main reason it is "needed" now is for hexdump of revision branch cache to keep an eye on how it changes and make sure the format is stable. It is a very generic tool that can end up being used a lot in tests, so I gave it very generic name.
author Mads Kiilerich <madski@unity3d.com>
date Wed, 14 Jan 2015 01:15:26 +0100
parents 661b246bf1c4
children 01426cad66dc
files tests/f tests/test-tools.t
diffstat 2 files changed, 245 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/f	Wed Jan 14 01:15:26 2015 +0100
@@ -0,0 +1,158 @@
+#!/usr/bin/env python
+
+"""
+Utility for inspecting files in various ways.
+
+This tool is like the collection of tools found in a unix environment but are
+cross platform and stable and suitable for our needs in the test suite.
+
+This can be used instead of tools like:
+  [
+  dd
+  find
+  head
+  hexdump
+  ls
+  md5sum
+  readlink
+  sha1sum
+  stat
+  tail
+  test
+  readlink.py
+  md5sum.py
+"""
+
+import sys, os, errno, re, glob, optparse
+
+def visit(opts, filenames, outfile):
+    """Process filenames in the way specified in opts, writing output to
+    outfile."""
+    for f in sorted(filenames):
+        isstdin = f == '-'
+        if not isstdin and not os.path.lexists(f):
+            outfile.write('%s: file not found\n' % f)
+            continue
+        quiet = opts.quiet and not opts.recurse or isstdin
+        isdir = os.path.isdir(f)
+        islink = os.path.islink(f)
+        isfile = os.path.isfile(f) and not islink
+        dirfiles = None
+        content = None
+        facts = []
+        if isfile:
+            if opts.type:
+                facts.append('file')
+            if opts.hexdump or opts.dump or opts.md5:
+                content = file(f).read()
+        elif islink:
+            if opts.type:
+                facts.append('link')
+            content = os.readlink(f)
+        elif isstdin:
+            content = sys.stdin.read()
+            if opts.size:
+                facts.append('size=%s' % len(content))
+        elif isdir:
+            if opts.recurse or opts.type:
+                dirfiles = glob.glob(f + '/*')
+                facts.append('directory with %s files' % len(dirfiles))
+        elif opts.type:
+            facts.append('type unknown')
+        if not isstdin:
+            stat = os.lstat(f)
+            if opts.size:
+                facts.append('size=%s' % stat.st_size)
+            if opts.mode:
+                facts.append('mode=%o' % (stat.st_mode & 0777))
+            if opts.links:
+                facts.append('links=%s' % stat.st_nlink)
+            if opts.newer:
+                # mtime might be in whole seconds so newer file might be same
+                if stat.st_mtime >= os.stat(opts.newer).st_mtime:
+                    facts.append('newer than %s' % opts.newer)
+                else:
+                    facts.append('older than %s' % opts.newer)
+        if opts.md5 and content is not None:
+            try:
+                from hashlib import md5
+            except ImportError:
+                from md5 import md5
+            facts.append('md5=%s' % md5(content).hexdigest()[:opts.bytes])
+        if opts.sha1 and content is not None:
+            try:
+                from hashlib import sha1
+            except ImportError:
+                from sha import sha as sha1
+            facts.append('sha1=%s' % sha1(content).hexdigest()[:opts.bytes])
+        if isstdin:
+            outfile.write(', '.join(facts) + '\n')
+        elif facts:
+            outfile.write('%s: %s\n' % (f, ', '.join(facts)))
+        elif not quiet:
+            outfile.write('%s:\n' % f)
+        if content is not None:
+            chunk = content
+            if not islink:
+                if opts.lines:
+                    if opts.lines >= 0:
+                        chunk = ''.join(chunk.splitlines(True)[:opts.lines])
+                    else:
+                        chunk = ''.join(chunk.splitlines(True)[opts.lines:])
+                if opts.bytes:
+                    if opts.bytes >= 0:
+                        chunk = chunk[:opts.bytes]
+                    else:
+                        chunk = chunk[opts.bytes:]
+            if opts.hexdump:
+                for i in range(0, len(chunk), 16):
+                    s = chunk[i:i+16]
+                    outfile.write('%04x: %-47s |%s|\n' %
+                                  (i, ' '.join('%02x' % ord(c) for c in s),
+                                   re.sub('[^ -~]', '.', s)))
+            if opts.dump:
+                if not quiet:
+                    outfile.write('>>>\n')
+                outfile.write(chunk)
+                if not quiet:
+                    if chunk.endswith('\n'):
+                        outfile.write('<<<\n')
+                    else:
+                        outfile.write('\n<<< no trailing newline\n')
+        if opts.recurse and dirfiles:
+            assert not isstdin
+            visit(opts, dirfiles, outfile)
+
+if __name__ == "__main__":
+    parser = optparse.OptionParser("%prog [options] [filenames]")
+    parser.add_option("-t", "--type", action="store_true",
+                      help="show file type (file or directory)")
+    parser.add_option("-m", "--mode", action="store_true",
+                      help="show file mode")
+    parser.add_option("-l", "--links", action="store_true",
+                      help="show number of links")
+    parser.add_option("-s", "--size", action="store_true",
+                      help="show size of file")
+    parser.add_option("-n", "--newer", action="store",
+                      help="check if file is newer (or same)")
+    parser.add_option("-r", "--recurse", action="store_true",
+                      help="recurse into directories")
+    parser.add_option("-S", "--sha1", action="store_true",
+                      help="show sha1 hash of the content")
+    parser.add_option("-M", "--md5", action="store_true",
+                      help="show md5 hash of the content")
+    parser.add_option("-D", "--dump", action="store_true",
+                      help="dump file content")
+    parser.add_option("-H", "--hexdump", action="store_true",
+                      help="hexdump file content")
+    parser.add_option("-B", "--bytes", type="int",
+                      help="number of characters to dump")
+    parser.add_option("-L", "--lines", type="int",
+                      help="number of lines to dump")
+    parser.add_option("-q", "--quiet", action="store_true",
+                      help="no default output")
+    (opts, filenames) = parser.parse_args(sys.argv[1:])
+    if not filenames:
+        filenames = ['-']
+
+    visit(opts, filenames, sys.stdout)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/test-tools.t	Wed Jan 14 01:15:26 2015 +0100
@@ -0,0 +1,87 @@
+Tests of the file helper tool
+
+  $ f -h
+  Usage: f [options] [filenames]
+  
+  Options:
+    -h, --help            show this help message and exit
+    -t, --type            show file type (file or directory)
+    -m, --mode            show file mode
+    -l, --links           show number of links
+    -s, --size            show size of file
+    -n NEWER, --newer=NEWER
+                          check if file is newer (or same)
+    -r, --recurse         recurse into directories
+    -S, --sha1            show sha1 hash of the content
+    -M, --md5             show md5 hash of the content
+    -D, --dump            dump file content
+    -H, --hexdump         hexdump file content
+    -B BYTES, --bytes=BYTES
+                          number of characters to dump
+    -L LINES, --lines=LINES
+                          number of lines to dump
+    -q, --quiet           no default output
+
+  $ mkdir dir
+  $ cd dir
+
+  $ f --size
+  size=0
+
+  $ echo hello | f --md5 --size
+  size=6, md5=b1946ac92492d2347c6235b4d2611184
+
+  $ f foo
+  foo: file not found
+
+  $ echo foo > foo
+  $ f foo
+  foo:
+
+  $ f foo --mode
+  foo: mode=644
+
+  $ seq 10 > bar
+  $ chmod +x bar
+  $ f bar --newer foo --mode --type --size --dump --links --bytes 7
+  bar: file, size=21, mode=755, links=1, newer than foo
+  >>>
+  1
+  2
+  3
+  4
+  <<< no trailing newline
+
+  $ ln bar baz
+  $ f bar -n baz -l --hexdump -t --sha1 --lines=9 -B 20
+  bar: file, links=2, newer than baz, sha1=612ca68d0305c821750a
+  0000: 31 0a 32 0a 33 0a 34 0a 35 0a 36 0a 37 0a 38 0a |1.2.3.4.5.6.7.8.|
+  0010: 39 0a                                           |9.|
+
+  $ ln -s yadda l
+  $ f . --recurse -MStmsB4
+  .: directory with 4 files, size=120, mode=755
+  ./bar: file, size=21, mode=755, md5=3b03, sha1=612c
+  ./baz: file, size=21, mode=755, md5=3b03, sha1=612c
+  ./foo: file, size=4, mode=644, md5=d3b0, sha1=f1d2
+  ./l: link, size=5, mode=777, md5=2faa, sha1=af93
+
+  $ f --quiet bar -DL 3
+  1
+  2
+  3
+
+  $ cd ..
+
+  $ f -qr dir -HB 17
+  dir: directory with 4 files
+  dir/bar:
+  0000: 31 0a 32 0a 33 0a 34 0a 35 0a 36 0a 37 0a 38 0a |1.2.3.4.5.6.7.8.|
+  0010: 39                                              |9|
+  dir/baz:
+  0000: 31 0a 32 0a 33 0a 34 0a 35 0a 36 0a 37 0a 38 0a |1.2.3.4.5.6.7.8.|
+  0010: 39                                              |9|
+  dir/foo:
+  0000: 66 6f 6f 0a                                     |foo.|
+  dir/l:
+  0000: 79 61 64 64 61                                  |yadda|