blob: 3b710c96b72c29eeb6c3ed4ec11168e20e9e5942 [file] [log] [blame]
Aviv Keshet39853bd2016-09-22 15:12:05 -07001#!/usr/bin/env python2
Mike Frysingere58c0e22017-10-04 15:43:30 -04002# -*- coding: utf-8 -*-
Aviv Keshet39853bd2016-09-22 15:12:05 -07003# Copyright 2016 The Chromium OS Authors. All rights reserved.
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6
7"""Wrapper around chromite executable scripts that use virtualenv."""
8
9from __future__ import print_function
10
11import os
Allen Li465a0d62016-11-30 14:55:08 -080012import subprocess
Aviv Keshet39853bd2016-09-22 15:12:05 -070013import sys
Allen Li465a0d62016-11-30 14:55:08 -080014
Aviv Keshet39853bd2016-09-22 15:12:05 -070015import wrapper
16
Don Garrett863f90b2017-04-19 17:04:20 -070017_CHROMITE_DIR = os.path.realpath(
18 os.path.join(os.path.abspath(__file__), '..', '..'))
Allen Li9f03f5b2016-12-13 15:23:52 -080019
Allen Li465a0d62016-11-30 14:55:08 -080020# _VIRTUALENV_DIR contains the scripts for working with venvs
Allen Lic5598242016-12-21 11:09:23 -080021_VIRTUALENV_DIR = os.path.join(_CHROMITE_DIR, '..', 'infra_virtualenv')
Allen Li68f42e22017-03-27 17:08:59 -070022_CREATE_VENV_PATH = os.path.join(_VIRTUALENV_DIR, 'bin', 'create_venv')
Allen Li465a0d62016-11-30 14:55:08 -080023_REQUIREMENTS = os.path.join(_CHROMITE_DIR, 'venv', 'requirements.txt')
24
Allen Li9f03f5b2016-12-13 15:23:52 -080025_VENV_MARKER = 'INSIDE_CHROMITE_VENV'
26
Allen Li465a0d62016-11-30 14:55:08 -080027
28def main():
Allen Li9f03f5b2016-12-13 15:23:52 -080029 if _IsInsideVenv(os.environ):
Allen Li465a0d62016-11-30 14:55:08 -080030 wrapper.DoMain()
31 else:
Allen Li68f42e22017-03-27 17:08:59 -070032 venvdir = _CreateVenv()
33 _ExecInVenv(venvdir, sys.argv)
Allen Li465a0d62016-11-30 14:55:08 -080034
35
36def _CreateVenv():
37 """Create or update chromite venv."""
Allen Li68f42e22017-03-27 17:08:59 -070038 return subprocess.check_output([
Allen Li9f03f5b2016-12-13 15:23:52 -080039 _CREATE_VENV_PATH,
Allen Li465a0d62016-11-30 14:55:08 -080040 _REQUIREMENTS,
Mike Frysingere3da4c92019-10-13 23:00:42 -040041 ]).rstrip().decode('utf-8')
Allen Li465a0d62016-11-30 14:55:08 -080042
43
Allen Li68f42e22017-03-27 17:08:59 -070044def _ExecInVenv(venvdir, args):
Allen Li465a0d62016-11-30 14:55:08 -080045 """Exec command in chromite venv.
46
47 Args:
Allen Li68f42e22017-03-27 17:08:59 -070048 venvdir: virtualenv directory
Allen Li465a0d62016-11-30 14:55:08 -080049 args: Sequence of arguments.
50 """
Allen Li68f42e22017-03-27 17:08:59 -070051 venv_python = os.path.join(venvdir, 'bin', 'python')
Allen Lic5598242016-12-21 11:09:23 -080052 os.execve(
Allen Li68f42e22017-03-27 17:08:59 -070053 venv_python,
54 [venv_python] + list(args),
Allen Lic5598242016-12-21 11:09:23 -080055 _CreateVenvEnvironment(os.environ))
Allen Li465a0d62016-11-30 14:55:08 -080056
57
Allen Li9f03f5b2016-12-13 15:23:52 -080058def _CreateVenvEnvironment(env_dict):
59 """Create environment for a virtualenv.
60
61 This adds a special marker variable to a copy of the input environment dict
62 and returns the copy.
63
64 Args:
65 env_dict: Environment variable dict to use as base, which is not modified.
66
67 Returns:
68 New environment dict for a virtualenv.
69 """
70 new_env_dict = env_dict.copy()
71 new_env_dict[_VENV_MARKER] = '1'
Allen Li82b48402017-03-31 18:33:24 -070072 new_env_dict.pop('PYTHONPATH', None)
Allen Li9f03f5b2016-12-13 15:23:52 -080073 return new_env_dict
74
75
76def _IsInsideVenv(env_dict):
77 """Return whether the environment dict is running inside a virtualenv.
78
79 This checks the environment dict for the special marker added by
80 _CreateVenvEnvironment().
81
82 Args:
83 env_dict: Environment variable dict to check
84
85 Returns:
86 A true value if inside virtualenv, else a false value.
87 """
88 # Checking sys.prefix or doing any kind of path check is unreliable because
89 # we check out chromite to weird places.
90 return env_dict.get(_VENV_MARKER, '')
Aviv Keshet39853bd2016-09-22 15:12:05 -070091
92
93if __name__ == '__main__':
Allen Li465a0d62016-11-30 14:55:08 -080094 main()