-
Notifications
You must be signed in to change notification settings - Fork 434
/
PythonEnvironmentConfig.java
145 lines (123 loc) · 4.36 KB
/
PythonEnvironmentConfig.java
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package org.grobid.core.jni;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.stream.Collectors;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.grobid.core.exceptions.GrobidException;
import org.grobid.core.exceptions.GrobidResourceException;
import org.grobid.core.utilities.GrobidProperties;
public class PythonEnvironmentConfig {
private Path virtualEnv;
private Path sitePackagesPath;
private Path jepPath;
private boolean active;
private String pythonVersion;
public PythonEnvironmentConfig(
Path virtualEnv,
Path sitePackagesPath,
Path jepPath,
String pythonVersion,
boolean active) {
this.virtualEnv = virtualEnv;
this.sitePackagesPath = sitePackagesPath;
this.jepPath = jepPath;
this.active = active;
this.pythonVersion = pythonVersion;
}
public boolean isEmpty() {
return this.virtualEnv == null;
}
public Path getVirtualEnv() {
return this.virtualEnv;
}
public Path getSitePackagesPath() {
return this.sitePackagesPath;
}
public Path getNativeLibPath() {
if (this.virtualEnv == null) {
return null;
}
return Paths.get(this.virtualEnv.toString(), "lib");
}
public Path[] getNativeLibPaths() {
if (this.virtualEnv == null) {
return new Path[0];
}
return new Path[]{
this.getNativeLibPath(),
this.getJepPath()
};
}
public Path getJepPath() {
return this.jepPath;
}
public boolean isActive() {
return this.active;
}
public static PythonEnvironmentConfig getInstanceForVirtualEnv(String virtualEnv, String activeVirtualEnv)
throws GrobidResourceException {
if (StringUtils.isEmpty(virtualEnv) && StringUtils.isEmpty(activeVirtualEnv)) {
return new PythonEnvironmentConfig(null, null, null, null, false);
}
if (StringUtils.isEmpty(virtualEnv)) {
virtualEnv = activeVirtualEnv;
}
List<Path> pythons;
try {
pythons = Files.find(
Paths.get(virtualEnv, "lib"),
1,
(path, attr) -> (
path.toFile().isDirectory()
&& path.getFileName().toString().contains("python3")
)
).collect(Collectors.toList());
} catch (IOException e) {
throw new GrobidResourceException("failed to get python versions from virtual environment", e);
}
List<String> pythonVersions = pythons
.stream()
.map(path -> FilenameUtils.getName(path.getFileName().toString())
.replace("libpython", "").replace("python", ""))
.filter(version -> version.contains("3.7") || version.contains("3.8") || version.contains("3.9"))
.distinct()
.sorted()
.collect(Collectors.toList());
if (CollectionUtils.isEmpty(pythonVersions)) {
throw new GrobidException(
"Cannot find a suitable version (3.7, 3.8 or 3.9) of python in your virtual environment: " +
virtualEnv
);
}
Path sitePackagesPath = Paths.get(pythons.get(0).toString(), "site-packages");
Path jepPath = Paths.get(sitePackagesPath.toString(), "jep");
return new PythonEnvironmentConfig(
Paths.get(virtualEnv),
sitePackagesPath,
jepPath,
pythonVersions.get(0),
StringUtils.equals(virtualEnv, activeVirtualEnv)
);
}
public static String getActiveVirtualEnv() {
String activeVirtualEnv = System.getenv("VIRTUAL_ENV");
if (StringUtils.isEmpty(activeVirtualEnv)) {
activeVirtualEnv = System.getenv("CONDA_PREFIX");
}
return activeVirtualEnv;
}
public static PythonEnvironmentConfig getInstance() throws GrobidResourceException {
return getInstanceForVirtualEnv(
GrobidProperties.getPythonVirtualEnv(),
getActiveVirtualEnv()
);
}
public String getPythonVersion() {
return pythonVersion;
}
}