blob: 0ac9343069869474e073e6c17eee8239d6093e43 [file] [log] [blame]
Mike Frysinger1ca14432020-02-16 00:18:56 -05001#!/usr/bin/env python3
Aviv Keshet39853bd2016-09-22 15:12:05 -07002# Copyright 2016 The Chromium OS Authors. All rights reserved.
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 Keshet39853bd2016-09-22 15:12:05 -07008import os
Allen Li465a0d62016-11-30 14:55:08 -08009import subprocess
Aviv Keshet39853bd2016-09-22 15:12:05 -070010import sys
Allen Li465a0d62016-11-30 14:55:08 -080011
Greg Edelstona4c9b3b2020-01-07 17:51:13 -070012try:
13 import pytest # pylint: disable=import-error
Mike Frysinger1ca14432020-02-16 00:18:56 -050014 wrapper3 = pytest.importorskip('wrapper3', reason='File must be run in venv')
Greg Edelstona4c9b3b2020-01-07 17:51:13 -070015except ImportError:
Mike Frysinger1ca14432020-02-16 00:18:56 -050016 import wrapper3
Aviv Keshet39853bd2016-09-22 15:12:05 -070017
Don Garrett863f90b2017-04-19 17:04:20 -070018_CHROMITE_DIR = os.path.realpath(
19 os.path.join(os.path.abspath(__file__), '..', '..'))
Allen Li9f03f5b2016-12-13 15:23:52 -080020
Allen Li465a0d62016-11-30 14:55:08 -080021# _VIRTUALENV_DIR contains the scripts for working with venvs
Allen Lic5598242016-12-21 11:09:23 -080022_VIRTUALENV_DIR = os.path.join(_CHROMITE_DIR, '..', 'infra_virtualenv')
Mike Frysinger1ca14432020-02-16 00:18:56 -050023_CREATE_VENV_PATH = os.path.join(_VIRTUALENV_DIR, 'bin', 'create_venv3')
Allen Li465a0d62016-11-30 14:55:08 -080024_REQUIREMENTS = os.path.join(_CHROMITE_DIR, 'venv', 'requirements.txt')
25
Allen Li9f03f5b2016-12-13 15:23:52 -080026_VENV_MARKER = 'INSIDE_CHROMITE_VENV'
27
Allen Li465a0d62016-11-30 14:55:08 -080028
29def main():
Allen Li9f03f5b2016-12-13 15:23:52 -080030 if _IsInsideVenv(os.environ):
Mike Frysingera6de8392020-04-15 04:07:41 -040031 # Don't bleed the marker into children processes that might use the wrapper
32 # themselves to run inside of the virtualenv.
33 os.environ.pop(_VENV_MARKER)
Mike Frysinger1ca14432020-02-16 00:18:56 -050034 wrapper3.DoMain()
Allen Li465a0d62016-11-30 14:55:08 -080035 else:
Allen Li68f42e22017-03-27 17:08:59 -070036 venvdir = _CreateVenv()
37 _ExecInVenv(venvdir, sys.argv)
Allen Li465a0d62016-11-30 14:55:08 -080038
39
40def _CreateVenv():
41 """Create or update chromite venv."""
Mike Frysingercbd46ca2020-07-27 07:36:52 -040042 result = subprocess.run(
43 [_CREATE_VENV_PATH, _REQUIREMENTS],
44 check=False, stdout=subprocess.PIPE, encoding='utf-8')
45 if result.returncode:
46 print(f'{os.path.basename(sys.argv[0])}: error: {" ".join(result.args)}: '
47 f'exited {result.returncode}', file=sys.stderr)
48 sys.exit(result.returncode)
49 return result.stdout.strip()
Allen Li465a0d62016-11-30 14:55:08 -080050
51
Allen Li68f42e22017-03-27 17:08:59 -070052def _ExecInVenv(venvdir, args):
Allen Li465a0d62016-11-30 14:55:08 -080053 """Exec command in chromite venv.
54
55 Args:
Allen Li68f42e22017-03-27 17:08:59 -070056 venvdir: virtualenv directory
Allen Li465a0d62016-11-30 14:55:08 -080057 args: Sequence of arguments.
58 """
Allen Li68f42e22017-03-27 17:08:59 -070059 venv_python = os.path.join(venvdir, 'bin', 'python')
Allen Lic5598242016-12-21 11:09:23 -080060 os.execve(
Allen Li68f42e22017-03-27 17:08:59 -070061 venv_python,
62 [venv_python] + list(args),
Allen Lic5598242016-12-21 11:09:23 -080063 _CreateVenvEnvironment(os.environ))
Allen Li465a0d62016-11-30 14:55:08 -080064
65
Allen Li9f03f5b2016-12-13 15:23:52 -080066def _CreateVenvEnvironment(env_dict):
67 """Create environment for a virtualenv.
68
69 This adds a special marker variable to a copy of the input environment dict
70 and returns the copy.
71
72 Args:
73 env_dict: Environment variable dict to use as base, which is not modified.
74
75 Returns:
76 New environment dict for a virtualenv.
77 """
78 new_env_dict = env_dict.copy()
79 new_env_dict[_VENV_MARKER] = '1'
Allen Li82b48402017-03-31 18:33:24 -070080 new_env_dict.pop('PYTHONPATH', None)
Allen Li9f03f5b2016-12-13 15:23:52 -080081 return new_env_dict
82
83
84def _IsInsideVenv(env_dict):
85 """Return whether the environment dict is running inside a virtualenv.
86
87 This checks the environment dict for the special marker added by
88 _CreateVenvEnvironment().
89
90 Args:
91 env_dict: Environment variable dict to check
92
93 Returns:
94 A true value if inside virtualenv, else a false value.
95 """
96 # Checking sys.prefix or doing any kind of path check is unreliable because
97 # we check out chromite to weird places.
98 return env_dict.get(_VENV_MARKER, '')
Aviv Keshet39853bd2016-09-22 15:12:05 -070099
100
101if __name__ == '__main__':
Allen Li465a0d62016-11-30 14:55:08 -0800102 main()