Aviv Keshet | 39853bd | 2016-09-22 15:12:05 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python2 |
| 2 | # 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 | |
| 8 | from __future__ import print_function |
| 9 | |
| 10 | import os |
Allen Li | 465a0d6 | 2016-11-30 14:55:08 -0800 | [diff] [blame] | 11 | import subprocess |
Aviv Keshet | 39853bd | 2016-09-22 15:12:05 -0700 | [diff] [blame] | 12 | import sys |
Allen Li | 465a0d6 | 2016-11-30 14:55:08 -0800 | [diff] [blame] | 13 | |
Aviv Keshet | 39853bd | 2016-09-22 15:12:05 -0700 | [diff] [blame] | 14 | import wrapper |
| 15 | |
Don Garrett | 863f90b | 2017-04-19 17:04:20 -0700 | [diff] [blame^] | 16 | _CHROMITE_DIR = os.path.realpath( |
| 17 | os.path.join(os.path.abspath(__file__), '..', '..')) |
Allen Li | 9f03f5b | 2016-12-13 15:23:52 -0800 | [diff] [blame] | 18 | |
Allen Li | 465a0d6 | 2016-11-30 14:55:08 -0800 | [diff] [blame] | 19 | # _VIRTUALENV_DIR contains the scripts for working with venvs |
Allen Li | c559824 | 2016-12-21 11:09:23 -0800 | [diff] [blame] | 20 | _VIRTUALENV_DIR = os.path.join(_CHROMITE_DIR, '..', 'infra_virtualenv') |
Allen Li | 68f42e2 | 2017-03-27 17:08:59 -0700 | [diff] [blame] | 21 | _CREATE_VENV_PATH = os.path.join(_VIRTUALENV_DIR, 'bin', 'create_venv') |
Allen Li | 465a0d6 | 2016-11-30 14:55:08 -0800 | [diff] [blame] | 22 | _REQUIREMENTS = os.path.join(_CHROMITE_DIR, 'venv', 'requirements.txt') |
| 23 | |
Allen Li | 9f03f5b | 2016-12-13 15:23:52 -0800 | [diff] [blame] | 24 | _VENV_MARKER = 'INSIDE_CHROMITE_VENV' |
| 25 | |
Allen Li | 465a0d6 | 2016-11-30 14:55:08 -0800 | [diff] [blame] | 26 | |
| 27 | def main(): |
Allen Li | 9f03f5b | 2016-12-13 15:23:52 -0800 | [diff] [blame] | 28 | if _IsInsideVenv(os.environ): |
Allen Li | 465a0d6 | 2016-11-30 14:55:08 -0800 | [diff] [blame] | 29 | wrapper.DoMain() |
| 30 | else: |
Allen Li | 68f42e2 | 2017-03-27 17:08:59 -0700 | [diff] [blame] | 31 | venvdir = _CreateVenv() |
| 32 | _ExecInVenv(venvdir, sys.argv) |
Allen Li | 465a0d6 | 2016-11-30 14:55:08 -0800 | [diff] [blame] | 33 | |
| 34 | |
| 35 | def _CreateVenv(): |
| 36 | """Create or update chromite venv.""" |
Allen Li | 68f42e2 | 2017-03-27 17:08:59 -0700 | [diff] [blame] | 37 | return subprocess.check_output([ |
Allen Li | 9f03f5b | 2016-12-13 15:23:52 -0800 | [diff] [blame] | 38 | _CREATE_VENV_PATH, |
Allen Li | 465a0d6 | 2016-11-30 14:55:08 -0800 | [diff] [blame] | 39 | _REQUIREMENTS, |
Allen Li | 68f42e2 | 2017-03-27 17:08:59 -0700 | [diff] [blame] | 40 | ]).rstrip() |
Allen Li | 465a0d6 | 2016-11-30 14:55:08 -0800 | [diff] [blame] | 41 | |
| 42 | |
Allen Li | 68f42e2 | 2017-03-27 17:08:59 -0700 | [diff] [blame] | 43 | def _ExecInVenv(venvdir, args): |
Allen Li | 465a0d6 | 2016-11-30 14:55:08 -0800 | [diff] [blame] | 44 | """Exec command in chromite venv. |
| 45 | |
| 46 | Args: |
Allen Li | 68f42e2 | 2017-03-27 17:08:59 -0700 | [diff] [blame] | 47 | venvdir: virtualenv directory |
Allen Li | 465a0d6 | 2016-11-30 14:55:08 -0800 | [diff] [blame] | 48 | args: Sequence of arguments. |
| 49 | """ |
Allen Li | 68f42e2 | 2017-03-27 17:08:59 -0700 | [diff] [blame] | 50 | venv_python = os.path.join(venvdir, 'bin', 'python') |
Allen Li | c559824 | 2016-12-21 11:09:23 -0800 | [diff] [blame] | 51 | os.execve( |
Allen Li | 68f42e2 | 2017-03-27 17:08:59 -0700 | [diff] [blame] | 52 | venv_python, |
| 53 | [venv_python] + list(args), |
Allen Li | c559824 | 2016-12-21 11:09:23 -0800 | [diff] [blame] | 54 | _CreateVenvEnvironment(os.environ)) |
Allen Li | 465a0d6 | 2016-11-30 14:55:08 -0800 | [diff] [blame] | 55 | |
| 56 | |
Allen Li | 9f03f5b | 2016-12-13 15:23:52 -0800 | [diff] [blame] | 57 | def _CreateVenvEnvironment(env_dict): |
| 58 | """Create environment for a virtualenv. |
| 59 | |
| 60 | This adds a special marker variable to a copy of the input environment dict |
| 61 | and returns the copy. |
| 62 | |
| 63 | Args: |
| 64 | env_dict: Environment variable dict to use as base, which is not modified. |
| 65 | |
| 66 | Returns: |
| 67 | New environment dict for a virtualenv. |
| 68 | """ |
| 69 | new_env_dict = env_dict.copy() |
| 70 | new_env_dict[_VENV_MARKER] = '1' |
Allen Li | 82b4840 | 2017-03-31 18:33:24 -0700 | [diff] [blame] | 71 | new_env_dict.pop('PYTHONPATH', None) |
Allen Li | 9f03f5b | 2016-12-13 15:23:52 -0800 | [diff] [blame] | 72 | return new_env_dict |
| 73 | |
| 74 | |
| 75 | def _IsInsideVenv(env_dict): |
| 76 | """Return whether the environment dict is running inside a virtualenv. |
| 77 | |
| 78 | This checks the environment dict for the special marker added by |
| 79 | _CreateVenvEnvironment(). |
| 80 | |
| 81 | Args: |
| 82 | env_dict: Environment variable dict to check |
| 83 | |
| 84 | Returns: |
| 85 | A true value if inside virtualenv, else a false value. |
| 86 | """ |
| 87 | # Checking sys.prefix or doing any kind of path check is unreliable because |
| 88 | # we check out chromite to weird places. |
| 89 | return env_dict.get(_VENV_MARKER, '') |
Aviv Keshet | 39853bd | 2016-09-22 15:12:05 -0700 | [diff] [blame] | 90 | |
| 91 | |
| 92 | if __name__ == '__main__': |
Allen Li | 465a0d6 | 2016-11-30 14:55:08 -0800 | [diff] [blame] | 93 | main() |