Ejemplo n.º 1
0
        public async Task <bool> InstallAsync(PackageSpec package, IPackageManagerUI ui, CancellationToken cancellationToken)
        {
            AbortOnInvalidConfiguration();
            await AbortIfNotReady(cancellationToken);

            bool success = false;
            var  args    = new List <string>();

            args.Add("install");
            args.Add("-p");
            args.Add(ProcessOutput.QuoteSingleArgument(_factory.Configuration.PrefixPath));
            args.Add("-y");

            args.Add(package.FullSpec);
            var name      = string.IsNullOrEmpty(package.Name) ? package.FullSpec : package.Name;
            var operation = string.Join(" ", args);

            using (await _working.LockAsync(cancellationToken)) {
                ui?.OnOperationStarted(this, operation);
                ui?.OnOutputTextReceived(this, Strings.InstallingPackageStarted.FormatUI(name));

                try {
                    using (var output = ProcessOutput.Run(
                               _condaPath,
                               args,
                               _factory.Configuration.PrefixPath,
                               UnbufferedEnv,
                               false,
                               PackageManagerUIRedirector.Get(this, ui),
                               quoteArgs: false,
                               elevate: await ShouldElevate(ui, operation)
                               )) {
                        if (!output.IsStarted)
                        {
                            return(false);
                        }
                        var exitCode = await output;
                        success = exitCode == 0;
                    }
                    return(success);
                } catch (IOException) {
                    return(false);
                } finally {
                    if (!success)
                    {
                        // Check whether we failed because conda is missing
                        UpdateIsReadyAsync(true, CancellationToken.None).DoNotWait();
                    }

                    var msg = success ? Strings.InstallingPackageSuccess : Strings.InstallingPackageFailed;
                    ui?.OnOutputTextReceived(this, msg.FormatUI(name));
                    ui?.OnOperationFinished(this, operation, success);
                    await CacheInstalledPackagesAsync(true, false, cancellationToken);
                }
            }
        }
Ejemplo n.º 2
0
        public async Task <bool> ExecuteAsync(string arguments, IPackageManagerUI ui, CancellationToken cancellationToken)
        {
            AbortOnInvalidConfiguration();
            await AbortIfNotReady(cancellationToken);

            using (await _working.LockAsync(cancellationToken)) {
                bool success = false;

                var args      = string.Join(" ", _commands.Base().Select(ProcessOutput.QuoteSingleArgument)) + " " + arguments;
                var operation = args.Trim();
                ui?.OnOutputTextReceived(this, operation);
                ui?.OnOperationStarted(this, Strings.ExecutingCommandStarted.FormatUI(arguments));

                var envVars = await GetEnvironmentVariables();

                try {
                    using (var output = ProcessOutput.Run(
                               _factory.Configuration.InterpreterPath,
                               new[] { args.Trim() },
                               _factory.Configuration.GetPrefixPath(),
                               envVars,
                               false,
                               PackageManagerUIRedirector.Get(this, ui),
                               quoteArgs: false,
                               elevate: await ShouldElevate(ui, operation)
                               )) {
                        if (!output.IsStarted)
                        {
                            return(false);
                        }
                        var exitCode = await output;
                        success = exitCode == 0;
                    }
                    return(success);
                } catch (IOException) {
                    return(false);
                } finally {
                    if (!success)
                    {
                        // Check whether we failed because pip is missing
                        UpdateIsReadyAsync(true, CancellationToken.None).DoNotWait();
                    }

                    var msg = success ? Strings.ExecutingCommandSucceeded : Strings.ExecutingCommandFailed;
                    ui?.OnOutputTextReceived(this, msg.FormatUI(arguments));
                    ui?.OnOperationFinished(this, operation, success);
                    await CacheInstalledPackagesAsync(true, false, cancellationToken);
                }
            }
        }
Ejemplo n.º 3
0
        public async Task PrepareAsync(IPackageManagerUI ui, CancellationToken cancellationToken)
        {
            if (IsReady)
            {
                return;
            }

            AbortOnInvalidConfiguration();

            await UpdateIsReadyAsync(false, cancellationToken);

            if (IsReady)
            {
                return;
            }

            var operation = "pip_downloader.py";
            var args      = _extraInterpreterArgs
                            .Concat(new[] { PythonToolsInstallPath.GetFile("pip_downloader.py", GetType().Assembly) });

            using (await _working.LockAsync(cancellationToken)) {
                ui?.OnOperationStarted(this, operation);
                ui?.OnOutputTextReceived(this, Strings.InstallingPipStarted);

                using (var proc = ProcessOutput.Run(
                           _factory.Configuration.InterpreterPath,
                           args,
                           _factory.Configuration.PrefixPath,
                           UnbufferedEnv,
                           false,
                           PackageManagerUIRedirector.Get(this, ui),
                           elevate: await ShouldElevate(ui, operation)
                           )) {
                    try {
                        IsReady = (await proc == 0);
                    } catch (OperationCanceledException) {
                        IsReady = false;
                    }
                }

                ui?.OnOutputTextReceived(this, IsReady ? Strings.InstallingPipSuccess : Strings.InstallingPackageFailed);
                ui?.OnOperationFinished(this, operation, IsReady);
            }
        }
Ejemplo n.º 4
0
        public async Task PrepareAsync(IPackageManagerUI ui, CancellationToken cancellationToken)
        {
            if (IsReady)
            {
                return;
            }

            AbortOnInvalidConfiguration();

            await UpdateIsReadyAsync(false, cancellationToken);

            if (IsReady)
            {
                return;
            }

            var operation = "pip_downloader.py";

            using (await _working.LockAsync(cancellationToken)) {
                ui?.OnOperationStarted(this, operation);
                ui?.OnOutputTextReceived(this, Strings.InstallingPipStarted);

                var envVars = await GetEnvironmentVariables();

                using (var proc = ProcessOutput.Run(
                           _factory.Configuration.InterpreterPath,
                           _commands.Prepare(),
                           _factory.Configuration.GetPrefixPath(),
                           envVars,
                           false,
                           PackageManagerUIRedirector.Get(this, ui),
                           elevate: await ShouldElevate(ui, operation)
                           )) {
                    try {
                        IsReady = (await proc == 0);
                    } catch (OperationCanceledException) {
                        IsReady = false;
                    }
                }

                ui?.OnOutputTextReceived(this, IsReady ? Strings.InstallingPipSuccess : Strings.InstallingPackageFailed);
                ui?.OnOperationFinished(this, operation, IsReady);
            }
        }
Ejemplo n.º 5
0
        public async Task <bool> UninstallAsync(PackageSpec package, IPackageManagerUI ui, CancellationToken cancellationToken)
        {
            AbortOnInvalidConfiguration();
            await AbortIfNotReady(cancellationToken);

            bool success = false;
            var  args    = _extraInterpreterArgs.ToList();

            if (!SupportsDashMPip)
            {
                args.Add("-c");
                args.Add("\"import pip; pip.main()\"");
            }
            else
            {
                args.Add("-m");
                args.Add("pip");
            }
            args.Add("uninstall");
            args.Add("-y");

            args.Add(package.FullSpec);
            var name      = string.IsNullOrEmpty(package.Name) ? package.FullSpec : package.Name;
            var operation = string.Join(" ", args);

            try {
                using (await _working.LockAsync(cancellationToken)) {
                    ui?.OnOperationStarted(this, operation);
                    ui?.OnOutputTextReceived(this, Strings.UninstallingPackageStarted.FormatUI(name));

                    using (var output = ProcessOutput.Run(
                               _factory.Configuration.InterpreterPath,
                               args,
                               _factory.Configuration.PrefixPath,
                               UnbufferedEnv,
                               false,
                               PackageManagerUIRedirector.Get(this, ui),
                               elevate: await ShouldElevate(ui, operation)
                               )) {
                        if (!output.IsStarted)
                        {
                            // The finally block handles output
                            return(false);
                        }
                        var exitCode = await output;
                        success = exitCode == 0;
                    }
                    return(success);
                }
            } catch (IOException) {
                return(false);
            } finally {
                if (!success)
                {
                    // Check whether we failed because pip is missing
                    UpdateIsReadyAsync(false, CancellationToken.None).DoNotWait();
                }

                if (IsReady)
                {
                    await CacheInstalledPackagesAsync(false, false, cancellationToken);

                    if (!success)
                    {
                        // Double check whether the package has actually
                        // been uninstalled, to avoid reporting errors
                        // where, for all practical purposes, there is no
                        // error.
                        if (!(await GetInstalledPackageAsync(package, cancellationToken)).IsValid)
                        {
                            success = true;
                        }
                    }
                }

                var msg = success ? Strings.UninstallingPackageSuccess : Strings.UninstallingPackageFailed;
                ui?.OnOutputTextReceived(this, msg.FormatUI(name));
                ui?.OnOperationFinished(this, operation, success);
            }
        }
Ejemplo n.º 6
0
        public async Task <bool> ExecuteAsync(string arguments, IPackageManagerUI ui, CancellationToken cancellationToken)
        {
            AbortOnInvalidConfiguration();
            await AbortIfNotReady(cancellationToken);

            using (await _working.LockAsync(cancellationToken)) {
                bool success = false;

                var args = _extraInterpreterArgs.ToList();

                if (!SupportsDashMPip)
                {
                    args.Add("-c");
                    args.Add("\"import pip; pip.main()\"");
                }
                else
                {
                    args.Add("-m");
                    args.Add("pip");
                }
                string argStr = (string.Join(" ", args.Select(ProcessOutput.QuoteSingleArgument)) + " " + arguments).Trim();

                var operation = argStr;
                ui?.OnOutputTextReceived(this, operation);
                ui?.OnOperationStarted(this, Strings.ExecutingCommandStarted.FormatUI(arguments));

                try {
                    using (var output = ProcessOutput.Run(
                               _factory.Configuration.InterpreterPath,
                               new[] { argStr },
                               _factory.Configuration.PrefixPath,
                               UnbufferedEnv,
                               false,
                               PackageManagerUIRedirector.Get(this, ui),
                               quoteArgs: false,
                               elevate: await ShouldElevate(ui, operation)
                               )) {
                        if (!output.IsStarted)
                        {
                            return(false);
                        }
                        var exitCode = await output;
                        success = exitCode == 0;
                    }
                    return(success);
                } catch (IOException) {
                    return(false);
                } finally {
                    if (!success)
                    {
                        // Check whether we failed because pip is missing
                        UpdateIsReadyAsync(true, CancellationToken.None).DoNotWait();
                    }

                    var msg = success ? Strings.ExecutingCommandSucceeded : Strings.ExecutingCommandFailed;
                    ui?.OnOutputTextReceived(this, msg.FormatUI(arguments));
                    ui?.OnOperationFinished(this, operation, success);
                    await CacheInstalledPackagesAsync(true, false, cancellationToken);
                }
            }
        }