"Fossies" - the Fresh Open Source Software Archive

Member "scons-3.0.5/engine/SCons/Platform/virtualenv.py" (26 Mar 2019, 4003 Bytes) of package /linux/misc/scons-3.0.5.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "virtualenv.py" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 3.0.4_vs_3.0.5.

    1 """SCons.Platform.virtualenv
    2 
    3 Support for virtualenv.
    4 """
    5 
    6 #
    7 # Copyright (c) 2001 - 2019 The SCons Foundation
    8 #
    9 # Permission is hereby granted, free of charge, to any person obtaining
   10 # a copy of this software and associated documentation files (the
   11 # "Software"), to deal in the Software without restriction, including
   12 # without limitation the rights to use, copy, modify, merge, publish,
   13 # distribute, sublicense, and/or sell copies of the Software, and to
   14 # permit persons to whom the Software is furnished to do so, subject to
   15 # the following conditions:
   16 #
   17 # The above copyright notice and this permission notice shall be included
   18 # in all copies or substantial portions of the Software.
   19 #
   20 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
   21 # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
   22 # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
   23 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
   24 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
   25 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
   26 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
   27 #
   28 
   29 __revision__ = "src/engine/SCons/Platform/virtualenv.py a56bbd8c09fb219ab8a9673330ffcd55279219d0 2019-03-26 23:16:31 bdeegan"
   30 
   31 import os
   32 import sys
   33 import SCons.Util
   34 
   35 
   36 virtualenv_enabled_by_default = False
   37 
   38 
   39 def _enable_virtualenv_default():
   40     return SCons.Util.get_os_env_bool('SCONS_ENABLE_VIRTUALENV', virtualenv_enabled_by_default)
   41 
   42 
   43 def _ignore_virtualenv_default():
   44     return SCons.Util.get_os_env_bool('SCONS_IGNORE_VIRTUALENV', False)
   45 
   46 
   47 enable_virtualenv = _enable_virtualenv_default()
   48 ignore_virtualenv = _ignore_virtualenv_default()
   49 virtualenv_variables = ['VIRTUAL_ENV', 'PIPENV_ACTIVE']
   50 
   51 
   52 def _running_in_virtualenv():
   53     """Returns True, if scons is executed within a virtualenv"""
   54     # see https://stackoverflow.com/a/42580137
   55     return (hasattr(sys, 'real_prefix') or
   56             (hasattr(sys, 'base_prefix') and sys.base_prefix != sys.prefix))
   57 
   58 
   59 def _is_path_in(path, base):
   60     """Returns true, if **path** is located under the **base** directory."""
   61     if not path or not base: # empty path may happen, base too
   62         return False
   63     rp = os.path.relpath(path, base)
   64     return ((not rp.startswith(os.path.pardir)) and (not rp == os.path.curdir))
   65 
   66 
   67 def _inject_venv_variables(env):
   68     if 'ENV' not in env:
   69         env['ENV'] = {}
   70     ENV = env['ENV']
   71     for name in virtualenv_variables:
   72         try:
   73             ENV[name] = os.environ[name]
   74         except KeyError:
   75             pass
   76 
   77 def _inject_venv_path(env, path_list=None):
   78     """Modify environment such that SCons will take into account its virtualenv
   79     when running external tools."""
   80     if path_list is None:
   81         path_list = os.getenv('PATH')
   82     env.PrependENVPath('PATH', select_paths_in_venv(path_list))
   83 
   84 
   85 def select_paths_in_venv(path_list):
   86     """Returns a list of paths from **path_list** which are under virtualenv's
   87     home directory."""
   88     if SCons.Util.is_String(path_list):
   89         path_list = path_list.split(os.path.pathsep)
   90     # Find in path_list the paths under the virtualenv's home
   91     return [path for path in path_list if IsInVirtualenv(path)]
   92 
   93 
   94 def ImportVirtualenv(env):
   95     """Copies virtualenv-related environment variables from OS environment
   96     to ``env['ENV']`` and prepends virtualenv's PATH to ``env['ENV']['PATH']``.
   97     """
   98     _inject_venv_variables(env)
   99     _inject_venv_path(env)
  100 
  101 
  102 def Virtualenv():
  103     """Returns path to the virtualenv home if scons is executing within a
  104     virtualenv or None, if not."""
  105     if _running_in_virtualenv():
  106         return sys.prefix
  107     return None
  108 
  109 
  110 def IsInVirtualenv(path):
  111     """Returns True, if **path** is under virtualenv's home directory. If not,
  112     or if we don't use virtualenv, returns False."""
  113     return _is_path_in(path, Virtualenv())
  114 
  115 
  116 # Local Variables:
  117 # tab-width:4
  118 # indent-tabs-mode:nil
  119 # End:
  120 # vim: set expandtab tabstop=4 shiftwidth=4: