Beispiel #1
0
        /// <summary>
        /// Creates a virtual environment using virtualenv. If virtualenv or pip
        /// are not installed then they are downloaded and installed automatically.
        /// </summary>
        public static async Task CreateWithVirtualEnv(
            IServiceProvider provider,
            IPythonInterpreterFactory factory,
            string path
            )
        {
            if (provider == null)
            {
                throw new ArgumentNullException(nameof(provider));
            }

            if (factory == null)
            {
                throw new ArgumentNullException(nameof(factory));
            }

            if (string.IsNullOrEmpty(path))
            {
                throw new ArgumentNullException(nameof(path));
            }

            factory.ThrowIfNotRunnable(nameof(factory));

            var cancel          = CancellationToken.None;
            var ui              = new VsPackageManagerUI(provider);
            var interpreterOpts = provider.GetComponentModel().GetService <IInterpreterOptionsService>();
            var pm              = interpreterOpts?.GetPackageManagers(factory).FirstOrDefault(p => p.UniqueKey == "pip");

            if (pm == null)
            {
                throw new InvalidOperationException(Strings.PackageManagementNotSupported);
            }
            if (!pm.IsReady)
            {
                await pm.PrepareAsync(ui, cancel);

                if (!pm.IsReady)
                {
                    throw new InvalidOperationException(Strings.VirtualEnvCreationFailed.FormatUI(path));
                }
            }

            bool hasVirtualEnv = (await factory.HasModuleAsync("venv", interpreterOpts)) ||
                                 (await factory.HasModuleAsync("virtualenv", interpreterOpts));

            if (!hasVirtualEnv)
            {
                if (!await Install(provider, factory))
                {
                    throw new InvalidOperationException(Strings.VirtualEnvCreationFailed.FormatUI(path));
                }
            }

            await ContinueCreate(provider, factory, path, false, PackageManagerUIRedirector.Get(pm, ui));
        }