forked from consulo/consulo-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPyVirtualEnvReader.java
More file actions
121 lines (105 loc) · 4 KB
/
PyVirtualEnvReader.java
File metadata and controls
121 lines (105 loc) · 4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
* Copyright 2013-2016 must-be.org
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.jetbrains.python.impl.run;
import consulo.application.util.SystemInfo;
import consulo.logging.Logger;
import consulo.process.local.EnvironmentUtil;
import consulo.util.io.FilePermissionCopier;
import consulo.util.io.FileUtil;
import org.jspecify.annotations.Nullable;
import java.io.File;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
public class PyVirtualEnvReader extends EnvironmentUtil.ShellEnvReader {
private static final Logger LOG = Logger.getInstance(PyVirtualEnvReader.class);
private String activate;
public PyVirtualEnvReader(String virtualEnvSdkPath) {
try {
activate = findActivateScript(virtualEnvSdkPath, getShell());
}
catch (Exception e) {
throw new RuntimeException(e);
}
}
private static String findActivateScript(String path, String shellPath) {
String shellName = shellPath != null ? new File(shellPath).getName() : null;
File activate;
if (SystemInfo.isWindows) {
activate = new File(new File(path).getParentFile(), "activate.bat");
}
else {
activate = ("fish".equals(shellName) || "csh".equals(shellName)) ? new File(new File(path).getParentFile(),
"activate." + shellName) : new File(new File(path).getParentFile(),
"activate");
}
return activate.exists() ? activate.getAbsolutePath() : null;
}
public String getActivate() {
return activate;
}
@Nullable
@Override
protected String getShell() {
if (new File("/bin/bash").exists()) {
return "/bin/bash";
}
else if (new File("/bin/sh").exists()) {
return "/bin/sh";
}
else {
return super.getShell();
}
}
@Override
protected List<String> getShellProcessCommand() {
String shellPath = getShell();
if (shellPath == null || !new File(shellPath).canExecute()) {
throw new IllegalArgumentException("shell:" + shellPath);
}
return activate != null ? Arrays.asList(shellPath, "-c", ". '$activate'") : super.getShellProcessCommand();
}
public Map<String, String> readPythonEnv() throws Exception {
if (SystemInfo.isUnix) {
// pass shell environment for correct virtualenv environment setup (virtualenv expects to be executed from the terminal)
return super.readShellEnv(null, EnvironmentUtil.getEnvironmentMap());
}
else {
if (activate != null) {
return readVirtualEnvOnWindows(activate);
}
else {
LOG.error("Can't find activate script for $virtualEnvSdkPath");
return Collections.emptyMap();
}
}
}
private Map<String, String> readVirtualEnvOnWindows(String activate) throws Exception {
File activateFile = FileUtil.createTempFile("pycharm-virualenv-activate.", ".bat", false);
File envFile = FileUtil.createTempFile("pycharm-virualenv-envs.", ".tmp", false);
try {
FileUtil.copy(new File(activate), activateFile, FilePermissionCopier.BY_NIO2);
FileUtil.appendToFile(activateFile, "\n\nset");
List<String> command = Arrays.asList(activateFile.getPath(), ">", envFile.getAbsolutePath());
return runProcessAndReadOutputAndEnvs(command, null, envFile.toPath()).getValue();
}
finally {
FileUtil.delete(activateFile);
FileUtil.delete(envFile);
}
}
}