tests/f
author Matt Harbison <matt_harbison@yahoo.com>
Sun, 22 Aug 2021 17:59:21 -0400
branchstable
changeset 47866 4162f6b40f2c
parent 45830 c102b704edb5
child 48875 6000f5b25c9b
permissions -rwxr-xr-x
windows: degrade to py2 behavior when reading a non-symlink as a symlink While waiting for the push to hg-committed in WSL to complete, I ran a `phabimport` from Windows and got this traceback: $ hg phabimport 11313 ** Unknown exception encountered with possibly-broken third-party extension "mercurial_keyring" (version N/A) ** which supports versions unknown of Mercurial. ** Please disable "mercurial_keyring" and try your action again. ** If that fixes the bug please report it to https://foss.heptapod.net/mercurial/mercurial_keyring/issues ** Python 3.9.5 (default, May 6 2021, 17:29:31) [MSC v.1928 64 bit (AMD64)] ** Mercurial Distributed SCM (version 5.9rc1+hg32.0e2f5733563d) ** Extensions loaded: absorb, blackbox, evolve 10.3.3, extdiff, fastannotate, fix, mercurial_keyring, mq, phabblocker 20210126, phabricator, rebase, show, strip, topic 0.22.3 Traceback (most recent call last): File "mercurial.lock", line 279, in _trylock File "mercurial.vfs", line 202, in makelock File "mercurial.util", line 2147, in makelock FileExistsError: [WinError 183] Cannot create a file when that file already exists: b'hp-omen:78348' -> b'C:\\Users\\Matt\\hg/.hg/store/lock' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "<string>", line 24, in <module> File "mercurial.dispatch", line 144, in run File "mercurial.dispatch", line 250, in dispatch File "mercurial.dispatch", line 294, in _rundispatch File "mercurial.dispatch", line 470, in _runcatch File "mercurial.dispatch", line 480, in _callcatch File "mercurial.scmutil", line 153, in callcatch File "mercurial.dispatch", line 460, in _runcatchfunc File "mercurial.dispatch", line 1273, in _dispatch File "mercurial.dispatch", line 918, in runcommand File "mercurial.dispatch", line 1285, in _runcommand File "mercurial.dispatch", line 1271, in <lambda> File "mercurial.util", line 1886, in check File "mercurial.util", line 1886, in check File "hgext.mq", line 4239, in mqcommand File "mercurial.util", line 1886, in check File "mercurial.util", line 1886, in check File "hgext.phabricator", line 314, in inner File "hgext.phabricator", line 2222, in phabimport File "hgext.phabricator", line 2123, in readpatch File "hgext.phabricator", line 2199, in _write File "mercurial.localrepo", line 2956, in lock File "mercurial.localrepo", line 2918, in _lock File "mercurial.lock", line 152, in trylock File "mercurial.lock", line 283, in _trylock File "mercurial.lock", line 314, in _readlock File "mercurial.vfs", line 221, in readlock File "mercurial.util", line 2163, in readlock File "mercurial.windows", line 619, in readlink ValueError: not a symbolic link Both exceptions look accurate (the file exists, and the Windows side can't read WSL side symlinks). I didn't try to reproduce this entirely within the Windows side, but we can do better than a cryptic stacktrace. With this change, the same scenario results in this abort: abort: C:\Users\Matt\hg/.hg/store/lock: The file cannot be accessed by the system When both the `push` and `phabimport` are done on the Windows side, it prints a message about waiting for the lock, and successfully applies the patch after the push completes. I'm not sure if there's enough info to be able to convert the abort into the wait scenario. As it stands now, we don't support symlinks on Windows, which requires either a UAC Administrator level process or an opt-in in developer mode, and there are several places where the new symlink on Windows support in py3 was explicitly disabled in order to get tests to pass quicker. Differential Revision: https://phab.mercurial-scm.org/D11333

#!/usr/bin/env python3

"""
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
"""

from __future__ import absolute_import

import binascii
import glob
import hashlib
import optparse
import os
import re
import sys

# Python 3 adapters
ispy3 = sys.version_info[0] >= 3
if ispy3:

    def iterbytes(s):
        for i in range(len(s)):
            yield s[i : i + 1]


else:
    iterbytes = iter


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(b'%s: file not found\n' % f.encode('utf-8'))
            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(b'file')
            if any((opts.hexdump, opts.dump, opts.md5, opts.sha1, opts.sha256)):
                with open(f, 'rb') as fobj:
                    content = fobj.read()
        elif islink:
            if opts.type:
                facts.append(b'link')
            content = os.readlink(f).encode('utf8')
        elif isstdin:
            content = getattr(sys.stdin, 'buffer', sys.stdin).read()
            if opts.size:
                facts.append(b'size=%d' % len(content))
        elif isdir:
            if opts.recurse or opts.type:
                dirfiles = glob.glob(f + '/*')
                facts.append(b'directory with %d files' % len(dirfiles))
        elif opts.type:
            facts.append(b'type unknown')
        if not isstdin:
            stat = os.lstat(f)
            if opts.size and not isdir:
                facts.append(b'size=%d' % stat.st_size)
            if opts.mode and not islink:
                facts.append(b'mode=%o' % (stat.st_mode & 0o777))
            if opts.links:
                facts.append(b'links=%d' % 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(
                        b'newer than %s' % opts.newer.encode('utf8', 'replace')
                    )
                else:
                    facts.append(
                        b'older than %s' % opts.newer.encode('utf8', 'replace')
                    )
        if opts.md5 and content is not None:
            h = hashlib.md5(content)
            facts.append(b'md5=%s' % binascii.hexlify(h.digest())[: opts.bytes])
        if opts.sha1 and content is not None:
            h = hashlib.sha1(content)
            facts.append(
                b'sha1=%s' % binascii.hexlify(h.digest())[: opts.bytes]
            )
        if opts.sha256 and content is not None:
            h = hashlib.sha256(content)
            facts.append(
                b'sha256=%s' % binascii.hexlify(h.digest())[: opts.bytes]
            )
        if isstdin:
            outfile.write(b', '.join(facts) + b'\n')
        elif facts:
            outfile.write(b'%s: %s\n' % (f.encode('utf-8'), b', '.join(facts)))
        elif not quiet:
            outfile.write(b'%s:\n' % f.encode('utf-8'))
        if content is not None:
            chunk = content
            if not islink:
                if opts.lines:
                    if opts.lines >= 0:
                        chunk = b''.join(chunk.splitlines(True)[: opts.lines])
                    else:
                        chunk = b''.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(
                        b'%04x: %-47s |%s|\n'
                        % (
                            i,
                            b' '.join(b'%02x' % ord(c) for c in iterbytes(s)),
                            re.sub(b'[^ -~]', b'.', s),
                        )
                    )
            if opts.dump:
                if not quiet:
                    outfile.write(b'>>>\n')
                outfile.write(chunk)
                if not quiet:
                    if chunk.endswith(b'\n'):
                        outfile.write(b'<<<\n')
                    else:
                        outfile.write(b'\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(
        "",
        "--sha256",
        action="store_true",
        help="show sha256 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, getattr(sys.stdout, 'buffer', sys.stdout))