|
| 1 | +#! /usr/bin/env python |
| 2 | +"""A pip-wrapper that injects platform-specific constraints into pip.""" |
| 3 | +from __future__ import print_function # noqa: WPS422 |
| 4 | + |
| 5 | +import functools |
| 6 | +import os |
| 7 | +import platform |
| 8 | +import subprocess # noqa: S404 |
| 9 | +import sys |
| 10 | + |
| 11 | +print_info = functools.partial(print, file=sys.stderr) |
| 12 | + |
| 13 | + |
| 14 | +def get_runtime_python_tag(): |
| 15 | + """Identify the Python tag of the current runtime. |
| 16 | +
|
| 17 | + :returns: Python tag. |
| 18 | + """ |
| 19 | + python_minor_ver = sys.version_info[:2] |
| 20 | + python_implementation = platform.python_implementation() |
| 21 | + |
| 22 | + if python_implementation == 'PyPy': |
| 23 | + python_tag_prefix = 'py' |
| 24 | + else: |
| 25 | + python_tag_prefix = 'cp' |
| 26 | + |
| 27 | + # pylint: disable=possibly-unused-variable |
| 28 | + python_minor_ver_tag = ''.join(map(str, python_minor_ver)) |
| 29 | + |
| 30 | + python_tag = ( |
| 31 | + '{python_tag_prefix!s}{python_minor_ver_tag!s}'. |
| 32 | + format(**locals()) # noqa: WPS421 |
| 33 | + ) |
| 34 | + |
| 35 | + if python_tag_prefix == 'cp' and python_implementation == 'CPython': |
| 36 | + print_info( |
| 37 | + 'WARNING: The chosen Python interpreter tag "{python_tag}" ' |
| 38 | + 'may not match the current ' |
| 39 | + 'Python implementation "{python_implementation}"'. |
| 40 | + format(**locals()), # noqa: WPS421 |
| 41 | + ) |
| 42 | + |
| 43 | + return python_tag |
| 44 | + |
| 45 | + |
| 46 | +def get_constraint_file_path(req_dir, toxenv, python_tag): |
| 47 | + """Identify the constraints filename for the current environment. |
| 48 | +
|
| 49 | + :param req_dir: Requirements directory. |
| 50 | + :param toxenv: tox testenv. |
| 51 | + :param python_tag: Python tag. |
| 52 | +
|
| 53 | + :returns: Constraints filename for the current environment. |
| 54 | + """ |
| 55 | + sys_platform = sys.platform |
| 56 | + platform_machine = platform.machine() |
| 57 | + |
| 58 | + if toxenv in {'py', 'python'}: |
| 59 | + toxenv = 'py{ver}'.format(ver=python_tag[2:]) |
| 60 | + |
| 61 | + if sys_platform == 'linux2': |
| 62 | + sys_platform = 'linux' |
| 63 | + |
| 64 | + constraint_name = ( |
| 65 | + 'tox-{toxenv}-{python_tag}-{sys_platform}-{platform_machine}'. |
| 66 | + format(**locals()) # noqa: WPS421 |
| 67 | + ) |
| 68 | + return os.path.join(req_dir, os.path.extsep.join((constraint_name, 'txt'))) |
| 69 | + |
| 70 | + |
| 71 | +def make_pip_cmd(pip_args, constraint_file_path): |
| 72 | + """Inject a lockfile constraint into the pip command if present. |
| 73 | +
|
| 74 | + :param pip_args: pip arguments. |
| 75 | + :param constraint_file_path: Path to a ``constraints.txt``-compatible file. |
| 76 | +
|
| 77 | + :returns: pip command. |
| 78 | + """ |
| 79 | + pip_cmd = [sys.executable, '-m', 'pip'] + pip_args |
| 80 | + if os.path.isfile(constraint_file_path): |
| 81 | + pip_cmd += ['--constraint', constraint_file_path] |
| 82 | + else: |
| 83 | + print_info( |
| 84 | + 'WARNING: The expected pinned constraints file for the current env' |
| 85 | + 'does not exist (should be "{constraint_file_path}").'. |
| 86 | + format(**locals()), # noqa: WPS421 |
| 87 | + ) |
| 88 | + return pip_cmd |
| 89 | + |
| 90 | + |
| 91 | +def run_cmd(cmd): |
| 92 | + """Invoke a shell command after logging it. |
| 93 | +
|
| 94 | + :param cmd: The command to invoke. |
| 95 | + """ |
| 96 | + print_info( |
| 97 | + 'Invoking the following command: {cmd}'. |
| 98 | + format(cmd=' '.join(cmd)), |
| 99 | + ) |
| 100 | + subprocess.check_call(cmd) # noqa: S603 |
| 101 | + |
| 102 | + |
| 103 | +def main(argv): |
| 104 | + """Invoke pip with the matching constraints file, if present. |
| 105 | +
|
| 106 | + :param argv: List of command-line arguments. |
| 107 | + """ |
| 108 | + constraint_file_path = get_constraint_file_path( |
| 109 | + req_dir=argv[1], |
| 110 | + toxenv=argv[0], |
| 111 | + python_tag=get_runtime_python_tag(), |
| 112 | + ) |
| 113 | + pip_cmd = make_pip_cmd( |
| 114 | + pip_args=argv[2:], |
| 115 | + constraint_file_path=constraint_file_path, |
| 116 | + ) |
| 117 | + run_cmd(pip_cmd) |
| 118 | + |
| 119 | + |
| 120 | +if __name__ == '__main__': |
| 121 | + main(sys.argv[1:]) |
0 commit comments