|
40 | 40 | */
|
41 | 41 | package com.oracle.graal.python.resources;
|
42 | 42 |
|
| 43 | +import java.io.File; |
43 | 44 | import java.io.IOException;
|
44 | 45 | import java.io.InputStream;
|
45 | 46 | import java.nio.file.InvalidPathException;
|
46 | 47 | import java.nio.file.Path;
|
| 48 | +import java.util.ArrayList; |
| 49 | +import java.util.Collections; |
47 | 50 | import java.util.List;
|
| 51 | +import java.util.function.Predicate; |
| 52 | +import java.util.regex.Pattern; |
48 | 53 |
|
49 | 54 | import com.oracle.truffle.api.CompilerDirectives;
|
50 | 55 | import com.oracle.truffle.api.InternalResource;
|
@@ -100,20 +105,94 @@ public final class PythonResource implements InternalResource {
|
100 | 105 | public void unpackFiles(Env env, Path targetDirectory) throws IOException {
|
101 | 106 | OS os = env.getOS();
|
102 | 107 | Path osArch = Path.of(os.toString()).resolve(env.getCPUArchitecture().toString());
|
| 108 | + ResourcesFilter filter = new ResourcesFilter(); |
103 | 109 | if (os.equals(OS.WINDOWS)) {
|
104 |
| - env.unpackResourceFiles(BASE_PATH.resolve(LIBPYTHON_FILES), targetDirectory.resolve("Lib"), BASE_PATH.resolve(LIBPYTHON)); |
105 |
| - env.unpackResourceFiles(BASE_PATH.resolve(LIBGRAALPY_FILES), targetDirectory.resolve("lib-graalpython"), BASE_PATH.resolve(LIBGRAALPY)); |
106 |
| - env.unpackResourceFiles(BASE_PATH.resolve(INCLUDE_FILES), targetDirectory.resolve("Include"), BASE_PATH.resolve(INCLUDE)); |
| 110 | + env.unpackResourceFiles(BASE_PATH.resolve(LIBPYTHON_FILES), targetDirectory.resolve("Lib"), BASE_PATH.resolve(LIBPYTHON), filter); |
| 111 | + env.unpackResourceFiles(BASE_PATH.resolve(LIBGRAALPY_FILES), targetDirectory.resolve("lib-graalpython"), BASE_PATH.resolve(LIBGRAALPY), filter); |
| 112 | + env.unpackResourceFiles(BASE_PATH.resolve(INCLUDE_FILES), targetDirectory.resolve("Include"), BASE_PATH.resolve(INCLUDE), filter); |
107 | 113 | } else {
|
108 | 114 | String pythonMajMin = "python" + PYTHON_MAJOR + "." + PYTHON_MINOR;
|
109 |
| - env.unpackResourceFiles(BASE_PATH.resolve(LIBPYTHON_FILES), targetDirectory.resolve("lib").resolve(pythonMajMin), BASE_PATH.resolve(LIBPYTHON)); |
110 |
| - env.unpackResourceFiles(BASE_PATH.resolve(LIBGRAALPY_FILES), targetDirectory.resolve("lib").resolve("graalpy" + GRAALVM_MAJOR + "." + GRAALVM_MINOR), BASE_PATH.resolve(LIBGRAALPY)); |
111 |
| - env.unpackResourceFiles(BASE_PATH.resolve(INCLUDE_FILES), targetDirectory.resolve("include").resolve(pythonMajMin), BASE_PATH.resolve(INCLUDE)); |
| 115 | + env.unpackResourceFiles(BASE_PATH.resolve(LIBPYTHON_FILES), targetDirectory.resolve("lib").resolve(pythonMajMin), BASE_PATH.resolve(LIBPYTHON), filter); |
| 116 | + env.unpackResourceFiles(BASE_PATH.resolve(LIBGRAALPY_FILES), targetDirectory.resolve("lib").resolve("graalpy" + GRAALVM_MAJOR + "." + GRAALVM_MINOR), BASE_PATH.resolve(LIBGRAALPY), |
| 117 | + filter); |
| 118 | + env.unpackResourceFiles(BASE_PATH.resolve(INCLUDE_FILES), targetDirectory.resolve("include").resolve(pythonMajMin), BASE_PATH.resolve(INCLUDE), filter); |
112 | 119 | }
|
113 | 120 | // ni files are in the same place on all platforms
|
114 |
| - env.unpackResourceFiles(BASE_PATH.resolve(NI_FILES), targetDirectory, BASE_PATH); |
| 121 | + env.unpackResourceFiles(BASE_PATH.resolve(NI_FILES), targetDirectory, BASE_PATH, filter); |
115 | 122 | // native files already have the correct structure
|
116 |
| - env.unpackResourceFiles(BASE_PATH.resolve(osArch).resolve(NATIVE_FILES), targetDirectory, BASE_PATH.resolve(osArch)); |
| 123 | + env.unpackResourceFiles(BASE_PATH.resolve(osArch).resolve(NATIVE_FILES), targetDirectory, BASE_PATH.resolve(osArch), filter); |
| 124 | + |
| 125 | + if (filter.log) { |
| 126 | + System.out.println("unpacked python resources:"); |
| 127 | + System.out.println("include pattern: '" + filter.include + "'"); |
| 128 | + System.out.println("exclude pattern: '" + filter.exclude + "'"); |
| 129 | + listFiles("included files:", filter.included); |
| 130 | + listFiles("excluded files:", filter.excluded); |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + private static void listFiles(String header, List<String> l) { |
| 135 | + if (!l.isEmpty()) { |
| 136 | + Collections.sort(l); |
| 137 | + System.out.println(header); |
| 138 | + for (String s : l) { |
| 139 | + System.out.println("\t" + s); |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + private static class ResourcesFilter implements Predicate<Path> { |
| 145 | + |
| 146 | + private final boolean log; |
| 147 | + private final Pattern includePattern; |
| 148 | + private final Pattern excludePattern; |
| 149 | + private final List<String> excluded; |
| 150 | + private final String exclude; |
| 151 | + private final String include; |
| 152 | + private final ArrayList<String> included; |
| 153 | + |
| 154 | + private ResourcesFilter() { |
| 155 | + include = getProperty("org.graalvm.python.resources.include"); |
| 156 | + exclude = getProperty("org.graalvm.python.resources.exclude"); |
| 157 | + log = Boolean.parseBoolean(getProperty("org.graalvm.python.resources.log")); |
| 158 | + includePattern = include != null ? Pattern.compile(include) : null; |
| 159 | + excludePattern = exclude != null ? Pattern.compile(exclude) : null; |
| 160 | + included = log ? new ArrayList<>() : null; |
| 161 | + excluded = log ? new ArrayList<>() : null; |
| 162 | + } |
| 163 | + |
| 164 | + @Override |
| 165 | + public boolean test(Path path) { |
| 166 | + String absolutePath = path.toAbsolutePath().toString(); |
| 167 | + if (File.separator.equals("\\")) { |
| 168 | + absolutePath = absolutePath.replaceAll("\\\\", "/"); |
| 169 | + } |
| 170 | + if ((includePattern != null && !includePattern.matcher(absolutePath).matches()) || |
| 171 | + (excludePattern != null && excludePattern.matcher(absolutePath).matches())) { |
| 172 | + if (log) { |
| 173 | + excluded.add(absolutePath); |
| 174 | + } |
| 175 | + return false; |
| 176 | + } else { |
| 177 | + if (log) { |
| 178 | + included.add(absolutePath); |
| 179 | + } |
| 180 | + return true; |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + private static String getProperty(String prop) { |
| 185 | + String s = System.getProperty(prop); |
| 186 | + if (s != null) { |
| 187 | + if (s.isEmpty()) { |
| 188 | + s = null; |
| 189 | + } else if (s.startsWith("\"") && s.endsWith("\"")) { |
| 190 | + // native-gradle-plugin sends system properties wrapped in " |
| 191 | + s = s.substring(1, s.length() - 1); |
| 192 | + } |
| 193 | + } |
| 194 | + return s; |
| 195 | + } |
117 | 196 | }
|
118 | 197 |
|
119 | 198 | @Override
|
|
0 commit comments