blob: 61b97764be3bdcd60b59bceb2c44c023597ce1de [file] [log] [blame]
Mike Frysinger1ca14432020-02-16 00:18:56 -05001#!/usr/bin/env python3
Mike Frysingere58c0e22017-10-04 15:43:30 -04002# -*- coding: utf-8 -*-
Aviv Keshet39853bd2016-09-22 15:12:05 -07003# 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
9from __future__ import print_function
10
11import os
Allen Li465a0d62016-11-30 14:55:08 -080012import subprocess
Aviv Keshet39853bd2016-09-22 15:12:05 -070013import sys
Allen Li465a0d62016-11-30 14:55:08 -080014
Greg Edelstona4c9b3b2020-01-07 17:51:13 -070015try:
16 import pytest # pylint: disable=import-error
Mike Frysinger1ca14432020-02-16 00:18:56 -050017 wrapper3 = pytest.importorskip('wrapper3', reason='File must be run in venv')
Greg Edelstona4c9b3b2020-01-07 17:51:13 -070018except ImportError:
Mike Frysinger1ca14432020-02-16 00:18:56 -050019 import wrapper3
Aviv Keshet39853bd2016-09-22 15:12:05 -070020
Don Garrett863f90b2017-04-19 17:04:20 -070021_CHROMITE_DIR = os.path.realpath(
22 os.path.join(os.path.abspath(__file__), '..', '..'))
Allen Li9f03f5b2016-12-13 15:23:52 -080023
Allen Li465a0d62016-11-30 14:55:08 -080024# _VIRTUALENV_DIR contains the scripts for working with venvs
Allen Lic5598242016-12-21 11:09:23 -080025_VIRTUALENV_DIR = os.path.join(_CHROMITE_DIR, '..', 'infra_virtualenv')
Mike Frysinger1ca14432020-02-16 00:18:56 -050026_CREATE_VENV_PATH = os.path.join(_VIRTUALENV_DIR, 'bin', 'create_venv3')
Allen Li465a0d62016-11-30 14:55:08 -080027_REQUIREMENTS = os.path.join(_CHROMITE_DIR, 'venv', 'requirements.txt')
28
Allen Li9f03f5b2016-12-13 15:23:52 -080029_VENV_MARKER = 'INSIDE_CHROMITE_VENV'
30
Allen Li465a0d62016-11-30 14:55:08 -080031
32def main():
Allen Li9f03f5b2016-12-13 15:23:52 -080033 if _IsInsideVenv(os.environ):
Mike Frysingera6de8392020-04-15 04:07:41 -040034 # Don't bleed the marker into children processes that might use the wrapper
35 # themselves to run inside of the virtualenv.
36 os.environ.pop(_VENV_MARKER)
Mike Frysinger1ca14432020-02-16 00:18:56 -050037 wrapper3.DoMain()
Allen Li465a0d62016-11-30 14:55:08 -080038 else:
Allen Li68f42e22017-03-27 17:08:59 -070039 venvdir = _CreateVenv()
40 _ExecInVenv(venvdir, sys.argv)
Allen Li465a0d62016-11-30 14:55:08 -080041
42
43def _CreateVenv():
44 """Create or update chromite venv."""
Mike Frysingercbd46ca2020-07-27 07:36:52 -040045 result = subprocess.run(
46 [_CREATE_VENV_PATH, _REQUIREMENTS],
47 check=False, stdout=subprocess.PIPE, encoding='utf-8')
48 if result.returncode:
49 print(f'{os.path.basename(sys.argv[0])}: error: {" ".join(result.args)}: '
50 f'exited {result.returncode}', file=sys.stderr)
51 sys.exit(result.returncode)
52 return result.stdout.strip()
Allen Li465a0d62016-11-30 14:55:08 -080053
54
Allen Li68f42e22017-03-27 17:08:59 -070055def _ExecInVenv(venvdir, args):
Allen Li465a0d62016-11-30 14:55:08 -080056 """Exec command in chromite venv.
57
58 Args:
Allen Li68f42e22017-03-27 17:08:59 -070059 venvdir: virtualenv directory
Allen Li465a0d62016-11-30 14:55:08 -080060 args: Sequence of arguments.
61 """
Allen Li68f42e22017-03-27 17:08:59 -070062 venv_python = os.path.join(venvdir, 'bin', 'python')
Allen Lic5598242016-12-21 11:09:23 -080063 os.execve(
Allen Li68f42e22017-03-27 17:08:59 -070064 venv_python,
65 [venv_python] + list(args),
Allen Lic5598242016-12-21 11:09:23 -080066 _CreateVenvEnvironment(os.environ))
Allen Li465a0d62016-11-30 14:55:08 -080067
68
Allen Li9f03f5b2016-12-13 15:23:52 -080069def _CreateVenvEnvironment(env_dict):
70 """Create environment for a virtualenv.
71
72 This adds a special marker variable to a copy of the input environment dict
73 and returns the copy.
74
75 Args:
76 env_dict: Environment variable dict to use as base, which is not modified.
77
78 Returns:
79 New environment dict for a virtualenv.
80 """
81 new_env_dict = env_dict.copy()
82 new_env_dict[_VENV_MARKER] = '1'
Allen Li82b48402017-03-31 18:33:24 -070083 new_env_dict.pop('PYTHONPATH', None)
Allen Li9f03f5b2016-12-13 15:23:52 -080084 return new_env_dict
85
86
87def _IsInsideVenv(env_dict):
88 """Return whether the environment dict is running inside a virtualenv.
89
90 This checks the environment dict for the special marker added by
91 _CreateVenvEnvironment().
92
93 Args:
94 env_dict: Environment variable dict to check
95
96 Returns:
97 A true value if inside virtualenv, else a false value.
98 """
99 # Checking sys.prefix or doing any kind of path check is unreliable because
100 # we check out chromite to weird places.
101 return env_dict.get(_VENV_MARKER, '')
Aviv Keshet39853bd2016-09-22 15:12:05 -0700102
103
104if __name__ == '__main__':
Allen Li465a0d62016-11-30 14:55:08 -0800105 main()