contrib/packaging/packaging.py
author Manuel Jacob <me@manueljacob.de>
Mon, 11 Jul 2022 01:51:20 +0200
branchstable
changeset 49378 094a5fa3cf52
parent 43660 303bf312d5ed
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:
43660
303bf312d5ed packaging: convert to UNIX line endings
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43513
diff changeset
     1
#!/usr/bin/env python3
303bf312d5ed packaging: convert to UNIX line endings
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43513
diff changeset
     2
#
303bf312d5ed packaging: convert to UNIX line endings
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43513
diff changeset
     3
# packaging.py - Mercurial packaging functionality
303bf312d5ed packaging: convert to UNIX line endings
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43513
diff changeset
     4
#
303bf312d5ed packaging: convert to UNIX line endings
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43513
diff changeset
     5
# Copyright 2019 Gregory Szorc <gregory.szorc@gmail.com>
303bf312d5ed packaging: convert to UNIX line endings
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43513
diff changeset
     6
#
303bf312d5ed packaging: convert to UNIX line endings
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43513
diff changeset
     7
# This software may be used and distributed according to the terms of the
303bf312d5ed packaging: convert to UNIX line endings
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43513
diff changeset
     8
# GNU General Public License version 2 or any later version.
303bf312d5ed packaging: convert to UNIX line endings
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43513
diff changeset
     9
303bf312d5ed packaging: convert to UNIX line endings
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43513
diff changeset
    10
import os
303bf312d5ed packaging: convert to UNIX line endings
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43513
diff changeset
    11
import pathlib
303bf312d5ed packaging: convert to UNIX line endings
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43513
diff changeset
    12
import subprocess
303bf312d5ed packaging: convert to UNIX line endings
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43513
diff changeset
    13
import sys
303bf312d5ed packaging: convert to UNIX line endings
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43513
diff changeset
    14
import venv
303bf312d5ed packaging: convert to UNIX line endings
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43513
diff changeset
    15
303bf312d5ed packaging: convert to UNIX line endings
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43513
diff changeset
    16
303bf312d5ed packaging: convert to UNIX line endings
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43513
diff changeset
    17
HERE = pathlib.Path(os.path.abspath(__file__)).parent
303bf312d5ed packaging: convert to UNIX line endings
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43513
diff changeset
    18
REQUIREMENTS_TXT = HERE / "requirements.txt"
303bf312d5ed packaging: convert to UNIX line endings
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43513
diff changeset
    19
SOURCE_DIR = HERE.parent.parent
303bf312d5ed packaging: convert to UNIX line endings
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43513
diff changeset
    20
VENV = SOURCE_DIR / "build" / "venv-packaging"
303bf312d5ed packaging: convert to UNIX line endings
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43513
diff changeset
    21
303bf312d5ed packaging: convert to UNIX line endings
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43513
diff changeset
    22
303bf312d5ed packaging: convert to UNIX line endings
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43513
diff changeset
    23
def bootstrap():
303bf312d5ed packaging: convert to UNIX line endings
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43513
diff changeset
    24
    venv_created = not VENV.exists()
303bf312d5ed packaging: convert to UNIX line endings
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43513
diff changeset
    25
303bf312d5ed packaging: convert to UNIX line endings
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43513
diff changeset
    26
    VENV.parent.mkdir(exist_ok=True)
303bf312d5ed packaging: convert to UNIX line endings
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43513
diff changeset
    27
303bf312d5ed packaging: convert to UNIX line endings
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43513
diff changeset
    28
    venv.create(VENV, with_pip=True)
303bf312d5ed packaging: convert to UNIX line endings
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43513
diff changeset
    29
303bf312d5ed packaging: convert to UNIX line endings
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43513
diff changeset
    30
    if os.name == "nt":
303bf312d5ed packaging: convert to UNIX line endings
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43513
diff changeset
    31
        venv_bin = VENV / "Scripts"
303bf312d5ed packaging: convert to UNIX line endings
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43513
diff changeset
    32
        pip = venv_bin / "pip.exe"
303bf312d5ed packaging: convert to UNIX line endings
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43513
diff changeset
    33
        python = venv_bin / "python.exe"
303bf312d5ed packaging: convert to UNIX line endings
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43513
diff changeset
    34
    else:
303bf312d5ed packaging: convert to UNIX line endings
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43513
diff changeset
    35
        venv_bin = VENV / "bin"
303bf312d5ed packaging: convert to UNIX line endings
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43513
diff changeset
    36
        pip = venv_bin / "pip"
303bf312d5ed packaging: convert to UNIX line endings
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43513
diff changeset
    37
        python = venv_bin / "python"
303bf312d5ed packaging: convert to UNIX line endings
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43513
diff changeset
    38
303bf312d5ed packaging: convert to UNIX line endings
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43513
diff changeset
    39
    args = [
303bf312d5ed packaging: convert to UNIX line endings
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43513
diff changeset
    40
        str(pip),
303bf312d5ed packaging: convert to UNIX line endings
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43513
diff changeset
    41
        "install",
303bf312d5ed packaging: convert to UNIX line endings
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43513
diff changeset
    42
        "-r",
303bf312d5ed packaging: convert to UNIX line endings
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43513
diff changeset
    43
        str(REQUIREMENTS_TXT),
303bf312d5ed packaging: convert to UNIX line endings
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43513
diff changeset
    44
        "--disable-pip-version-check",
303bf312d5ed packaging: convert to UNIX line endings
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43513
diff changeset
    45
    ]
303bf312d5ed packaging: convert to UNIX line endings
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43513
diff changeset
    46
303bf312d5ed packaging: convert to UNIX line endings
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43513
diff changeset
    47
    if not venv_created:
303bf312d5ed packaging: convert to UNIX line endings
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43513
diff changeset
    48
        args.append("-q")
303bf312d5ed packaging: convert to UNIX line endings
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43513
diff changeset
    49
303bf312d5ed packaging: convert to UNIX line endings
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43513
diff changeset
    50
    subprocess.run(args, check=True)
303bf312d5ed packaging: convert to UNIX line endings
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43513
diff changeset
    51
303bf312d5ed packaging: convert to UNIX line endings
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43513
diff changeset
    52
    os.environ["HGPACKAGING_BOOTSTRAPPED"] = "1"
303bf312d5ed packaging: convert to UNIX line endings
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43513
diff changeset
    53
    os.environ["PATH"] = "%s%s%s" % (venv_bin, os.pathsep, os.environ["PATH"])
303bf312d5ed packaging: convert to UNIX line endings
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43513
diff changeset
    54
303bf312d5ed packaging: convert to UNIX line endings
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43513
diff changeset
    55
    subprocess.run([str(python), __file__] + sys.argv[1:], check=True)
303bf312d5ed packaging: convert to UNIX line endings
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43513
diff changeset
    56
303bf312d5ed packaging: convert to UNIX line endings
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43513
diff changeset
    57
303bf312d5ed packaging: convert to UNIX line endings
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43513
diff changeset
    58
def run():
303bf312d5ed packaging: convert to UNIX line endings
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43513
diff changeset
    59
    import hgpackaging.cli as cli
303bf312d5ed packaging: convert to UNIX line endings
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43513
diff changeset
    60
303bf312d5ed packaging: convert to UNIX line endings
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43513
diff changeset
    61
    # Need to strip off main Python executable.
303bf312d5ed packaging: convert to UNIX line endings
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43513
diff changeset
    62
    cli.main()
303bf312d5ed packaging: convert to UNIX line endings
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43513
diff changeset
    63
303bf312d5ed packaging: convert to UNIX line endings
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43513
diff changeset
    64
303bf312d5ed packaging: convert to UNIX line endings
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43513
diff changeset
    65
if __name__ == "__main__":
303bf312d5ed packaging: convert to UNIX line endings
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43513
diff changeset
    66
    try:
303bf312d5ed packaging: convert to UNIX line endings
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43513
diff changeset
    67
        if "HGPACKAGING_BOOTSTRAPPED" not in os.environ:
303bf312d5ed packaging: convert to UNIX line endings
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43513
diff changeset
    68
            bootstrap()
303bf312d5ed packaging: convert to UNIX line endings
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43513
diff changeset
    69
        else:
303bf312d5ed packaging: convert to UNIX line endings
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43513
diff changeset
    70
            run()
303bf312d5ed packaging: convert to UNIX line endings
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43513
diff changeset
    71
    except subprocess.CalledProcessError as e:
303bf312d5ed packaging: convert to UNIX line endings
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43513
diff changeset
    72
        sys.exit(e.returncode)
303bf312d5ed packaging: convert to UNIX line endings
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43513
diff changeset
    73
    except KeyboardInterrupt:
303bf312d5ed packaging: convert to UNIX line endings
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43513
diff changeset
    74
        sys.exit(1)