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

import com.intellij.ide.DataManager;
import com.intellij.openapi.actionSystem.PlatformDataKeys;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.SystemInfo;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.util.SystemProperties;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * User : catherine
 */
public class VirtualEnvSdkFlavor extends CPythonSdkFlavor {
  private VirtualEnvSdkFlavor() {
  }

  private final static String[] NAMES = new String[]{"python", "jython", "pypy", "python.exe", "jython.bat", "pypy.exe"};

  public static VirtualEnvSdkFlavor INSTANCE = new VirtualEnvSdkFlavor();

  @Override
  public Collection<String> suggestHomePaths() {
    final Project project = PlatformDataKeys.PROJECT.getData(DataManager.getInstance().getDataContext());
    List<String> candidates = new ArrayList<String>();
    if (project != null) {
      VirtualFile rootDir = project.getBaseDir();
      if (rootDir != null)
        candidates.addAll(findInDirectory(rootDir));
    }
    
    final VirtualFile path = getDefaultLocation();
    if (path != null)
      candidates.addAll(findInDirectory(path));

    return candidates;
  }

  @Nullable
  public static VirtualFile getDefaultLocation() {
    final String path = System.getenv().get("WORKON_HOME");
    if (path != null) return LocalFileSystem.getInstance().findFileByPath(path.replace('\\','/'));

    final VirtualFile userHome = LocalFileSystem.getInstance().findFileByPath(SystemProperties.getUserHome().replace('\\','/'));
    if (userHome != null) {
      return userHome.findChild(".virtualenvs");
    }
    return null;
  }

  public static Collection<String> findInDirectory(VirtualFile rootDir) {
    List<String> candidates = new ArrayList<String>();
    if (rootDir != null) {
      VirtualFile[] suspects = rootDir.getChildren();
      for (VirtualFile child : suspects) {
        if (child.isDirectory()) {
          final String childName = child.getName();
          if (childName.equals("bin") || childName.equals("Scripts"))
            candidates.addAll(findInterpreter(child));
          else
            candidates.addAll(findInDirectory(child));
        }
      }
    }
    return candidates;
  }

  private static Collection<String> findInterpreter(VirtualFile dir) {
    List<String> candidates = new ArrayList<String>();
    for (VirtualFile child : dir.getChildren()) {
      if (!child.isDirectory()) {
        final String childName = child.getName();
        for (String name : NAMES) {
          if (SystemInfo.isWindows && childName.equals(name)) {
            candidates.add(child.getPath());
          }
          else if (childName.startsWith(name)) {
            if (!childName.endsWith("-config")) candidates.add(child.getPath());
            break;
          }
        }
      }
    }
    return candidates;
  }

  @Override
  public boolean isValidSdkPath(@NotNull File file) {
    if (!super.isValidSdkPath(file)) return false;
    File bin = file.getParentFile();
    if (bin != null) {
      File[] children = bin.listFiles();
      if (children != null) {
        for (File f : children) {
          //is it good enough to determine virtual env?
          if (f.getName().equals("activate_this.py")) {
            return true;
          }
        }
      }
    }
    return false;
  }
}<commit_msg>icon for virtualenv; use toolbar button instead of regular button for creating virtualenv<commit_after>package com.jetbrains.python.sdk;

import com.intellij.ide.DataManager;
import com.intellij.openapi.actionSystem.PlatformDataKeys;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.IconLoader;
import com.intellij.openapi.util.SystemInfo;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.util.SystemProperties;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * User : catherine
 */
public class VirtualEnvSdkFlavor extends CPythonSdkFlavor {
  private VirtualEnvSdkFlavor() {
  }

  private final static String[] NAMES = new String[]{"python", "jython", "pypy", "python.exe", "jython.bat", "pypy.exe"};

  public static VirtualEnvSdkFlavor INSTANCE = new VirtualEnvSdkFlavor();

  @Override
  public Collection<String> suggestHomePaths() {
    final Project project = PlatformDataKeys.PROJECT.getData(DataManager.getInstance().getDataContext());
    List<String> candidates = new ArrayList<String>();
    if (project != null) {
      VirtualFile rootDir = project.getBaseDir();
      if (rootDir != null)
        candidates.addAll(findInDirectory(rootDir));
    }
    
    final VirtualFile path = getDefaultLocation();
    if (path != null)
      candidates.addAll(findInDirectory(path));

    return candidates;
  }

  @Nullable
  public static VirtualFile getDefaultLocation() {
    final String path = System.getenv().get("WORKON_HOME");
    if (path != null) return LocalFileSystem.getInstance().findFileByPath(path.replace('\\','/'));

    final VirtualFile userHome = LocalFileSystem.getInstance().findFileByPath(SystemProperties.getUserHome().replace('\\','/'));
    if (userHome != null) {
      return userHome.findChild(".virtualenvs");
    }
    return null;
  }

  public static Collection<String> findInDirectory(VirtualFile rootDir) {
    List<String> candidates = new ArrayList<String>();
    if (rootDir != null) {
      VirtualFile[] suspects = rootDir.getChildren();
      for (VirtualFile child : suspects) {
        if (child.isDirectory()) {
          final String childName = child.getName();
          if (childName.equals("bin") || childName.equals("Scripts"))
            candidates.addAll(findInterpreter(child));
          else
            candidates.addAll(findInDirectory(child));
        }
      }
    }
    return candidates;
  }

  private static Collection<String> findInterpreter(VirtualFile dir) {
    List<String> candidates = new ArrayList<String>();
    for (VirtualFile child : dir.getChildren()) {
      if (!child.isDirectory()) {
        final String childName = child.getName();
        for (String name : NAMES) {
          if (SystemInfo.isWindows && childName.equals(name)) {
            candidates.add(child.getPath());
          }
          else if (childName.startsWith(name)) {
            if (!childName.endsWith("-config")) candidates.add(child.getPath());
            break;
          }
        }
      }
    }
    return candidates;
  }

  @Override
  public boolean isValidSdkPath(@NotNull File file) {
    if (!super.isValidSdkPath(file)) return false;
    File bin = file.getParentFile();
    if (bin != null) {
      File[] children = bin.listFiles();
      if (children != null) {
        for (File f : children) {
          //is it good enough to determine virtual env?
          if (f.getName().equals("activate_this.py")) {
            return true;
          }
        }
      }
    }
    return false;
  }

  @Override
  public Icon getIcon() {
    return IconLoader.getIcon("/com/jetbrains/python/icons/virtualenv.png");
  }
}
{'fraction_non_alphanumeric': 0.12562138922477495, 'fraction_numerical': 0.0, 'mean_word_length': 3.2127900396151667, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, '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': '4439707', 'n_tokens_mistral': 2163, 'n_tokens_neox': 2144, 'n_words': 464}