Exemplo n.º 1
0
        private async Task <bool> DoOperationAsync(
            IEnumerable <string> args,
            ICondaEnvironmentManagerUI ui,
            CancellationToken ct,
            Redirector redirector = null
            )
        {
            bool success = false;

            try {
                var envVars = await GetEnvironmentVariables();

                // Note: conda tries to write temporary files to the current working directory
                using (var output = ProcessOutput.Run(
                           CondaPath,
                           args,
                           Path.GetTempPath(),
                           envVars,
                           false,
                           redirector ?? CondaEnvironmentManagerUIRedirector.Get(this, ui),
                           quoteArgs: false,
                           elevate: false
                           )) {
                    if (!output.IsStarted)
                    {
                        return(false);
                    }
                    var exitCode = await output;
                    success = exitCode == 0;
                }
                return(success);
            } catch (IOException) {
                return(false);
            }
        }
        private async Task <bool> DoOperationAsync(
            IEnumerable <string> args,
            ICondaEnvironmentManagerUI ui,
            CancellationToken ct,
            Redirector redirector = null
            )
        {
            bool success = false;

            try {
                using (var output = ProcessOutput.Run(
                           CondaPath,
                           args,
                           Path.GetDirectoryName(CondaPath),
                           UnbufferedEnv,
                           false,
                           redirector ?? CondaEnvironmentManagerUIRedirector.Get(this, ui),
                           quoteArgs: false,
                           elevate: false
                           )) {
                    if (!output.IsStarted)
                    {
                        return(false);
                    }
                    var exitCode = await output;
                    success = exitCode == 0;
                }
                return(success);
            } catch (IOException) {
                return(false);
            }
        }
Exemplo n.º 3
0
        private async Task <bool> ExportAsync(string envPath, string destinationSpecFilePath, string[] args, ICondaEnvironmentManagerUI ui, CancellationToken ct)
        {
            bool success = false;

            using (await _working.LockAsync(ct)) {
                await EnsureActivatedAsync();

                var operation = "conda " + string.Join(" ", args);

                ui?.OnOperationStarted(this, operation);
                ui?.OnOutputTextReceived(this, Strings.CondaExportStarted.FormatUI(envPath));
                try {
                    if (!PathUtils.IsValidPath(envPath))
                    {
                        ui?.OnErrorTextReceived(this, Strings.CondaFolderNotFoundError.FormatUI(envPath));
                        success = false;
                        return(success);
                    }

                    var entries    = new List <string>();
                    var capture    = new ListRedirector(entries);
                    var redirector = new TeeRedirector(CondaEnvironmentManagerUIRedirector.Get(this, ui), capture);

                    success = await DoOperationAsync(args, ui, ct, redirector);

                    if (success)
                    {
                        try {
                            using (var writer = new StreamWriter(destinationSpecFilePath, false, Encoding.UTF8)) {
                                foreach (var line in entries)
                                {
                                    await writer.WriteLineAsync(line);
                                }
                            }
                        } catch (IOException ex) {
                            ui?.OnErrorTextReceived(this, ex.Message);
                            success = false;
                        } catch (UnauthorizedAccessException ex) {
                            ui?.OnErrorTextReceived(this, ex.Message);
                            success = false;
                        } catch (ArgumentException ex) {
                            ui?.OnErrorTextReceived(this, ex.Message);
                            success = false;
                        }
                    }

                    return(success);
                } finally {
                    var msg = success ? Strings.CondaExportSuccess : Strings.CondaExportFailed;
                    ui?.OnOutputTextReceived(this, msg.FormatUI(envPath));
                    ui?.OnOperationFinished(this, operation, success);
                }
            }
        }