示例#1
0
 private void EnsureActivated()
 {
     if (_activatedEnvironmentVariables == null)
     {
         var prefixPath = _factory.Configuration.GetPrefixPath();
         if (_condaLocatorProvider != null && Directory.Exists(prefixPath) && CondaUtils.IsCondaEnvironment(prefixPath))
         {
             var rootConda = _condaLocatorProvider.FindLocator()?.CondaExecutablePath;
             if (File.Exists(rootConda))
             {
                 var env = CondaUtils.CaptureActivationEnvironmentVariablesForPrefix(rootConda, prefixPath);
                 _activatedEnvironmentVariables = env.Union(UnbufferedEnv).ToArray();
             }
             else
             {
                 // Normally, the root is required for this environment to have been discovered,
                 // but it could be that the user added this as a custom environment and then
                 // uninstalled the root. When that's the case, there is no way to activate,
                 // so proceed without activation env variables.
                 _activatedEnvironmentVariables = UnbufferedEnv;
             }
         }
         else
         {
             // Not a conda environment, no activation necessary.
             _activatedEnvironmentVariables = UnbufferedEnv;
         }
     }
 }
        public static Dictionary <string, string> GetFullEnvironment(LaunchConfiguration config, IServiceProvider serviceProvider)
        {
            if (config.Interpreter == null)
            {
                throw new ArgumentNullException(nameof(Interpreter));
            }
            if (serviceProvider == null)
            {
                throw new ArgumentNullException(nameof(serviceProvider));
            }


            // Start with global environment, add configured environment,
            // then add search paths.
            var baseEnv = System.Environment.GetEnvironmentVariables();
            // Clear search paths from the global environment. The launch
            // configuration should include the existing value

            var pathVar = config.Interpreter?.PathEnvironmentVariable;

            if (string.IsNullOrEmpty(pathVar))
            {
                pathVar = "PYTHONPATH";
            }
            baseEnv[pathVar] = string.Empty;

            // TODO: We could introduce a cache so that we don't activate the
            // environment + capture the env variables every time. Not doing this
            // right now to minimize risk/complexity so close to release.
            if (CondaUtils.IsCondaEnvironment(config.Interpreter.GetPrefixPath()))
            {
                var condaExe   = CondaUtils.GetRootCondaExecutablePath(serviceProvider);
                var prefixPath = config.Interpreter.GetPrefixPath();
                if (File.Exists(condaExe) && Directory.Exists(prefixPath))
                {
                    var condaEnv = CondaUtils.CaptureActivationEnvironmentVariablesForPrefix(condaExe, prefixPath);
                    baseEnv = PathUtils.MergeEnvironments(baseEnv.AsEnumerable <string, string>(), condaEnv, "Path", pathVar);
                }
            }

            var env = PathUtils.MergeEnvironments(
                baseEnv.AsEnumerable <string, string>(),
                config.GetEnvironmentVariables(),
                "Path", pathVar
                );

            if (config.SearchPaths != null && config.SearchPaths.Any())
            {
                env = PathUtils.MergeEnvironments(
                    env,
                    new[] {
                    new KeyValuePair <string, string>(
                        pathVar,
                        PathUtils.JoinPathList(config.SearchPaths)
                        )
                },
                    pathVar
                    );
            }
            return(env);
        }