public void GenerateDatabase(GenerateDatabaseOptions options, Action <int> onExit = null)
 {
     IsCurrentReason = GeneratingReason;
     IsCurrent       = false;
     if (_useUpdater)
     {
         if (_updater != null)
         {
             _updater.Dispose();
         }
         _updater = new AnalyzerStatusUpdater(AnalyzerStatusUpdater.GetIdentifier(Id, _config.Version));
         _updater.WaitForWorkerStarted();
         _updater.ThrowPendingExceptions();
         _updater.UpdateStatus(0, 0);
     }
 }
Пример #2
0
        private async void Listener_ProgressUpdate(Dictionary <string, AnalysisProgress> status)
        {
            bool anyUpdates = status.Any();

            if (!anyUpdates)
            {
                lock (_environmentsLock) {
                    anyUpdates = _currentlyRefreshing.Count != 0;
                }
            }

            if (anyUpdates)
            {
                var updates = new List <DispatcherOperation>();

                lock (_environmentsLock) {
                    foreach (var env in _environments)
                    {
                        if (env.Factory == null)
                        {
                            continue;
                        }

                        AnalysisProgress progress;
                        if (status.TryGetValue(AnalyzerStatusUpdater.GetIdentifier(env.Factory), out progress))
                        {
                            _currentlyRefreshing.Add(env.Factory);

                            updates.Add(env.Dispatcher.InvokeAsync(() => {
                                if (progress.Maximum > 0)
                                {
                                    var percent = progress.Progress * 100 / progress.Maximum;
                                    var current = env.RefreshDBProgress;
                                    // Filter out small instances of 'reverse'
                                    // progress, but allow big jumps backwards.
                                    if (percent > current || percent < current - 25)
                                    {
                                        env.RefreshDBProgress = percent;
                                    }
                                    env.IsRefreshDBProgressIndeterminate = false;
                                }
                                else
                                {
                                    env.IsRefreshDBProgressIndeterminate = true;
                                }
                                env.RefreshDBMessage = progress.Message;
                                env.IsRefreshingDB   = true;
                            }));
                        }
                        else if (_currentlyRefreshing.Remove(env.Factory))
                        {
                            updates.Add(env.Dispatcher.InvokeAsync(() => {
                                env.IsRefreshingDB = false;
                                env.IsRefreshDBProgressIndeterminate = false;
                                env.RefreshDBMessage = string.Empty;
                                CommandManager.InvalidateRequerySuggested();
                            }));
                        }
                    }
                }

                try {
                    await Task.WhenAll(updates.Select(d => d.Task).ToArray());
                } catch (OperationCanceledException) {
                    // Tasks were cancelled, which probably means we are closing.
                    // In this case, _timer will be disposed before the next update.
                }
            }

            if (Interlocked.Decrement(ref _listenerTimeToLive) == 0)
            {
                // It's time to reset the listener. We do this periodically in
                // case the global mutex has become abandoned. By releasing our
                // handle, it should go away and any errors (which may be caused
                // by users killing the analyzer) become transient rather than
                // permanent.

                // Because we are currently on the listener's thread, we need to
                // recreate on a separate thread so that this one can terminate.
                Task.Run((Action)CreateListener)
                .HandleAllExceptions(Properties.Resources.PythonToolsForVisualStudio)
                .DoNotWait();
            }
        }
Пример #3
0
        public static async Task <int> GenerateAsync(PythonTypeDatabaseCreationRequest request)
        {
            var fact = request.Factory;
            var evt  = request.OnExit;

            if (fact == null || !Directory.Exists(fact.Configuration.LibraryPath))
            {
                if (evt != null)
                {
                    evt(NotSupportedExitCode);
                }
                return(NotSupportedExitCode);
            }
            var outPath = request.OutputPath;

            var analyzerPath = PythonToolsInstallPath.GetFile("Microsoft.PythonTools.Analyzer.exe");

            Directory.CreateDirectory(CompletionDatabasePath);

            var baseDb = BaselineDatabasePath;

            if (request.ExtraInputDatabases.Any())
            {
                baseDb = baseDb + ";" + string.Join(";", request.ExtraInputDatabases);
            }

            var logPath  = Path.Combine(outPath, "AnalysisLog.txt");
            var glogPath = Path.Combine(CompletionDatabasePath, "AnalysisLog.txt");

            using (var output = ProcessOutput.RunHiddenAndCapture(
                       analyzerPath,
                       "/id", fact.Configuration.Id,
                       "/version", fact.Configuration.Version.ToString(),
                       "/python", fact.Configuration.InterpreterPath,
                       request.DetectLibraryPath ? null : "/library",
                       request.DetectLibraryPath ? null : fact.Configuration.LibraryPath,
                       "/outdir", outPath,
                       "/basedb", baseDb,
                       (request.SkipUnchanged ? null : "/all"), // null will be filtered out; empty strings are quoted
                       "/log", logPath,
                       "/glog", glogPath,
                       "/wait", (request.WaitFor != null ? AnalyzerStatusUpdater.GetIdentifier(request.WaitFor) : "")
                       )) {
                output.PriorityClass = ProcessPriorityClass.BelowNormal;
                int exitCode = await output;

                if (exitCode > -10 && exitCode < 0)
                {
                    try {
                        File.AppendAllLines(
                            glogPath,
                            new[] { string.Format("FAIL_STDLIB: ({0}) {1}", exitCode, output.Arguments) }
                            .Concat(output.StandardErrorLines)
                            );
                    } catch (IOException) {
                    } catch (ArgumentException) {
                    } catch (SecurityException) {
                    } catch (UnauthorizedAccessException) {
                    }
                }

                if (evt != null)
                {
                    evt(exitCode);
                }
                return(exitCode);
            }
        }
Пример #4
0
        public void RefreshDBStates()
        {
            using (var fact = new MockPythonInterpreterFactory(
                       Guid.NewGuid(),
                       "Test Factory 1",
                       MockInterpreterConfiguration(
                           PythonPaths.Versions.First().InterpreterPath
                           ),
                       true
                       ))
                using (var wpf = new WpfProxy())
                    using (var list = new EnvironmentListProxy(wpf)) {
                        list.CreateDBExtension = true;

                        var mockService = new MockInterpreterOptionsService();
                        mockService.AddProvider(new MockPythonInterpreterFactoryProvider("Test Provider 1", fact));
                        list.Service = mockService;
                        var view = list.Environments.Single();

                        Assert.IsFalse(wpf.Invoke(() => view.IsRefreshingDB));
                        Assert.IsTrue(list.CanExecute(DBExtension.StartRefreshDB, view));
                        Assert.IsFalse(fact.IsCurrent);
                        Assert.AreEqual(MockPythonInterpreterFactory.NoDatabaseReason, fact.GetIsCurrentReason(null));

                        list.Execute(DBExtension.StartRefreshDB, view).GetAwaiter().GetResult();
                        for (int retries = 10; retries > 0 && !wpf.Invoke(() => view.IsRefreshingDB); --retries)
                        {
                            Thread.Sleep(200);
                        }

                        Assert.IsTrue(wpf.Invoke(() => view.IsRefreshingDB));
                        Assert.IsFalse(list.CanExecute(DBExtension.StartRefreshDB, view));
                        Assert.IsFalse(fact.IsCurrent);
                        Assert.AreEqual(MockPythonInterpreterFactory.GeneratingReason, fact.GetIsCurrentReason(null));

                        fact.EndGenerateCompletionDatabase(AnalyzerStatusUpdater.GetIdentifier(fact), false);
                        for (int retries = 10; retries > 0 && wpf.Invoke(() => view.IsRefreshingDB); --retries)
                        {
                            Thread.Sleep(1000);
                        }

                        Assert.IsFalse(wpf.Invoke(() => view.IsRefreshingDB));
                        Assert.IsTrue(list.CanExecute(DBExtension.StartRefreshDB, view));
                        Assert.IsFalse(fact.IsCurrent);
                        Assert.AreEqual(MockPythonInterpreterFactory.MissingModulesReason, fact.GetIsCurrentReason(null));

                        list.Execute(DBExtension.StartRefreshDB, view).GetAwaiter().GetResult();

                        Assert.IsTrue(wpf.Invoke(() => view.IsRefreshingDB));
                        Assert.IsFalse(list.CanExecute(DBExtension.StartRefreshDB, view));
                        Assert.IsFalse(fact.IsCurrent);
                        Assert.AreEqual(MockPythonInterpreterFactory.GeneratingReason, fact.GetIsCurrentReason(null));

                        fact.EndGenerateCompletionDatabase(AnalyzerStatusUpdater.GetIdentifier(fact), true);
                        for (int retries = 10; retries > 0 && wpf.Invoke(() => view.IsRefreshingDB); --retries)
                        {
                            Thread.Sleep(1000);
                        }

                        Assert.IsFalse(wpf.Invoke(() => view.IsRefreshingDB));
                        Assert.IsTrue(list.CanExecute(DBExtension.StartRefreshDB, view));
                        Assert.IsTrue(fact.IsCurrent);
                        Assert.AreEqual(MockPythonInterpreterFactory.UpToDateReason, fact.GetIsCurrentReason(null));
                        Assert.AreEqual(MockPythonInterpreterFactory.UpToDateReason, fact.GetIsCurrentReason(null));
                    }
        }