Skip to content

Commit 7928a85

Browse files
tomasstupkatimfel
authored andcommitted
list also all parent directories in __graalpython__.list_files
(cherry picked from commit 62f1771)
1 parent c2442e1 commit 7928a85

File tree

2 files changed

+42
-23
lines changed

2 files changed

+42
-23
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/GraalPythonModuleBuiltins.java

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,9 @@
7878
import java.util.ArrayList;
7979
import java.util.Arrays;
8080
import java.util.Collection;
81+
import java.util.HashSet;
8182
import java.util.List;
83+
import java.util.Set;
8284
import java.util.logging.Level;
8385

8486
import org.graalvm.nativeimage.ImageInfo;
@@ -1021,15 +1023,19 @@ Object list(TruffleString dirPath, TruffleString filesListPath) {
10211023
}
10221024

10231025
try (BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filesListPath.toJavaStringUncached())))) {
1024-
getContext().getPublicTruffleFileRelaxed(filesListPath).getParent().createDirectories();
1025-
List<String> ret = list(dir);
1026-
String parentPathString = dir.getParent().getAbsoluteFile().getPath();
1027-
for (String f : ret) {
1028-
String tt = f.substring(parentPathString.length());
1029-
if (tt.charAt(0) == '\\') {
1030-
tt = tt.replace("\\", "/");
1026+
TruffleFile p = getContext().getPublicTruffleFileRelaxed(filesListPath).getParent();
1027+
if(!p.exists()) {
1028+
getContext().getPublicTruffleFileRelaxed(filesListPath).getParent().createDirectories();
1029+
}
1030+
Set<String> ret = list(dir, null);
1031+
String[] a = ret.toArray(new String[ret.size()]);
1032+
Arrays.sort(a);
1033+
for (String f : a) {
1034+
if (f.charAt(0) == '\\') {
1035+
f = f.replace("\\", "/");
10311036
}
1032-
bw.write(tt);
1037+
1038+
bw.write(f);
10331039
bw.write("\n");
10341040
}
10351041
} catch (IOException e) {
@@ -1039,26 +1045,46 @@ Object list(TruffleString dirPath, TruffleString filesListPath) {
10391045
return PNone.NONE;
10401046
}
10411047

1042-
private static List<String> list(TruffleFile dir) throws IOException {
1043-
List<String> ret = new ArrayList<>();
1048+
private static Set<String> list(TruffleFile dir, TruffleFile rd) throws IOException {
1049+
HashSet<String> ret = new HashSet<>();
10441050
Collection<TruffleFile> files = dir.list();
1045-
String dirPath = dir.getAbsoluteFile().getPath();
1046-
if (!dirPath.endsWith("/")) {
1047-
dirPath = dirPath + "/";
1051+
String dirPath = makeDirPath(dir.getAbsoluteFile().getPath());
1052+
1053+
// add dir
1054+
TruffleFile rootDir = rd == null ? dir : rd;
1055+
String rootPath = makeDirPath(rootDir.getAbsoluteFile().getPath());
1056+
int rootEndIdx = rootPath.lastIndexOf("/", rootPath.lastIndexOf("/") - 1);
1057+
ret.add(dirPath.substring(rootEndIdx));
1058+
1059+
// add parents up to root
1060+
TruffleFile parent = dir;
1061+
while(!parent.equals(rootDir)) {
1062+
String p = makeDirPath(parent.getAbsoluteFile().getPath());
1063+
p = p.substring(rootEndIdx);
1064+
ret.add(p);
1065+
parent = parent.getParent();
10481066
}
1049-
ret.add(dirPath);
1067+
1068+
// add children
10501069
if (files != null) {
10511070
for (TruffleFile f : files) {
10521071
if (f.isRegularFile()) {
1053-
ret.add(f.getAbsoluteFile().getPath());
1072+
ret.add(f.getAbsoluteFile().getPath().substring(rootEndIdx));
10541073
} else {
1055-
ret.addAll(list(f));
1074+
ret.addAll(list(f, rootDir));
10561075
}
10571076
}
10581077
}
10591078
return ret;
10601079
}
10611080

1081+
private static String makeDirPath(String p) {
1082+
if (!p.endsWith("/")) {
1083+
p = p + "/";
1084+
}
1085+
return p;
1086+
}
1087+
10621088
private void print(OutputStream out, String msg) {
10631089
try {
10641090
out.write(String.format("%s: %s", getContext().getOption(PythonOptions.Executable), msg).getBytes(StandardCharsets.UTF_8));

graalpython/lib-graalpython/modules/standalone/templates/VirtualFileSystem.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -200,13 +200,6 @@ private static void initFilesAndDirsList() throws IOException {
200200
if(line.endsWith(RESOURCE_SEPARATOR)) {
201201
line = line.substring(0, line.length() - 1);
202202
dirsList.add(line);
203-
int idx;
204-
while((idx = line.lastIndexOf(RESOURCE_SEPARATOR)) != -1) {
205-
line = line.substring(0, idx);
206-
if(!line.isEmpty()) {
207-
dirsList.add(line);
208-
}
209-
}
210203
} else {
211204
filesList.add(line);
212205
}

0 commit comments

Comments
 (0)