contrib/packaging/packaging.py
author Gregory Szorc <gregory.szorc@gmail.com>
Wed, 23 Oct 2019 18:37:36 -0700
changeset 43513 081a77df7bc6
child 43660 303bf312d5ed
permissions -rwxr-xr-x
packaging: consolidate CLI functionality into packaging.py Consolidating functionality for invoking code in the hgpackaging package through a single CLI entry point will make things simpler when we add more complexity to that package. For example, it will allow us to run things out of a virtualenv with third party packages. This commit consolidates functionality from the Inno and WiX build.py scripts into a new packaging.py script. That script simply creates a virtualenv and runs the CLI functionality in it. The new virtualenv is populated with jinja2 because I felt it easier to incorporate requirements file processing in this commit and we will soon use jinja2 in an upcoming commit. The unified CLI functionality will also make it easier to script other packaging workflows going forward. e.g. RPM, Debian, and macOS packaging. Differential Revision: https://phab.mercurial-scm.org/D7156
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
43513
081a77df7bc6 packaging: consolidate CLI functionality into packaging.py
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     1
#!/usr/bin/env python3
081a77df7bc6 packaging: consolidate CLI functionality into packaging.py
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     2
#
081a77df7bc6 packaging: consolidate CLI functionality into packaging.py
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     3
# packaging.py - Mercurial packaging functionality
081a77df7bc6 packaging: consolidate CLI functionality into packaging.py
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     4
#
081a77df7bc6 packaging: consolidate CLI functionality into packaging.py
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     5
# Copyright 2019 Gregory Szorc <gregory.szorc@gmail.com>
081a77df7bc6 packaging: consolidate CLI functionality into packaging.py
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     6
#
081a77df7bc6 packaging: consolidate CLI functionality into packaging.py
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     7
# This software may be used and distributed according to the terms of the
081a77df7bc6 packaging: consolidate CLI functionality into packaging.py
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     8
# GNU General Public License version 2 or any later version.
081a77df7bc6 packaging: consolidate CLI functionality into packaging.py
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     9
081a77df7bc6 packaging: consolidate CLI functionality into packaging.py
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    10
import os
081a77df7bc6 packaging: consolidate CLI functionality into packaging.py
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    11
import pathlib
081a77df7bc6 packaging: consolidate CLI functionality into packaging.py
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    12
import subprocess
081a77df7bc6 packaging: consolidate CLI functionality into packaging.py
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    13
import sys
081a77df7bc6 packaging: consolidate CLI functionality into packaging.py
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    14
import venv
081a77df7bc6 packaging: consolidate CLI functionality into packaging.py
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    15
081a77df7bc6 packaging: consolidate CLI functionality into packaging.py
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    16
081a77df7bc6 packaging: consolidate CLI functionality into packaging.py
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    17
HERE = pathlib.Path(os.path.abspath(__file__)).parent
081a77df7bc6 packaging: consolidate CLI functionality into packaging.py
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    18
REQUIREMENTS_TXT = HERE / "requirements.txt"
081a77df7bc6 packaging: consolidate CLI functionality into packaging.py
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    19
SOURCE_DIR = HERE.parent.parent
081a77df7bc6 packaging: consolidate CLI functionality into packaging.py
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    20
VENV = SOURCE_DIR / "build" / "venv-packaging"
081a77df7bc6 packaging: consolidate CLI functionality into packaging.py
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    21
081a77df7bc6 packaging: consolidate CLI functionality into packaging.py
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    22
081a77df7bc6 packaging: consolidate CLI functionality into packaging.py
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    23
def bootstrap():
081a77df7bc6 packaging: consolidate CLI functionality into packaging.py
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    24
    venv_created = not VENV.exists()
081a77df7bc6 packaging: consolidate CLI functionality into packaging.py
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    25
081a77df7bc6 packaging: consolidate CLI functionality into packaging.py
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    26
    VENV.parent.mkdir(exist_ok=True)
081a77df7bc6 packaging: consolidate CLI functionality into packaging.py
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    27
081a77df7bc6 packaging: consolidate CLI functionality into packaging.py
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    28
    venv.create(VENV, with_pip=True)
081a77df7bc6 packaging: consolidate CLI functionality into packaging.py
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    29
081a77df7bc6 packaging: consolidate CLI functionality into packaging.py
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    30
    if os.name == "nt":
081a77df7bc6 packaging: consolidate CLI functionality into packaging.py
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    31
        venv_bin = VENV / "Scripts"
081a77df7bc6 packaging: consolidate CLI functionality into packaging.py
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    32
        pip = venv_bin / "pip.exe"
081a77df7bc6 packaging: consolidate CLI functionality into packaging.py
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    33
        python = venv_bin / "python.exe"
081a77df7bc6 packaging: consolidate CLI functionality into packaging.py
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    34
    else:
081a77df7bc6 packaging: consolidate CLI functionality into packaging.py
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    35
        venv_bin = VENV / "bin"
081a77df7bc6 packaging: consolidate CLI functionality into packaging.py
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    36
        pip = venv_bin / "pip"
081a77df7bc6 packaging: consolidate CLI functionality into packaging.py
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    37
        python = venv_bin / "python"
081a77df7bc6 packaging: consolidate CLI functionality into packaging.py
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    38
081a77df7bc6 packaging: consolidate CLI functionality into packaging.py
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    39
    args = [
081a77df7bc6 packaging: consolidate CLI functionality into packaging.py
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    40
        str(pip),
081a77df7bc6 packaging: consolidate CLI functionality into packaging.py
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    41
        "install",
081a77df7bc6 packaging: consolidate CLI functionality into packaging.py
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    42
        "-r",
081a77df7bc6 packaging: consolidate CLI functionality into packaging.py
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    43
        str(REQUIREMENTS_TXT),
081a77df7bc6 packaging: consolidate CLI functionality into packaging.py
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    44
        "--disable-pip-version-check",
081a77df7bc6 packaging: consolidate CLI functionality into packaging.py
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    45
    ]
081a77df7bc6 packaging: consolidate CLI functionality into packaging.py
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    46
081a77df7bc6 packaging: consolidate CLI functionality into packaging.py
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    47
    if not venv_created:
081a77df7bc6 packaging: consolidate CLI functionality into packaging.py
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    48
        args.append("-q")
081a77df7bc6 packaging: consolidate CLI functionality into packaging.py
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    49
081a77df7bc6 packaging: consolidate CLI functionality into packaging.py
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    50
    subprocess.run(args, check=True)
081a77df7bc6 packaging: consolidate CLI functionality into packaging.py
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    51
081a77df7bc6 packaging: consolidate CLI functionality into packaging.py
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    52
    os.environ["HGPACKAGING_BOOTSTRAPPED"] = "1"
081a77df7bc6 packaging: consolidate CLI functionality into packaging.py
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    53
    os.environ["PATH"] = "%s%s%s" % (venv_bin, os.pathsep, os.environ["PATH"])
081a77df7bc6 packaging: consolidate CLI functionality into packaging.py
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    54
081a77df7bc6 packaging: consolidate CLI functionality into packaging.py
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    55
    subprocess.run([str(python), __file__] + sys.argv[1:], check=True)
081a77df7bc6 packaging: consolidate CLI functionality into packaging.py
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    56
081a77df7bc6 packaging: consolidate CLI functionality into packaging.py
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    57
081a77df7bc6 packaging: consolidate CLI functionality into packaging.py
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    58
def run():
081a77df7bc6 packaging: consolidate CLI functionality into packaging.py
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    59
    import hgpackaging.cli as cli
081a77df7bc6 packaging: consolidate CLI functionality into packaging.py
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    60
081a77df7bc6 packaging: consolidate CLI functionality into packaging.py
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    61
    # Need to strip off main Python executable.
081a77df7bc6 packaging: consolidate CLI functionality into packaging.py
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    62
    cli.main()
081a77df7bc6 packaging: consolidate CLI functionality into packaging.py
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    63
081a77df7bc6 packaging: consolidate CLI functionality into packaging.py
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    64
081a77df7bc6 packaging: consolidate CLI functionality into packaging.py
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    65
if __name__ == "__main__":
081a77df7bc6 packaging: consolidate CLI functionality into packaging.py
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    66
    try:
081a77df7bc6 packaging: consolidate CLI functionality into packaging.py
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    67
        if "HGPACKAGING_BOOTSTRAPPED" not in os.environ:
081a77df7bc6 packaging: consolidate CLI functionality into packaging.py
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    68
            bootstrap()
081a77df7bc6 packaging: consolidate CLI functionality into packaging.py
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    69
        else:
081a77df7bc6 packaging: consolidate CLI functionality into packaging.py
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    70
            run()
081a77df7bc6 packaging: consolidate CLI functionality into packaging.py
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    71
    except subprocess.CalledProcessError as e:
081a77df7bc6 packaging: consolidate CLI functionality into packaging.py
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    72
        sys.exit(e.returncode)
081a77df7bc6 packaging: consolidate CLI functionality into packaging.py
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    73
    except KeyboardInterrupt:
081a77df7bc6 packaging: consolidate CLI functionality into packaging.py
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    74
        sys.exit(1)