Mike Frysinger | 1ca1443 | 2020-02-16 00:18:56 -0500 | [diff] [blame^] | 1 | #!/usr/bin/env python3 |
Mike Frysinger | e58c0e2 | 2017-10-04 15:43:30 -0400 | [diff] [blame] | 2 | # -*- coding: utf-8 -*- |
Aviv Keshet | 39853bd | 2016-09-22 15:12:05 -0700 | [diff] [blame] | 3 | # Copyright 2016 The Chromium OS Authors. All rights reserved. |
| 4 | # Use of this source code is governed by a BSD-style license that can be |
| 5 | # found in the LICENSE file. |
| 6 | |
| 7 | """Wrapper around chromite executable scripts that use virtualenv.""" |
| 8 | |
| 9 | from __future__ import print_function |
| 10 | |
| 11 | import os |
Allen Li | 465a0d6 | 2016-11-30 14:55:08 -0800 | [diff] [blame] | 12 | import subprocess |
Aviv Keshet | 39853bd | 2016-09-22 15:12:05 -0700 | [diff] [blame] | 13 | import sys |
Allen Li | 465a0d6 | 2016-11-30 14:55:08 -0800 | [diff] [blame] | 14 | |
Greg Edelston | a4c9b3b | 2020-01-07 17:51:13 -0700 | [diff] [blame] | 15 | try: |
| 16 | import pytest # pylint: disable=import-error |
Mike Frysinger | 1ca1443 | 2020-02-16 00:18:56 -0500 | [diff] [blame^] | 17 | wrapper3 = pytest.importorskip('wrapper3', reason='File must be run in venv') |
Greg Edelston | a4c9b3b | 2020-01-07 17:51:13 -0700 | [diff] [blame] | 18 | except ImportError: |
Mike Frysinger | 1ca1443 | 2020-02-16 00:18:56 -0500 | [diff] [blame^] | 19 | import wrapper3 |
Aviv Keshet | 39853bd | 2016-09-22 15:12:05 -0700 | [diff] [blame] | 20 | |
Don Garrett | 863f90b | 2017-04-19 17:04:20 -0700 | [diff] [blame] | 21 | _CHROMITE_DIR = os.path.realpath( |
| 22 | os.path.join(os.path.abspath(__file__), '..', '..')) |
Allen Li | 9f03f5b | 2016-12-13 15:23:52 -0800 | [diff] [blame] | 23 | |
Allen Li | 465a0d6 | 2016-11-30 14:55:08 -0800 | [diff] [blame] | 24 | # _VIRTUALENV_DIR contains the scripts for working with venvs |
Allen Li | c559824 | 2016-12-21 11:09:23 -0800 | [diff] [blame] | 25 | _VIRTUALENV_DIR = os.path.join(_CHROMITE_DIR, '..', 'infra_virtualenv') |
Mike Frysinger | 1ca1443 | 2020-02-16 00:18:56 -0500 | [diff] [blame^] | 26 | _CREATE_VENV_PATH = os.path.join(_VIRTUALENV_DIR, 'bin', 'create_venv3') |
Allen Li | 465a0d6 | 2016-11-30 14:55:08 -0800 | [diff] [blame] | 27 | _REQUIREMENTS = os.path.join(_CHROMITE_DIR, 'venv', 'requirements.txt') |
| 28 | |
Allen Li | 9f03f5b | 2016-12-13 15:23:52 -0800 | [diff] [blame] | 29 | _VENV_MARKER = 'INSIDE_CHROMITE_VENV' |
| 30 | |
Allen Li | 465a0d6 | 2016-11-30 14:55:08 -0800 | [diff] [blame] | 31 | |
| 32 | def main(): |
Allen Li | 9f03f5b | 2016-12-13 15:23:52 -0800 | [diff] [blame] | 33 | if _IsInsideVenv(os.environ): |
Mike Frysinger | 1ca1443 | 2020-02-16 00:18:56 -0500 | [diff] [blame^] | 34 | wrapper3.DoMain() |
Allen Li | 465a0d6 | 2016-11-30 14:55:08 -0800 | [diff] [blame] | 35 | else: |
Allen Li | 68f42e2 | 2017-03-27 17:08:59 -0700 | [diff] [blame] | 36 | venvdir = _CreateVenv() |
| 37 | _ExecInVenv(venvdir, sys.argv) |
Allen Li | 465a0d6 | 2016-11-30 14:55:08 -0800 | [diff] [blame] | 38 | |
| 39 | |
| 40 | def _CreateVenv(): |
| 41 | """Create or update chromite venv.""" |
Allen Li | 68f42e2 | 2017-03-27 17:08:59 -0700 | [diff] [blame] | 42 | return subprocess.check_output([ |
Allen Li | 9f03f5b | 2016-12-13 15:23:52 -0800 | [diff] [blame] | 43 | _CREATE_VENV_PATH, |
Allen Li | 465a0d6 | 2016-11-30 14:55:08 -0800 | [diff] [blame] | 44 | _REQUIREMENTS, |
Mike Frysinger | e3da4c9 | 2019-10-13 23:00:42 -0400 | [diff] [blame] | 45 | ]).rstrip().decode('utf-8') |
Allen Li | 465a0d6 | 2016-11-30 14:55:08 -0800 | [diff] [blame] | 46 | |
| 47 | |
Allen Li | 68f42e2 | 2017-03-27 17:08:59 -0700 | [diff] [blame] | 48 | def _ExecInVenv(venvdir, args): |
Allen Li | 465a0d6 | 2016-11-30 14:55:08 -0800 | [diff] [blame] | 49 | """Exec command in chromite venv. |
| 50 | |
| 51 | Args: |
Allen Li | 68f42e2 | 2017-03-27 17:08:59 -0700 | [diff] [blame] | 52 | venvdir: virtualenv directory |
Allen Li | 465a0d6 | 2016-11-30 14:55:08 -0800 | [diff] [blame] | 53 | args: Sequence of arguments. |
| 54 | """ |
Allen Li | 68f42e2 | 2017-03-27 17:08:59 -0700 | [diff] [blame] | 55 | venv_python = os.path.join(venvdir, 'bin', 'python') |
Allen Li | c559824 | 2016-12-21 11:09:23 -0800 | [diff] [blame] | 56 | os.execve( |
Allen Li | 68f42e2 | 2017-03-27 17:08:59 -0700 | [diff] [blame] | 57 | venv_python, |
| 58 | [venv_python] + list(args), |
Allen Li | c559824 | 2016-12-21 11:09:23 -0800 | [diff] [blame] | 59 | _CreateVenvEnvironment(os.environ)) |
Allen Li | 465a0d6 | 2016-11-30 14:55:08 -0800 | [diff] [blame] | 60 | |
| 61 | |
Allen Li | 9f03f5b | 2016-12-13 15:23:52 -0800 | [diff] [blame] | 62 | def _CreateVenvEnvironment(env_dict): |
| 63 | """Create environment for a virtualenv. |
| 64 | |
| 65 | This adds a special marker variable to a copy of the input environment dict |
| 66 | and returns the copy. |
| 67 | |
| 68 | Args: |
| 69 | env_dict: Environment variable dict to use as base, which is not modified. |
| 70 | |
| 71 | Returns: |
| 72 | New environment dict for a virtualenv. |
| 73 | """ |
| 74 | new_env_dict = env_dict.copy() |
| 75 | new_env_dict[_VENV_MARKER] = '1' |
Allen Li | 82b4840 | 2017-03-31 18:33:24 -0700 | [diff] [blame] | 76 | new_env_dict.pop('PYTHONPATH', None) |
Allen Li | 9f03f5b | 2016-12-13 15:23:52 -0800 | [diff] [blame] | 77 | return new_env_dict |
| 78 | |
| 79 | |
| 80 | def _IsInsideVenv(env_dict): |
| 81 | """Return whether the environment dict is running inside a virtualenv. |
| 82 | |
| 83 | This checks the environment dict for the special marker added by |
| 84 | _CreateVenvEnvironment(). |
| 85 | |
| 86 | Args: |
| 87 | env_dict: Environment variable dict to check |
| 88 | |
| 89 | Returns: |
| 90 | A true value if inside virtualenv, else a false value. |
| 91 | """ |
| 92 | # Checking sys.prefix or doing any kind of path check is unreliable because |
| 93 | # we check out chromite to weird places. |
| 94 | return env_dict.get(_VENV_MARKER, '') |
Aviv Keshet | 39853bd | 2016-09-22 15:12:05 -0700 | [diff] [blame] | 95 | |
| 96 | |
| 97 | if __name__ == '__main__': |
Allen Li | 465a0d6 | 2016-11-30 14:55:08 -0800 | [diff] [blame] | 98 | main() |