blob: 95710b9f95bc259841ac2d0821461d5a04738a8e [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 Li68f42e22017-03-27 17:08:59 -070028_CREATE_VENV_PATH = os.path.join(_VIRTUALENV_DIR, 'bin', 'create_venv')
Allen Li465a0d62016-11-30 14:55:08 -080029_REQUIREMENTS = os.path.join(_CHROMITE_DIR, 'venv', 'requirements.txt')
30
Allen Li9f03f5b2016-12-13 15:23:52 -080031_VENV_MARKER = 'INSIDE_CHROMITE_VENV'
32
Allen Li465a0d62016-11-30 14:55:08 -080033
34def main():
Allen Li9f03f5b2016-12-13 15:23:52 -080035 if _IsInsideVenv(os.environ):
Allen Li465a0d62016-11-30 14:55:08 -080036 wrapper.DoMain()
37 else:
Allen Li68f42e22017-03-27 17:08:59 -070038 venvdir = _CreateVenv()
39 _ExecInVenv(venvdir, sys.argv)
Allen Li465a0d62016-11-30 14:55:08 -080040
41
42def _CreateVenv():
43 """Create or update chromite venv."""
Allen Li68f42e22017-03-27 17:08:59 -070044 return subprocess.check_output([
Allen Li9f03f5b2016-12-13 15:23:52 -080045 _CREATE_VENV_PATH,
Allen Li465a0d62016-11-30 14:55:08 -080046 _REQUIREMENTS,
Allen Li68f42e22017-03-27 17:08:59 -070047 ]).rstrip()
Allen Li465a0d62016-11-30 14:55:08 -080048
49
Allen Li68f42e22017-03-27 17:08:59 -070050def _ExecInVenv(venvdir, args):
Allen Li465a0d62016-11-30 14:55:08 -080051 """Exec command in chromite venv.
52
53 Args:
Allen Li68f42e22017-03-27 17:08:59 -070054 venvdir: virtualenv directory
Allen Li465a0d62016-11-30 14:55:08 -080055 args: Sequence of arguments.
56 """
Allen Li68f42e22017-03-27 17:08:59 -070057 venv_python = os.path.join(venvdir, 'bin', 'python')
Allen Lic5598242016-12-21 11:09:23 -080058 os.execve(
Allen Li68f42e22017-03-27 17:08:59 -070059 venv_python,
60 [venv_python] + list(args),
Allen Lic5598242016-12-21 11:09:23 -080061 _CreateVenvEnvironment(os.environ))
Allen Li465a0d62016-11-30 14:55:08 -080062
63
Allen Li9f03f5b2016-12-13 15:23:52 -080064def _CreateVenvEnvironment(env_dict):
65 """Create environment for a virtualenv.
66
67 This adds a special marker variable to a copy of the input environment dict
68 and returns the copy.
69
70 Args:
71 env_dict: Environment variable dict to use as base, which is not modified.
72
73 Returns:
74 New environment dict for a virtualenv.
75 """
76 new_env_dict = env_dict.copy()
77 new_env_dict[_VENV_MARKER] = '1'
Allen Li82b48402017-03-31 18:33:24 -070078 new_env_dict.pop('PYTHONPATH', None)
Allen Li9f03f5b2016-12-13 15:23:52 -080079 return new_env_dict
80
81
82def _IsInsideVenv(env_dict):
83 """Return whether the environment dict is running inside a virtualenv.
84
85 This checks the environment dict for the special marker added by
86 _CreateVenvEnvironment().
87
88 Args:
89 env_dict: Environment variable dict to check
90
91 Returns:
92 A true value if inside virtualenv, else a false value.
93 """
94 # Checking sys.prefix or doing any kind of path check is unreliable because
95 # we check out chromite to weird places.
96 return env_dict.get(_VENV_MARKER, '')
Aviv Keshet39853bd2016-09-22 15:12:05 -070097
98
99if __name__ == '__main__':
Allen Li465a0d62016-11-30 14:55:08 -0800100 main()