blob: 67c7b17e07228526546837c49358a9854e54ba2a [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
Allen Li9f03f5b2016-12-13 15:23:52 -080016
Allen Li465a0d62016-11-30 14:55:08 -080017def _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 Keshet39853bd2016-09-22 15:12:05 -070022
Aviv Keshet39853bd2016-09-22 15:12:05 -070023
Allen Li465a0d62016-11-30 14:55:08 -080024_CHROMITE_DIR = _FindChromiteDir()
Allen Li9f03f5b2016-12-13 15:23:52 -080025
Allen Li465a0d62016-11-30 14:55:08 -080026# _VIRTUALENV_DIR contains the scripts for working with venvs
Allen Lic5598242016-12-21 11:09:23 -080027_VIRTUALENV_DIR = os.path.join(_CHROMITE_DIR, '..', 'infra_virtualenv')
Allen Li9f03f5b2016-12-13 15:23:52 -080028_CREATE_VENV_PATH = os.path.join(_VIRTUALENV_DIR, 'create_venv')
Allen Li9f03f5b2016-12-13 15:23:52 -080029
30# _VENV_DIR is the virtualenv dir that contains bin/activate.
Allen Li8f0e8452016-12-16 15:33:12 -080031_VENV_DIR = os.path.join(_CHROMITE_DIR, 'venv', '.venv')
Allen Lic5598242016-12-21 11:09:23 -080032_VENV_PYTHON = os.path.join(_VENV_DIR, 'bin', 'python')
Allen Li465a0d62016-11-30 14:55:08 -080033_REQUIREMENTS = os.path.join(_CHROMITE_DIR, 'venv', 'requirements.txt')
34
Allen Li9f03f5b2016-12-13 15:23:52 -080035_VENV_MARKER = 'INSIDE_CHROMITE_VENV'
36
Allen Li465a0d62016-11-30 14:55:08 -080037
38def main():
Allen Li9f03f5b2016-12-13 15:23:52 -080039 if _IsInsideVenv(os.environ):
Allen Li465a0d62016-11-30 14:55:08 -080040 wrapper.DoMain()
41 else:
42 _CreateVenv()
43 _ExecInVenv(sys.argv)
44
45
46def _CreateVenv():
47 """Create or update chromite venv."""
48 subprocess.check_call([
Allen Li9f03f5b2016-12-13 15:23:52 -080049 _CREATE_VENV_PATH,
Allen Li465a0d62016-11-30 14:55:08 -080050 _VENV_DIR,
51 _REQUIREMENTS,
52 ], stdout=sys.stderr)
53
54
55def _ExecInVenv(args):
56 """Exec command in chromite venv.
57
58 Args:
59 args: Sequence of arguments.
60 """
Allen Lic5598242016-12-21 11:09:23 -080061 os.execve(
62 _VENV_PYTHON,
63 [_VENV_PYTHON] + list(args),
64 _CreateVenvEnvironment(os.environ))
Allen Li465a0d62016-11-30 14:55:08 -080065
66
Allen Li9f03f5b2016-12-13 15:23:52 -080067def _CreateVenvEnvironment(env_dict):
68 """Create environment for a virtualenv.
69
70 This adds a special marker variable to a copy of the input environment dict
71 and returns the copy.
72
73 Args:
74 env_dict: Environment variable dict to use as base, which is not modified.
75
76 Returns:
77 New environment dict for a virtualenv.
78 """
79 new_env_dict = env_dict.copy()
80 new_env_dict[_VENV_MARKER] = '1'
81 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()