2 * Copyright 2000-2013 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.jetbrains.python.sdk.flavors;
18 import com.google.common.collect.ImmutableMap;
19 import com.intellij.openapi.util.io.FileUtil;
20 import com.intellij.openapi.util.io.WindowsRegistryUtil;
21 import com.intellij.openapi.util.text.StringUtil;
22 import com.intellij.openapi.vfs.LocalFileSystem;
23 import com.intellij.openapi.vfs.VirtualFile;
24 import com.intellij.openapi.vfs.newvfs.NewVirtualFile;
25 import com.jetbrains.python.PythonHelpersLocator;
33 public class WinPythonSdkFlavor extends CPythonSdkFlavor {
34 public static WinPythonSdkFlavor INSTANCE = new WinPythonSdkFlavor();
35 private static Map<String, String> ourRegistryMap =
36 ImmutableMap.of("HKEY_LOCAL_MACHINE\\SOFTWARE\\Python\\PythonCore", "python.exe",
37 "HKEY_LOCAL_MACHINE\\SOFTWARE\\Wow6432Node\\Python\\PythonCore", "python.exe",
38 "HKEY_LOCAL_MACHINE\\SOFTWARE\\IronPython", "ipy.exe");
40 private WinPythonSdkFlavor() {
44 public Collection<String> suggestHomePaths() {
45 Set<String> candidates = new TreeSet<String>();
46 findInCandidatePaths(candidates, "python.exe", "jython.bat", "pypy.exe");
47 findInstallations(candidates, "python.exe", PythonHelpersLocator.getHelpersRoot().getParent());
51 private static void findInCandidatePaths(Set<String> candidates, String... exe_names) {
52 for (String name : exe_names) {
53 findInstallations(candidates, name, "C:\\", "C:\\Program Files\\");
54 findInPath(candidates, name);
55 findInRegistry(candidates);
59 private static void findInstallations(Set<String> candidates, String exe_name, String... roots) {
60 for (String root : roots) {
61 findSubdirInstallations(candidates, root, FileUtil.getNameWithoutExtension(exe_name), exe_name);
65 public static void findInPath(Collection<String> candidates, String exeName) {
66 final String path = System.getenv("PATH");
67 if (path == null) return;
68 for (String pathEntry : StringUtil.split(path, ";")) {
69 if (pathEntry.startsWith("\"") && pathEntry.endsWith("\"")) {
70 if (pathEntry.length() < 2) continue;
71 pathEntry = pathEntry.substring(1, pathEntry.length() - 1);
73 File f = new File(pathEntry, exeName);
75 candidates.add(FileUtil.toSystemDependentName(f.getPath()));
80 public static void findInRegistry(Collection<String> candidates) {
81 for (Map.Entry<String, String> entry : ourRegistryMap.entrySet()) {
82 final String prefix = entry.getKey();
83 final String exePath = entry.getValue();
84 List<String> strings = WindowsRegistryUtil.readRegistryBranch(prefix);
85 for (String string : strings) {
87 WindowsRegistryUtil.readRegistryDefault(prefix + "\\" + string +
90 File f = new File(path, exePath);
92 candidates.add(FileUtil.toSystemDependentName(f.getPath()));
99 private static void findSubdirInstallations(Collection<String> candidates, String rootDir, String dir_prefix, String exe_name) {
100 VirtualFile rootVDir = LocalFileSystem.getInstance().findFileByPath(rootDir);
101 if (rootVDir != null) {
102 if (rootVDir instanceof NewVirtualFile) {
103 ((NewVirtualFile)rootVDir).markDirty();
105 rootVDir.refresh(true, false);
106 for (VirtualFile dir : rootVDir.getChildren()) {
107 if (dir.isDirectory() && dir.getName().toLowerCase().startsWith(dir_prefix)) {
108 VirtualFile python_exe = dir.findChild(exe_name);
109 if (python_exe != null) candidates.add(FileUtil.toSystemDependentName(python_exe.getPath()));