tests/printenv.py
author Manuel Jacob <me@manueljacob.de>
Mon, 11 Jul 2022 01:51:20 +0200
branchstable
changeset 49378 094a5fa3cf52
parent 48875 6000f5b25c9b
permissions -rwxr-xr-x
procutil: make stream detection in make_line_buffered more correct and strict In make_line_buffered(), we don’t want to wrap the stream if we know that lines get flushed to the underlying raw stream already. Previously, the heuristic was too optimistic. It assumed that any stream which is not an instance of io.BufferedIOBase doesn’t need wrapping. However, there are buffered streams that aren’t instances of io.BufferedIOBase, like Mercurial’s own winstdout. The new logic is different in two ways: First, only for the check, if unwraps any combination of WriteAllWrapper and winstdout. Second, it skips wrapping the stream only if it is an instance of io.RawIOBase (or already wrapped). If it is an instance of io.BufferedIOBase, it gets wrapped. In any other case, the function raises an exception. This ensures that, if an unknown stream is passed or we add another wrapper in the future, we don’t wrap the stream if it’s already line buffered or not wrap the stream if it’s not line buffered. In fact, this was already helpful during development of this change. Without it, I possibly would have forgot that WriteAllWrapper needs to be ignored for the check, leading to unnecessary wrapping if stdout is unbuffered. The alternative would have been to always wrap unknown streams. However, I don’t think that anyone would benefit from being less strict. We can expect streams from the standard library to be subclassing either io.RawIOBase or io.BufferedIOBase, so running Mercurial in the standard way should not regress by this change. Py2exe might replace sys.stdout and sys.stderr, but that currently breaks Mercurial anyway and also these streams don’t claim to be interactive, so this function is not called for them.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
45830
c102b704edb5 global: use python3 in shebangs
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43076
diff changeset
     1
#!/usr/bin/env python3
25477
a372f7b4463b tests: make printenv executable
Matt Mackall <mpm@selenic.com>
parents: 17018
diff changeset
     2
#
4285
4fd6f7e60894 Add tests/printenv.py
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
     3
# simple script to be used in hooks
4fd6f7e60894 Add tests/printenv.py
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
     4
#
4fd6f7e60894 Add tests/printenv.py
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
     5
# put something like this in the repo .hg/hgrc:
4fd6f7e60894 Add tests/printenv.py
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
     6
#
4fd6f7e60894 Add tests/printenv.py
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
     7
#     [hooks]
17018
e7fdfc702d9f tests: consistently use printenv.py the same MSYS/Windows-compatible way
Mads Kiilerich <mads@kiilerich.com>
parents: 16982
diff changeset
     8
#     changegroup = python "$TESTDIR/printenv.py" <hookname> [exit] [output]
4285
4fd6f7e60894 Add tests/printenv.py
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
     9
#
4fd6f7e60894 Add tests/printenv.py
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    10
#   - <hookname> is a mandatory argument (e.g. "changegroup")
4fd6f7e60894 Add tests/printenv.py
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    11
#   - [exit] is the exit code of the hook (default: 0)
4fd6f7e60894 Add tests/printenv.py
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    12
#   - [output] is the name of the output file (default: use sys.stdout)
4fd6f7e60894 Add tests/printenv.py
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    13
#              the file will be opened in append mode.
4fd6f7e60894 Add tests/printenv.py
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    14
#
41025
3c5aaea9638f tests: update printenv.py argument parsing
Boris Feld <boris.feld@octobus.net>
parents: 38296
diff changeset
    15
import argparse
4285
4fd6f7e60894 Add tests/printenv.py
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    16
import os
4fd6f7e60894 Add tests/printenv.py
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    17
import sys
4fd6f7e60894 Add tests/printenv.py
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    18
7080
a6477aa893b8 tests: Windows compatibility fixes
Patrick Mezard <pmezard@gmail.com>
parents: 4659
diff changeset
    19
try:
a6477aa893b8 tests: Windows compatibility fixes
Patrick Mezard <pmezard@gmail.com>
parents: 4659
diff changeset
    20
    import msvcrt
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41036
diff changeset
    21
7080
a6477aa893b8 tests: Windows compatibility fixes
Patrick Mezard <pmezard@gmail.com>
parents: 4659
diff changeset
    22
    msvcrt.setmode(sys.stdin.fileno(), os.O_BINARY)
a6477aa893b8 tests: Windows compatibility fixes
Patrick Mezard <pmezard@gmail.com>
parents: 4659
diff changeset
    23
    msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
7186
f77c8d8331ca clean up trailing spaces, leading spaces in C
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7080
diff changeset
    24
    msvcrt.setmode(sys.stderr.fileno(), os.O_BINARY)
7080
a6477aa893b8 tests: Windows compatibility fixes
Patrick Mezard <pmezard@gmail.com>
parents: 4659
diff changeset
    25
except ImportError:
a6477aa893b8 tests: Windows compatibility fixes
Patrick Mezard <pmezard@gmail.com>
parents: 4659
diff changeset
    26
    pass
a6477aa893b8 tests: Windows compatibility fixes
Patrick Mezard <pmezard@gmail.com>
parents: 4659
diff changeset
    27
41025
3c5aaea9638f tests: update printenv.py argument parsing
Boris Feld <boris.feld@octobus.net>
parents: 38296
diff changeset
    28
parser = argparse.ArgumentParser()
3c5aaea9638f tests: update printenv.py argument parsing
Boris Feld <boris.feld@octobus.net>
parents: 38296
diff changeset
    29
parser.add_argument("name", help="the hook name, used for display")
3c5aaea9638f tests: update printenv.py argument parsing
Boris Feld <boris.feld@octobus.net>
parents: 38296
diff changeset
    30
parser.add_argument(
3c5aaea9638f tests: update printenv.py argument parsing
Boris Feld <boris.feld@octobus.net>
parents: 38296
diff changeset
    31
    "exitcode",
3c5aaea9638f tests: update printenv.py argument parsing
Boris Feld <boris.feld@octobus.net>
parents: 38296
diff changeset
    32
    nargs="?",
3c5aaea9638f tests: update printenv.py argument parsing
Boris Feld <boris.feld@octobus.net>
parents: 38296
diff changeset
    33
    default=0,
3c5aaea9638f tests: update printenv.py argument parsing
Boris Feld <boris.feld@octobus.net>
parents: 38296
diff changeset
    34
    type=int,
3c5aaea9638f tests: update printenv.py argument parsing
Boris Feld <boris.feld@octobus.net>
parents: 38296
diff changeset
    35
    help="the exit code for the hook",
3c5aaea9638f tests: update printenv.py argument parsing
Boris Feld <boris.feld@octobus.net>
parents: 38296
diff changeset
    36
)
3c5aaea9638f tests: update printenv.py argument parsing
Boris Feld <boris.feld@octobus.net>
parents: 38296
diff changeset
    37
parser.add_argument(
3c5aaea9638f tests: update printenv.py argument parsing
Boris Feld <boris.feld@octobus.net>
parents: 38296
diff changeset
    38
    "out", nargs="?", default=None, help="where to write the output"
3c5aaea9638f tests: update printenv.py argument parsing
Boris Feld <boris.feld@octobus.net>
parents: 38296
diff changeset
    39
)
41036
73da729ccfef test: introduce a new flag to display env variable line per line
Boris Feld <boris.feld@octobus.net>
parents: 41025
diff changeset
    40
parser.add_argument(
73da729ccfef test: introduce a new flag to display env variable line per line
Boris Feld <boris.feld@octobus.net>
parents: 41025
diff changeset
    41
    "--line",
73da729ccfef test: introduce a new flag to display env variable line per line
Boris Feld <boris.feld@octobus.net>
parents: 41025
diff changeset
    42
    action="store_true",
73da729ccfef test: introduce a new flag to display env variable line per line
Boris Feld <boris.feld@octobus.net>
parents: 41025
diff changeset
    43
    help="print environment variables one per line instead of on a single line",
73da729ccfef test: introduce a new flag to display env variable line per line
Boris Feld <boris.feld@octobus.net>
parents: 41025
diff changeset
    44
)
41025
3c5aaea9638f tests: update printenv.py argument parsing
Boris Feld <boris.feld@octobus.net>
parents: 38296
diff changeset
    45
args = parser.parse_args()
4285
4fd6f7e60894 Add tests/printenv.py
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    46
41025
3c5aaea9638f tests: update printenv.py argument parsing
Boris Feld <boris.feld@octobus.net>
parents: 38296
diff changeset
    47
if args.out is None:
3c5aaea9638f tests: update printenv.py argument parsing
Boris Feld <boris.feld@octobus.net>
parents: 38296
diff changeset
    48
    out = sys.stdout
3c5aaea9638f tests: update printenv.py argument parsing
Boris Feld <boris.feld@octobus.net>
parents: 38296
diff changeset
    49
    out = getattr(out, "buffer", out)
3c5aaea9638f tests: update printenv.py argument parsing
Boris Feld <boris.feld@octobus.net>
parents: 38296
diff changeset
    50
else:
3c5aaea9638f tests: update printenv.py argument parsing
Boris Feld <boris.feld@octobus.net>
parents: 38296
diff changeset
    51
    out = open(args.out, "ab")
4285
4fd6f7e60894 Add tests/printenv.py
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    52
4643
a39cec1d5cb8 printenv: filter empty environment variables for portability.
Patrick Mezard <pmezard@gmail.com>
parents: 4285
diff changeset
    53
# variables with empty values may not exist on all platforms, filter
a39cec1d5cb8 printenv: filter empty environment variables for portability.
Patrick Mezard <pmezard@gmail.com>
parents: 4285
diff changeset
    54
# them now for portability sake.
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41036
diff changeset
    55
env = [(k, v) for k, v in os.environ.items() if k.startswith("HG_") and v]
4285
4fd6f7e60894 Add tests/printenv.py
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    56
env.sort()
4fd6f7e60894 Add tests/printenv.py
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    57
41025
3c5aaea9638f tests: update printenv.py argument parsing
Boris Feld <boris.feld@octobus.net>
parents: 38296
diff changeset
    58
out.write(b"%s hook: " % args.name.encode('ascii'))
16982
9c892c830a72 tests/printenv.py: eliminate trailing spaces on output
Adrian Buehlmann <adrian@cadifra.com>
parents: 16963
diff changeset
    59
if os.name == 'nt':
9c892c830a72 tests/printenv.py: eliminate trailing spaces on output
Adrian Buehlmann <adrian@cadifra.com>
parents: 16963
diff changeset
    60
    filter = lambda x: x.replace('\\', '/')
9c892c830a72 tests/printenv.py: eliminate trailing spaces on output
Adrian Buehlmann <adrian@cadifra.com>
parents: 16963
diff changeset
    61
else:
9c892c830a72 tests/printenv.py: eliminate trailing spaces on output
Adrian Buehlmann <adrian@cadifra.com>
parents: 16963
diff changeset
    62
    filter = lambda x: x
41036
73da729ccfef test: introduce a new flag to display env variable line per line
Boris Feld <boris.feld@octobus.net>
parents: 41025
diff changeset
    63
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41036
diff changeset
    64
vars = [
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41036
diff changeset
    65
    b"%s=%s" % (k.encode('ascii'), filter(v).encode('ascii')) for k, v in env
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41036
diff changeset
    66
]
41036
73da729ccfef test: introduce a new flag to display env variable line per line
Boris Feld <boris.feld@octobus.net>
parents: 41025
diff changeset
    67
73da729ccfef test: introduce a new flag to display env variable line per line
Boris Feld <boris.feld@octobus.net>
parents: 41025
diff changeset
    68
# Print variables on out
73da729ccfef test: introduce a new flag to display env variable line per line
Boris Feld <boris.feld@octobus.net>
parents: 41025
diff changeset
    69
if not args.line:
73da729ccfef test: introduce a new flag to display env variable line per line
Boris Feld <boris.feld@octobus.net>
parents: 41025
diff changeset
    70
    out.write(b" ".join(vars))
73da729ccfef test: introduce a new flag to display env variable line per line
Boris Feld <boris.feld@octobus.net>
parents: 41025
diff changeset
    71
else:
73da729ccfef test: introduce a new flag to display env variable line per line
Boris Feld <boris.feld@octobus.net>
parents: 41025
diff changeset
    72
    for var in vars:
73da729ccfef test: introduce a new flag to display env variable line per line
Boris Feld <boris.feld@octobus.net>
parents: 41025
diff changeset
    73
        out.write(var)
73da729ccfef test: introduce a new flag to display env variable line per line
Boris Feld <boris.feld@octobus.net>
parents: 41025
diff changeset
    74
        out.write(b"\n")
73da729ccfef test: introduce a new flag to display env variable line per line
Boris Feld <boris.feld@octobus.net>
parents: 41025
diff changeset
    75
38111
bacbe829c2bf py3: use bytes in tests/printenv.py
Pulkit Goyal <7895pulkit@gmail.com>
parents: 36254
diff changeset
    76
out.write(b"\n")
4285
4fd6f7e60894 Add tests/printenv.py
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    77
out.close()
4fd6f7e60894 Add tests/printenv.py
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    78
41025
3c5aaea9638f tests: update printenv.py argument parsing
Boris Feld <boris.feld@octobus.net>
parents: 38296
diff changeset
    79
sys.exit(args.exitcode)