Пример #1
0
        private void LogLoadException(IEnvironmentViewExtensionProvider provider, Exception ex)
        {
            string message;

            if (provider == null)
            {
                message = SR.GetString(SR.ErrorLoadingEnvironmentViewExtensions, ex);
            }
            else
            {
                message = SR.GetString(SR.ErrorLoadingEnvironmentViewExtension, provider.GetType().FullName, ex);
            }

            Debug.Fail(message);
            var log = _site.GetService(typeof(SVsActivityLog)) as IVsActivityLog;

            if (log != null)
            {
                log.LogEntry(
                    (uint)__ACTIVITYLOG_ENTRYTYPE.ALE_ERROR,
                    SR.ProductName,
                    message
                    );
            }
        }
Пример #2
0
        private void OpenInteractiveWindow_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            var view    = (EnvironmentView)e.Parameter;
            var factory = view.Factory;
            IVsInteractiveWindow window;

            var provider  = _service.KnownProviders.OfType <LoadedProjectInterpreterFactoryProvider>().FirstOrDefault();
            var vsProject = provider == null ?
                            null :
                            provider.GetProject(factory);
            var project = vsProject == null ? null : vsProject.GetPythonProject();

            try {
                window = ExecuteInReplCommand.EnsureReplWindow(_site, factory, project);
            } catch (InvalidOperationException ex) {
                MessageBox.Show(SR.GetString(SR.ErrorOpeningInteractiveWindow, ex), SR.ProductName);
                return;
            }
            if (window != null)
            {
                var pane = window as ToolWindowPane;
                if (pane != null)
                {
                    ErrorHandler.ThrowOnFailure(((IVsWindowFrame)pane.Frame).Show());
                }
                window.Show(true);
            }
        }
Пример #3
0
        private async void CreateProjectAndHandleErrors(
            IVsStatusbar statusBar,
            Microsoft.PythonTools.Project.ImportWizard.ImportWizard dlg
            )
        {
            try {
                var path = await dlg.ImportSettings.CreateRequestedProjectAsync();

                if (File.Exists(path))
                {
                    object outRef = null, pathRef = ProcessOutput.QuoteSingleArgument(path);
                    _serviceProvider.GetDTE().Commands.Raise(
                        VSConstants.GUID_VSStandardCommandSet97.ToString("B"),
                        (int)VSConstants.VSStd97CmdID.OpenProject,
                        ref pathRef,
                        ref outRef
                        );
                    statusBar.SetText("");
                    return;
                }
            } catch (UnauthorizedAccessException) {
                MessageBox.Show(SR.GetString(SR.ErrorImportWizardUnauthorizedAccess), SR.ProductName);
            } catch (Exception ex) {
                ActivityLog.LogError(SR.ProductName, ex.ToString());
                MessageBox.Show(SR.GetString(SR.ErrorImportWizardException, ex.GetType().Name), SR.ProductName);
            }
            statusBar.SetText(SR.GetString(SR.StatusImportWizardError));
        }
Пример #4
0
        protected override void OnCreate()
        {
            base.OnCreate();

            _site = (IServiceProvider)this;

            _pyService = _site.GetPythonToolsService();

            // TODO: Get PYEnvironment added to image list
            BitmapImageMoniker = KnownMonikers.DockPanel;
            Caption            = SR.GetString(SR.Environments);

            _service = _site.GetComponentModel().GetService <IInterpreterOptionsService>();

            _outputWindow = OutputWindowRedirector.GetGeneral(_site);
            Debug.Assert(_outputWindow != null);
            _statusBar = _site.GetService(typeof(SVsStatusbar)) as IVsStatusbar;

            var list = new ToolWindow();

            list.ViewCreated += List_ViewCreated;

            list.CommandBindings.Add(new CommandBinding(
                                         EnvironmentView.OpenInteractiveWindow,
                                         OpenInteractiveWindow_Executed,
                                         OpenInteractiveWindow_CanExecute
                                         ));
            list.CommandBindings.Add(new CommandBinding(
                                         EnvironmentView.OpenInteractiveOptions,
                                         OpenInteractiveOptions_Executed,
                                         OpenInteractiveOptions_CanExecute
                                         ));
            list.CommandBindings.Add(new CommandBinding(
                                         EnvironmentPathsExtension.StartInterpreter,
                                         StartInterpreter_Executed,
                                         StartInterpreter_CanExecute
                                         ));
            list.CommandBindings.Add(new CommandBinding(
                                         EnvironmentPathsExtension.StartWindowsInterpreter,
                                         StartInterpreter_Executed,
                                         StartInterpreter_CanExecute
                                         ));
            list.CommandBindings.Add(new CommandBinding(
                                         ApplicationCommands.Help,
                                         OnlineHelp_Executed,
                                         OnlineHelp_CanExecute
                                         ));
            list.CommandBindings.Add(new CommandBinding(
                                         ToolWindow.UnhandledException,
                                         UnhandledException_Executed,
                                         UnhandledException_CanExecute
                                         ));

            list.Service = _service;

            Content = list;
        }
Пример #5
0
 protected override void WriteInitializationMessage()
 {
     if (Interpreter is UnavailableFactory)
     {
         Window.WriteError(SR.GetString(SR.ReplEvaluatorInterpreterNotFound));
     }
     else
     {
         base.WriteInitializationMessage();
     }
 }
Пример #6
0
        protected override void OnCreate()
        {
            base.OnCreate();

            _site = (IServiceProvider)this;

            _pyService = _site.GetPythonToolsService();

            BitmapResourceID = PythonConstants.ResourceIdForReplImages;
            BitmapIndex      = 0;
            Caption          = SR.GetString(SR.Environments);

            _service = _site.GetComponentModel().GetService <IInterpreterOptionsService>();

            _outputWindow = OutputWindowRedirector.GetGeneral(_site);
            Debug.Assert(_outputWindow != null);
            _statusBar = _site.GetService(typeof(SVsStatusbar)) as IVsStatusbar;

            var list = new ToolWindow();

            list.ViewCreated += List_ViewCreated;

            list.CommandBindings.Add(new CommandBinding(
                                         EnvironmentView.OpenInteractiveWindow,
                                         OpenInteractiveWindow_Executed,
                                         OpenInteractiveWindow_CanExecute
                                         ));
            list.CommandBindings.Add(new CommandBinding(
                                         EnvironmentView.OpenInteractiveOptions,
                                         OpenInteractiveOptions_Executed,
                                         OpenInteractiveOptions_CanExecute
                                         ));
            list.CommandBindings.Add(new CommandBinding(
                                         EnvironmentPathsExtension.StartInterpreter,
                                         StartInterpreter_Executed,
                                         StartInterpreter_CanExecute
                                         ));
            list.CommandBindings.Add(new CommandBinding(
                                         EnvironmentPathsExtension.StartWindowsInterpreter,
                                         StartInterpreter_Executed,
                                         StartInterpreter_CanExecute
                                         ));
            list.CommandBindings.Add(new CommandBinding(
                                         ToolWindow.UnhandledException,
                                         UnhandledException_Executed,
                                         UnhandledException_CanExecute
                                         ));

            list.Service = _service;

            Content = list;
        }
Пример #7
0
        public override void DoCommand(object sender, EventArgs args)
        {
            var statusBar = (IVsStatusbar)_serviceProvider.GetService(typeof(SVsStatusbar));

            statusBar.SetText(SR.GetString(SR.StatusImportWizardStarting));

            string initialProjectPath = null, initialSourcePath = null;

            var oleArgs = args as Microsoft.VisualStudio.Shell.OleMenuCmdEventArgs;

            if (oleArgs != null)
            {
                string projectArgs = oleArgs.InValue as string;
                if (projectArgs != null)
                {
                    var argItems = projectArgs.Split('|');
                    if (argItems.Length == 2)
                    {
                        initialProjectPath = CommonUtils.GetAvailableFilename(
                            argItems[1],
                            argItems[0],
                            ".pyproj"
                            );
                        initialSourcePath = argItems[1];
                    }
                }
            }

            var dlg = new Microsoft.PythonTools.Project.ImportWizard.ImportWizard(
                _serviceProvider,
                initialSourcePath,
                initialProjectPath
                );

            if (dlg.ShowModal() ?? false)
            {
                CreateProjectAndHandleErrors(statusBar, dlg);
            }
            else
            {
                statusBar.SetText("");
            }
        }
Пример #8
0
        protected override void Connect()
        {
            _serviceProvider.GetUIThread().MustBeCalledFromUIThread();

            var configurableOptions = CurrentOptions as ConfigurablePythonReplOptions;

            if (configurableOptions != null)
            {
                _interpreter = configurableOptions.InterpreterFactory ?? _interpreter;
            }

            if (Interpreter == null || Interpreter is UnavailableFactory)
            {
                Window.WriteError(SR.GetString(SR.ReplEvaluatorInterpreterNotFound));
                return;
            }
            else if (String.IsNullOrWhiteSpace(Interpreter.Configuration.InterpreterPath))
            {
                Window.WriteError(SR.GetString(SR.ReplEvaluatorInterpreterNotConfigured, Interpreter.Description));
                return;
            }
            var processInfo = new ProcessStartInfo(Interpreter.Configuration.InterpreterPath);

#if DEBUG
            bool debugMode = Environment.GetEnvironmentVariable("DEBUG_REPL") != null;
            processInfo.CreateNoWindow         = !debugMode;
            processInfo.UseShellExecute        = debugMode;
            processInfo.RedirectStandardOutput = !debugMode;
            processInfo.RedirectStandardError  = !debugMode;
            processInfo.RedirectStandardInput  = !debugMode;
#else
            processInfo.CreateNoWindow         = true;
            processInfo.UseShellExecute        = false;
            processInfo.RedirectStandardOutput = true;
            processInfo.RedirectStandardError  = true;
            processInfo.RedirectStandardInput  = true;
#endif

            Socket conn;
            int    portNum;
            CreateConnection(out conn, out portNum);


            List <string> args = new List <string>();

            if (!String.IsNullOrWhiteSpace(CurrentOptions.InterpreterOptions))
            {
                args.Add(CurrentOptions.InterpreterOptions);
            }

            var workingDir = CurrentOptions.WorkingDirectory;
            if (!string.IsNullOrEmpty(workingDir))
            {
                processInfo.WorkingDirectory = workingDir;
            }
            else if (configurableOptions != null && configurableOptions.Project != null)
            {
                processInfo.WorkingDirectory = configurableOptions.Project.GetWorkingDirectory();
            }
            else
            {
                processInfo.WorkingDirectory = Interpreter.Configuration.PrefixPath;
            }

#if DEBUG
            if (!debugMode)
            {
#endif
            var envVars = CurrentOptions.EnvironmentVariables;
            if (envVars != null)
            {
                foreach (var keyValue in envVars)
                {
                    processInfo.EnvironmentVariables[keyValue.Key] = keyValue.Value;
                }
            }

            string pathEnvVar = Interpreter.Configuration.PathEnvironmentVariable ?? "";

            if (!string.IsNullOrWhiteSpace(pathEnvVar))
            {
                var searchPaths = CurrentOptions.SearchPaths;

                if (string.IsNullOrEmpty(searchPaths))
                {
                    if (_serviceProvider.GetPythonToolsService().GeneralOptions.ClearGlobalPythonPath)
                    {
                        processInfo.EnvironmentVariables[pathEnvVar] = "";
                    }
                }
                else if (_serviceProvider.GetPythonToolsService().GeneralOptions.ClearGlobalPythonPath)
                {
                    processInfo.EnvironmentVariables[pathEnvVar] = searchPaths;
                }
                else
                {
                    processInfo.EnvironmentVariables[pathEnvVar] = searchPaths + ";" + Environment.GetEnvironmentVariable(pathEnvVar);
                }
            }
#if DEBUG
        }
#endif
            var interpreterArgs = CurrentOptions.InterpreterArguments;
            if (!String.IsNullOrWhiteSpace(interpreterArgs))
            {
                args.Add(interpreterArgs);
            }

            var analyzer = CurrentOptions.ProjectAnalyzer;
            if (analyzer != null && analyzer.InterpreterFactory == _interpreter)
            {
                if (_replAnalyzer != null && _replAnalyzer != analyzer)
                {
                    analyzer.SwitchAnalyzers(_replAnalyzer);
                }
                _replAnalyzer = analyzer;
                _ownsAnalyzer = false;
            }

            args.Add(ProcessOutput.QuoteSingleArgument(PythonToolsInstallPath.GetFile("visualstudio_py_repl.py")));
            args.Add("--port");
            args.Add(portNum.ToString());

            if (!String.IsNullOrWhiteSpace(CurrentOptions.StartupScript))
            {
                args.Add("--launch_file");
                args.Add(ProcessOutput.QuoteSingleArgument(CurrentOptions.StartupScript));
            }

            _enableAttach = CurrentOptions.EnableAttach;
            if (CurrentOptions.EnableAttach)
            {
                args.Add("--enable-attach");
            }

            bool multipleScopes = true;
            if (!String.IsNullOrWhiteSpace(CurrentOptions.ExecutionMode))
            {
                // change ID to module name if we have a registered mode
                var    modes     = Microsoft.PythonTools.Options.ExecutionMode.GetRegisteredModes(_serviceProvider);
                string modeValue = CurrentOptions.ExecutionMode;
                foreach (var mode in modes)
                {
                    if (mode.Id == CurrentOptions.ExecutionMode)
                    {
                        modeValue      = mode.Type;
                        multipleScopes = mode.SupportsMultipleScopes;
                        _supportsMultipleCompleteStatementInputs = mode.SupportsMultipleCompleteStatementInputs;
                        break;
                    }
                }
                args.Add("--execution_mode");
                args.Add(modeValue);
            }

            SetMultipleScopes(multipleScopes);

            processInfo.Arguments = String.Join(" ", args);

            var process = new Process();
            process.StartInfo = processInfo;
            try {
                if (!File.Exists(processInfo.FileName))
                {
                    throw new Win32Exception(Microsoft.VisualStudioTools.Project.NativeMethods.ERROR_FILE_NOT_FOUND);
                }
                process.Start();
            } catch (Exception e) {
                if (e.IsCriticalException())
                {
                    throw;
                }

                Win32Exception wex = e as Win32Exception;
                if (wex != null && wex.NativeErrorCode == Microsoft.VisualStudioTools.Project.NativeMethods.ERROR_FILE_NOT_FOUND)
                {
                    Window.WriteError(SR.GetString(SR.ReplEvaluatorInterpreterNotFound));
                }
                else
                {
                    Window.WriteError(SR.GetString(SR.ErrorStartingInteractiveProcess, e.ToString()));
                }
                return;
            }

            CreateCommandProcessor(conn, processInfo.RedirectStandardOutput, process);
        }
Пример #9
0
 public override string GetClassName() => SR.GetString(SR.ReferenceProperties);
Пример #10
0
        public static async Task <bool> Install(
            IServiceProvider provider,
            IPythonInterpreterFactory factory,
            IInterpreterOptionsService service,
            string package,
            Redirector output = null
            )
        {
            factory.ThrowIfNotRunnable("factory");

            IPythonInterpreterFactory condaFactory;

            if (!TryGetCondaFactory(factory, service, out condaFactory))
            {
                throw new InvalidOperationException("Cannot find conda");
            }
            condaFactory.ThrowIfNotRunnable();

            if (output != null)
            {
                output.WriteLine(SR.GetString(SR.PackageInstalling, package));
                if (provider.GetPythonToolsService().GeneralOptions.ShowOutputWindowForPackageInstallation)
                {
                    output.ShowAndActivate();
                }
                else
                {
                    output.Show();
                }
            }

            using (var proc = ProcessOutput.Run(
                       condaFactory.Configuration.InterpreterPath,
                       new[] { "-m", "conda", "install", "--yes", "-n", factory.Configuration.PrefixPath, package },
                       factory.Configuration.PrefixPath,
                       UnbufferedEnv,
                       false,
                       output
                       )) {
                var exitCode = await proc;
                if (output != null)
                {
                    if (exitCode == 0)
                    {
                        output.WriteLine(SR.GetString(SR.PackageInstallSucceeded, package));
                    }
                    else
                    {
                        output.WriteLine(SR.GetString(SR.PackageInstallFailedExitCode, package, exitCode));
                    }
                    if (provider.GetPythonToolsService().GeneralOptions.ShowOutputWindowForPackageInstallation)
                    {
                        output.ShowAndActivate();
                    }
                    else
                    {
                        output.Show();
                    }
                }
                return(exitCode == 0);
            }
        }