Mike Frysinger | 1ca1443 | 2020-02-16 00:18:56 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Mike Frysinger | f1ba7ad | 2022-09-12 05:42:57 -0400 | [diff] [blame] | 2 | # Copyright 2016 The ChromiumOS Authors |
Aviv Keshet | 39853bd | 2016-09-22 15:12:05 -0700 | [diff] [blame] | 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
| 6 | """Wrapper around chromite executable scripts that use virtualenv.""" |
| 7 | |
Aviv Keshet | 39853bd | 2016-09-22 15:12:05 -0700 | [diff] [blame] | 8 | import os |
Allen Li | 465a0d6 | 2016-11-30 14:55:08 -0800 | [diff] [blame] | 9 | import subprocess |
Aviv Keshet | 39853bd | 2016-09-22 15:12:05 -0700 | [diff] [blame] | 10 | import sys |
Allen Li | 465a0d6 | 2016-11-30 14:55:08 -0800 | [diff] [blame] | 11 | |
Mike Frysinger | 807d828 | 2022-04-28 22:45:17 -0400 | [diff] [blame] | 12 | |
Greg Edelston | a4c9b3b | 2020-01-07 17:51:13 -0700 | [diff] [blame] | 13 | try: |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 14 | import pytest # pylint: disable=import-error |
| 15 | |
| 16 | wrapper3 = pytest.importorskip( |
| 17 | "wrapper3", reason="File must be run in venv" |
| 18 | ) |
Greg Edelston | a4c9b3b | 2020-01-07 17:51:13 -0700 | [diff] [blame] | 19 | except ImportError: |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 20 | import wrapper3 |
Aviv Keshet | 39853bd | 2016-09-22 15:12:05 -0700 | [diff] [blame] | 21 | |
Don Garrett | 863f90b | 2017-04-19 17:04:20 -0700 | [diff] [blame] | 22 | _CHROMITE_DIR = os.path.realpath( |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 23 | os.path.join(os.path.abspath(__file__), "..", "..") |
| 24 | ) |
Allen Li | 9f03f5b | 2016-12-13 15:23:52 -0800 | [diff] [blame] | 25 | |
Allen Li | 465a0d6 | 2016-11-30 14:55:08 -0800 | [diff] [blame] | 26 | # _VIRTUALENV_DIR contains the scripts for working with venvs |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 27 | _VIRTUALENV_DIR = os.path.join(_CHROMITE_DIR, "..", "infra_virtualenv") |
| 28 | _CREATE_VENV_PATH = os.path.join(_VIRTUALENV_DIR, "bin", "create_venv3") |
| 29 | _REQUIREMENTS = os.path.join(_CHROMITE_DIR, "venv", "requirements.txt") |
Allen Li | 465a0d6 | 2016-11-30 14:55:08 -0800 | [diff] [blame] | 30 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 31 | _VENV_MARKER = "INSIDE_CHROMITE_VENV" |
Allen Li | 9f03f5b | 2016-12-13 15:23:52 -0800 | [diff] [blame] | 32 | |
Allen Li | 465a0d6 | 2016-11-30 14:55:08 -0800 | [diff] [blame] | 33 | |
| 34 | def main(): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 35 | if _IsInsideVenv(os.environ): |
Trent Apted | 4a0812b | 2023-05-15 15:33:55 +1000 | [diff] [blame^] | 36 | # Don't bleed the marker into children processes that might use the |
| 37 | # wrapper themselves to run inside of the virtualenv. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 38 | os.environ.pop(_VENV_MARKER) |
| 39 | wrapper3.DoMain() |
| 40 | else: |
| 41 | venvdir = _CreateVenv() |
| 42 | _ExecInVenv(venvdir, sys.argv) |
Allen Li | 465a0d6 | 2016-11-30 14:55:08 -0800 | [diff] [blame] | 43 | |
| 44 | |
| 45 | def _CreateVenv(): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 46 | """Create or update chromite venv.""" |
| 47 | result = subprocess.run( |
| 48 | [_CREATE_VENV_PATH, _REQUIREMENTS], |
| 49 | check=False, |
| 50 | stdout=subprocess.PIPE, |
| 51 | encoding="utf-8", |
| 52 | ) |
| 53 | if result.returncode: |
| 54 | print( |
| 55 | f'{os.path.basename(sys.argv[0])}: error: {" ".join(result.args)}: ' |
| 56 | f"exited {result.returncode}", |
| 57 | file=sys.stderr, |
| 58 | ) |
| 59 | sys.exit(result.returncode) |
| 60 | return result.stdout.strip() |
Allen Li | 465a0d6 | 2016-11-30 14:55:08 -0800 | [diff] [blame] | 61 | |
| 62 | |
Allen Li | 68f42e2 | 2017-03-27 17:08:59 -0700 | [diff] [blame] | 63 | def _ExecInVenv(venvdir, args): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 64 | """Exec command in chromite venv. |
Allen Li | 465a0d6 | 2016-11-30 14:55:08 -0800 | [diff] [blame] | 65 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 66 | Args: |
Trent Apted | c20bb6d | 2023-05-10 15:00:03 +1000 | [diff] [blame] | 67 | venvdir: virtualenv directory |
| 68 | args: Sequence of arguments. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 69 | """ |
| 70 | venv_python = os.path.join(venvdir, "bin", "python") |
| 71 | os.execve( |
| 72 | venv_python, |
| 73 | [venv_python] + list(args), |
| 74 | _CreateVenvEnvironment(os.environ), |
| 75 | ) |
Allen Li | 465a0d6 | 2016-11-30 14:55:08 -0800 | [diff] [blame] | 76 | |
| 77 | |
Allen Li | 9f03f5b | 2016-12-13 15:23:52 -0800 | [diff] [blame] | 78 | def _CreateVenvEnvironment(env_dict): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 79 | """Create environment for a virtualenv. |
Allen Li | 9f03f5b | 2016-12-13 15:23:52 -0800 | [diff] [blame] | 80 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 81 | This adds a special marker variable to a copy of the input environment dict |
| 82 | and returns the copy. |
Allen Li | 9f03f5b | 2016-12-13 15:23:52 -0800 | [diff] [blame] | 83 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 84 | Args: |
Trent Apted | c20bb6d | 2023-05-10 15:00:03 +1000 | [diff] [blame] | 85 | env_dict: Environment variable dict to use as base, which is not |
| 86 | modified. |
Allen Li | 9f03f5b | 2016-12-13 15:23:52 -0800 | [diff] [blame] | 87 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 88 | Returns: |
Trent Apted | c20bb6d | 2023-05-10 15:00:03 +1000 | [diff] [blame] | 89 | New environment dict for a virtualenv. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 90 | """ |
| 91 | new_env_dict = env_dict.copy() |
| 92 | new_env_dict[_VENV_MARKER] = "1" |
| 93 | new_env_dict.pop("PYTHONPATH", None) |
| 94 | return new_env_dict |
Allen Li | 9f03f5b | 2016-12-13 15:23:52 -0800 | [diff] [blame] | 95 | |
| 96 | |
| 97 | def _IsInsideVenv(env_dict): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 98 | """Return whether the environment dict is running inside a virtualenv. |
Allen Li | 9f03f5b | 2016-12-13 15:23:52 -0800 | [diff] [blame] | 99 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 100 | This checks the environment dict for the special marker added by |
| 101 | _CreateVenvEnvironment(). |
Allen Li | 9f03f5b | 2016-12-13 15:23:52 -0800 | [diff] [blame] | 102 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 103 | Args: |
Trent Apted | c20bb6d | 2023-05-10 15:00:03 +1000 | [diff] [blame] | 104 | env_dict: Environment variable dict to check |
Allen Li | 9f03f5b | 2016-12-13 15:23:52 -0800 | [diff] [blame] | 105 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 106 | Returns: |
Trent Apted | c20bb6d | 2023-05-10 15:00:03 +1000 | [diff] [blame] | 107 | A true value if inside virtualenv, else a false value. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 108 | """ |
| 109 | # Checking sys.prefix or doing any kind of path check is unreliable because |
| 110 | # we check out chromite to weird places. |
| 111 | return env_dict.get(_VENV_MARKER, "") |
Aviv Keshet | 39853bd | 2016-09-22 15:12:05 -0700 | [diff] [blame] | 112 | |
| 113 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 114 | if __name__ == "__main__": |
| 115 | main() |