Fix conda activation on Windows (PY-21923)
authorDmitry Trofimov <dmitry.trofimov@jetbrains.com>
Wed, 14 Dec 2016 15:46:57 +0000 (16:46 +0100)
committerDmitry Trofimov <dmitry.trofimov@jetbrains.com>
Thu, 15 Dec 2016 13:34:42 +0000 (14:34 +0100)
python/src/com/jetbrains/python/run/PyVirtualEnvReader.kt

index 961577934eb045d3ef7c4878b1d93d697d46206c..7b412a228a4565de4cbff00c64b833184c4baeb0 100644 (file)
@@ -67,10 +67,10 @@ class PyVirtualEnvReader(val virtualEnvSdkPath: String) : EnvironmentUtil.ShellE
     val envFile = FileUtil.createTempFile("pycharm-virualenv-envs.", ".tmp", false)
     try {
       FileUtil.copy(File(activate.first), activateFile);
-      FileUtil.appendToFile(activateFile, "\n\nset")
+      FileUtil.appendToFile(activateFile, "\n\nset >" + envFile.absoluteFile)
 
-      val command = if (activate.second != null) listOf<String>(activateFile.path, activate.second!!, ">", envFile.absolutePath)
-      else listOf<String>(activateFile.path, ">", envFile.absolutePath)
+      val command = if (activate.second != null) listOf<String>(activateFile.path, activate.second!!)
+      else listOf<String>(activateFile.path)
 
       return runProcessAndReadEnvs(command, envFile, LineSeparator.CRLF.separatorString)
     }
@@ -99,14 +99,27 @@ class PyVirtualEnvReader(val virtualEnvSdkPath: String) : EnvironmentUtil.ShellE
 
 fun findActivateScript(path: String?, shellPath: String?): Pair<String, String?>? {
   val shellName = if (shellPath != null) File(shellPath).name else null
-  val activate = if (SystemInfo.isWindows) File(File(path).parentFile, "activate.bat")
+  val activate = if (SystemInfo.isWindows) findActivateOnWindows(path)
   else if (shellName == "fish" || shellName == "csh") File(File(path).parentFile, "activate." + shellName)
   else File(File(path).parentFile, "activate")
 
-  return if (activate.exists()) {
+  return if (activate != null && activate.exists()) {
     val sdk = PythonSdkType.findSdkByPath(path)
-    if (sdk != null && PythonSdkType.isCondaVirtualEnv(sdk)) Pair(activate.absolutePath, File(path).parentFile.parent)
+    if (sdk != null && PythonSdkType.isCondaVirtualEnv(sdk)) Pair(activate.absolutePath, condaEnvFolder(path))
     else Pair(activate.absolutePath, null)
   }
   else null
+}
+
+private fun condaEnvFolder(path: String?) = if (SystemInfo.isWindows) File(path).parent else File(path).parentFile.parent
+
+private fun findActivateOnWindows(path: String?): File? {
+  for (location in arrayListOf("activate.bat", "Scripts/activate.bat")) {
+    val file = File(File(path).parentFile, location)
+    if (file.exists()) {
+      return file
+    }
+  }
+
+  return null
 }
\ No newline at end of file