<commit_before>package com.jetbrains.python.sdk;

import com.intellij.openapi.application.Application;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleManager;
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.progress.Task;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.projectRoots.Sdk;
import com.intellij.openapi.projectRoots.SdkModificator;
import com.intellij.openapi.projectRoots.SdkType;
import com.intellij.openapi.roots.OrderRootType;
import com.intellij.openapi.startup.StartupActivity;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.jetbrains.python.PyBundle;
import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.io.IOException;
import java.util.*;

/**
 * A component that initiates a refresh of all project's Python SDKs.
 * Delegates most of the work to PythonSdkType.
 * <br/>
 * @author yole
 */
public class PythonSdkUpdater implements StartupActivity {
  private static final Logger LOG = Logger.getInstance("#com.jetbrains.python.sdk.PythonSdkUpdater");

  private final Set<String> myAlreadyUpdated = new HashSet<String>();

  @Override
  public void runActivity(final Project project) {
    final Application application = ApplicationManager.getApplication();
    if (application.isUnitTestMode()) {
      return;
    }

    final Set<Sdk> sdksToUpdate = new HashSet<Sdk>();
    for (Module module : ModuleManager.getInstance(project).getModules()) {
      final Sdk sdk = PythonSdkType.findPythonSdk(module);
      if (sdk != null) {
        final SdkType sdkType = sdk.getSdkType();
        if (sdkType instanceof PythonSdkType && !myAlreadyUpdated.contains(sdk.getHomePath())) {
          sdksToUpdate.add(sdk);
        }
      }
    }
    
    // NOTE: everything is run later on the AWT thread
    if (!sdksToUpdate.isEmpty()) {
      ApplicationManager.getApplication().executeOnPooledThread(new Runnable() {
        public void run() {
          try {
            Thread.sleep(7000); // wait until all short-term disk-hitting activity ceases
          }
          catch (InterruptedException ignore) {}
          // update skeletons
          ApplicationManager.getApplication().invokeLater(new Runnable() {
            @Override
            public void run() {
              ProgressManager.getInstance().run(new Task.Backgroundable(project, PyBundle.message("sdk.gen.updating.skels"), false) {
                @Override
                public void run(@NotNull ProgressIndicator indicator) {
                  for (Sdk sdk : sdksToUpdate) {
                    PythonSdkType.refreshSkeletonsOfSDK(sdk); // NOTE: whole thing would need a rename

                    long start_time = System.currentTimeMillis();
                    updateSdkPath(sdk, PythonSdkType.getSysPath(sdk.getHomePath()));
                    LOG.info("Updating sys.path took " + (System.currentTimeMillis() - start_time) + " ms");<commit_msg>update roots in EDT; update skeletons on first activation of a certain interpreter in the project<commit_after>package com.jetbrains.python.sdk;

import com.intellij.openapi.application.Application;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.extensions.Extensions;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleManager;
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.progress.Task;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.projectRoots.Sdk;
import com.intellij.openapi.projectRoots.SdkModificator;
import com.intellij.openapi.projectRoots.SdkType;
import com.intellij.openapi.roots.OrderRootType;
import com.intellij.openapi.startup.StartupActivity;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.jetbrains.python.PyBundle;
import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.io.IOException;
import java.util.*;

/**
 * A component that initiates a refresh of all project's Python SDKs.
 * Delegates most of the work to PythonSdkType.
 * <br/>
 * @author yole
 */
public class PythonSdkUpdater implements StartupActivity {
  private static final Logger LOG = Logger.getInstance("#com.jetbrains.python.sdk.PythonSdkUpdater");

  private final Set<String> myAlreadyUpdated = new HashSet<String>();
  
  public static PythonSdkUpdater getInstance() {
    final StartupActivity[] extensions = Extensions.getExtensions(StartupActivity.POST_STARTUP_ACTIVITY);
    for (StartupActivity extension : extensions) {
      if (extension instanceof PythonSdkUpdater) {
        return (PythonSdkUpdater) extension;
      }
    }
    throw new UnsupportedOperationException("could not find self");
  }

  @Override
  public void runActivity(final Project project) {
    final Application application = ApplicationManager.getApplication();
    if (application.isUnitTestMode()) {
      return;
    }

    updateActiveSdks(project, 7000);
  }

  public void updateActiveSdks(final Project project, final int delay) {
    final Set<Sdk> sdksToUpdate = new HashSet<Sdk>();
    for (Module module : ModuleManager.getInstance(project).getModules()) {
      final Sdk sdk = PythonSdkType.findPythonSdk(module);
      if (sdk != null) {
        final SdkType sdkType = sdk.getSdkType();
        if (sdkType instanceof PythonSdkType && !myAlreadyUpdated.contains(sdk.getHomePath())) {
          sdksToUpdate.add(sdk);
        }
      }
    }

    // NOTE: everything is run later on the AWT thread
    if (!sdksToUpdate.isEmpty()) {
      ApplicationManager.getApplication().executeOnPooledThread(new Runnable() {
        public void run() {
          if (delay > 0) {
            try {
              Thread.sleep(delay); // wait until all short-term disk-hitting activity ceases
            }
            catch (InterruptedException ignore) {
            }
          }
          // update skeletons
          ApplicationManager.getApplication().invokeLater(new Runnable() {
            @Override
            public void run() {
              ProgressManager.getInstance().run(new Task.Backgroundable(project, PyBundle.message("sdk.gen.updating.skels"), false) {
                @Override
                public void run(@NotNull ProgressIndicator indicator) {
                  for (final Sdk sdk : sdksToUpdate) {
                    PythonSdkType.refreshSkeletonsOfSDK(sdk); // NOTE: whole thing would need a rename

                    long start_time = System.currentTimeMillis();
                    final List<String> sysPath = PythonSdkType.getSysPath(sdk.getHomePath());
                    ApplicationManager.getApplication().invokeLater(new Runnable() {
                      @Override
                      public void run() {
                        updateSdkPath(sdk, sysPath);
                      }
                    });
                    LOG.info("Updating sys.path took " + (System.currentTimeMillis() - start_time) + " ms");
{'fraction_non_alphanumeric': 0.09878730072216924, 'fraction_numerical': 0.0012263251124131353, 'mean_word_length': 3.1657207718501703, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4150808', 'n_tokens_mistral': 2112, 'n_tokens_neox': 2053, 'n_words': 445}