blob: 5a8a79ac82bac65cb4a44c5ea099f55de464d770 [file] [log] [blame]
Aviv Keshet39853bd2016-09-22 15:12:05 -07001#!/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
8from __future__ import print_function
9
10import os
Allen Li465a0d62016-11-30 14:55:08 -080011import subprocess
Aviv Keshet39853bd2016-09-22 15:12:05 -070012import sys
Allen Li465a0d62016-11-30 14:55:08 -080013
Aviv Keshet39853bd2016-09-22 15:12:05 -070014import wrapper
15
Don Garrett863f90b2017-04-19 17:04:20 -070016_CHROMITE_DIR = os.path.realpath(
17 os.path.join(os.path.abspath(__file__), '..', '..'))
Allen Li9f03f5b2016-12-13 15:23:52 -080018
Allen Li465a0d62016-11-30 14:55:08 -080019# _VIRTUALENV_DIR contains the scripts for working with venvs
Allen Lic5598242016-12-21 11:09:23 -080020_VIRTUALENV_DIR = os.path.join(_CHROMITE_DIR, '..', 'infra_virtualenv')
Allen Li68f42e22017-03-27 17:08:59 -070021_CREATE_VENV_PATH = os.path.join(_VIRTUALENV_DIR, 'bin', 'create_venv')
Allen Li465a0d62016-11-30 14:55:08 -080022_REQUIREMENTS = os.path.join(_CHROMITE_DIR, 'venv', 'requirements.txt')
23
Allen Li9f03f5b2016-12-13 15:23:52 -080024_VENV_MARKER = 'INSIDE_CHROMITE_VENV'
25
Allen Li465a0d62016-11-30 14:55:08 -080026
27def main():
Allen Li9f03f5b2016-12-13 15:23:52 -080028 if _IsInsideVenv(os.environ):
Allen Li465a0d62016-11-30 14:55:08 -080029 wrapper.DoMain()
30 else:
Allen Li68f42e22017-03-27 17:08:59 -070031 venvdir = _CreateVenv()
32 _ExecInVenv(venvdir, sys.argv)
Allen Li465a0d62016-11-30 14:55:08 -080033
34
35def _CreateVenv():
36 """Create or update chromite venv."""
Allen Li68f42e22017-03-27 17:08:59 -070037 return subprocess.check_output([
Allen Li9f03f5b2016-12-13 15:23:52 -080038 _CREATE_VENV_PATH,
Allen Li465a0d62016-11-30 14:55:08 -080039 _REQUIREMENTS,
Allen Li68f42e22017-03-27 17:08:59 -070040 ]).rstrip()
Allen Li465a0d62016-11-30 14:55:08 -080041
42
Allen Li68f42e22017-03-27 17:08:59 -070043def _ExecInVenv(venvdir, args):
Allen Li465a0d62016-11-30 14:55:08 -080044 """Exec command in chromite venv.
45
46 Args:
Allen Li68f42e22017-03-27 17:08:59 -070047 venvdir: virtualenv directory
Allen Li465a0d62016-11-30 14:55:08 -080048 args: Sequence of arguments.
49 """
Allen Li68f42e22017-03-27 17:08:59 -070050 venv_python = os.path.join(venvdir, 'bin', 'python')
Allen Lic5598242016-12-21 11:09:23 -080051 os.execve(
Allen Li68f42e22017-03-27 17:08:59 -070052 venv_python,
53 [venv_python] + list(args),
Allen Lic5598242016-12-21 11:09:23 -080054 _CreateVenvEnvironment(os.environ))
Allen Li465a0d62016-11-30 14:55:08 -080055
56
Allen Li9f03f5b2016-12-13 15:23:52 -080057def _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 Li82b48402017-03-31 18:33:24 -070071 new_env_dict.pop('PYTHONPATH', None)
Allen Li9f03f5b2016-12-13 15:23:52 -080072 return new_env_dict
73
74
75def _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 Keshet39853bd2016-09-22 15:12:05 -070090
91
92if __name__ == '__main__':
Allen Li465a0d62016-11-30 14:55:08 -080093 main()