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 | |
Allen Li | 9f03f5b | 2016-12-13 15:23:52 -0800 | [diff] [blame^] | 16 | |
Allen Li | 465a0d6 | 2016-11-30 14:55:08 -0800 | [diff] [blame] | 17 | def _FindChromiteDir(): |
| 18 | path = os.path.dirname(os.path.realpath(__file__)) |
| 19 | while not os.path.exists(os.path.join(path, 'PRESUBMIT.cfg')): |
| 20 | path = os.path.dirname(path) |
| 21 | return path |
Aviv Keshet | 39853bd | 2016-09-22 15:12:05 -0700 | [diff] [blame] | 22 | |
Aviv Keshet | 39853bd | 2016-09-22 15:12:05 -0700 | [diff] [blame] | 23 | |
Allen Li | 465a0d6 | 2016-11-30 14:55:08 -0800 | [diff] [blame] | 24 | _CHROMITE_DIR = _FindChromiteDir() |
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 |
| 27 | _VIRTUALENV_DIR = os.path.join(_CHROMITE_DIR, '../infra_virtualenv') |
Allen Li | 9f03f5b | 2016-12-13 15:23:52 -0800 | [diff] [blame^] | 28 | _CREATE_VENV_PATH = os.path.join(_VIRTUALENV_DIR, 'create_venv') |
| 29 | _VENV_COMMAND_PATH = os.path.join(_VIRTUALENV_DIR, 'venv_command') |
| 30 | |
| 31 | # _VENV_DIR is the virtualenv dir that contains bin/activate. |
Allen Li | 465a0d6 | 2016-11-30 14:55:08 -0800 | [diff] [blame] | 32 | _VENV_DIR = os.path.join(_CHROMITE_DIR, '.venv') |
| 33 | _REQUIREMENTS = os.path.join(_CHROMITE_DIR, 'venv', 'requirements.txt') |
| 34 | |
Allen Li | 9f03f5b | 2016-12-13 15:23:52 -0800 | [diff] [blame^] | 35 | _VENV_MARKER = 'INSIDE_CHROMITE_VENV' |
| 36 | |
Allen Li | 465a0d6 | 2016-11-30 14:55:08 -0800 | [diff] [blame] | 37 | |
| 38 | def main(): |
Allen Li | 9f03f5b | 2016-12-13 15:23:52 -0800 | [diff] [blame^] | 39 | if _IsInsideVenv(os.environ): |
Allen Li | 465a0d6 | 2016-11-30 14:55:08 -0800 | [diff] [blame] | 40 | wrapper.DoMain() |
| 41 | else: |
| 42 | _CreateVenv() |
| 43 | _ExecInVenv(sys.argv) |
| 44 | |
| 45 | |
| 46 | def _CreateVenv(): |
| 47 | """Create or update chromite venv.""" |
| 48 | subprocess.check_call([ |
Allen Li | 9f03f5b | 2016-12-13 15:23:52 -0800 | [diff] [blame^] | 49 | _CREATE_VENV_PATH, |
Allen Li | 465a0d6 | 2016-11-30 14:55:08 -0800 | [diff] [blame] | 50 | _VENV_DIR, |
| 51 | _REQUIREMENTS, |
| 52 | ], stdout=sys.stderr) |
| 53 | |
| 54 | |
| 55 | def _ExecInVenv(args): |
| 56 | """Exec command in chromite venv. |
| 57 | |
| 58 | Args: |
| 59 | args: Sequence of arguments. |
| 60 | """ |
Allen Li | 9f03f5b | 2016-12-13 15:23:52 -0800 | [diff] [blame^] | 61 | os.execve(_VENV_COMMAND_PATH, |
| 62 | [os.path.basename(_VENV_COMMAND_PATH), _VENV_DIR] + list(args), |
| 63 | _CreateVenvEnvironment(os.environ)) |
Allen Li | 465a0d6 | 2016-11-30 14:55:08 -0800 | [diff] [blame] | 64 | |
| 65 | |
Allen Li | 9f03f5b | 2016-12-13 15:23:52 -0800 | [diff] [blame^] | 66 | def _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' |
| 80 | return new_env_dict |
| 81 | |
| 82 | |
| 83 | def _IsInsideVenv(env_dict): |
| 84 | """Return whether the environment dict is running inside a virtualenv. |
| 85 | |
| 86 | This checks the environment dict for the special marker added by |
| 87 | _CreateVenvEnvironment(). |
| 88 | |
| 89 | Args: |
| 90 | env_dict: Environment variable dict to check |
| 91 | |
| 92 | Returns: |
| 93 | A true value if inside virtualenv, else a false value. |
| 94 | """ |
| 95 | # Checking sys.prefix or doing any kind of path check is unreliable because |
| 96 | # we check out chromite to weird places. |
| 97 | return env_dict.get(_VENV_MARKER, '') |
Aviv Keshet | 39853bd | 2016-09-22 15:12:05 -0700 | [diff] [blame] | 98 | |
| 99 | |
| 100 | if __name__ == '__main__': |
Allen Li | 465a0d6 | 2016-11-30 14:55:08 -0800 | [diff] [blame] | 101 | main() |