contrib/packaging/packaging.py
author Pierre-Yves David <pierre-yves.david@octobus.net>
Tue, 13 Jul 2021 13:06:50 +0200
changeset 47665 a4443f66be6a
parent 43660 303bf312d5ed
permissions -rwxr-xr-x
dirstate-item: add a `from_v1_data` constructor This class method is dedicated to building a DirstateItem from the data available in the "dirstate-v1" format. Since that format is frozen, this constructor will never change (unlike the `__init__` one). Differential Revision: https://phab.mercurial-scm.org/D11118
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)