blob: 15def65e7643bbb3daa178572892fedf33b19082 [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
27_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')
29_VENV_COMMAND_PATH = os.path.join(_VIRTUALENV_DIR, 'venv_command')
30
31# _VENV_DIR is the virtualenv dir that contains bin/activate.
Allen Li465a0d62016-11-30 14:55:08 -080032_VENV_DIR = os.path.join(_CHROMITE_DIR, '.venv')
33_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 Li9f03f5b2016-12-13 15:23:52 -080061 os.execve(_VENV_COMMAND_PATH,
62 [os.path.basename(_VENV_COMMAND_PATH), _VENV_DIR] + list(args),
63 _CreateVenvEnvironment(os.environ))
Allen Li465a0d62016-11-30 14:55:08 -080064
65
Allen Li9f03f5b2016-12-13 15:23:52 -080066def _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
83def _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 Keshet39853bd2016-09-22 15:12:05 -070098
99
100if __name__ == '__main__':
Allen Li465a0d62016-11-30 14:55:08 -0800101 main()