-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDetectClones.java
291 lines (251 loc) · 11.3 KB
/
DetectClones.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
package tasks;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Writer;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.Callable;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.TrueFileFilter;
import org.apache.commons.lang3.SystemUtils;
import picocli.CommandLine;
import picocli.CommandLine.Mixin;
import picocli.CommandLine.Option;
import tasks.MixinOptions.*;
import util.FixPath;
import util.StreamGobbler;
@CommandLine.Command(
name = "detectClones",
description = "Executes the clone detection tool for IJaDataset in an automated procedure. " +
"Requires a script that configures and executes the tool, " +
"and the scalability limits of the tool in terms of the maximum input size measured in source files. " +
"Used deterministic input partitioning to overcome scalability limits. " +
"Optional, clone detection can be performed manually if desired.",
mixinStandardHelpOptions = true,
versionProvider = util.Version.class)
public class DetectClones implements Callable<Void> {
@CommandLine.Spec
private CommandLine.Model.CommandSpec spec;
@Mixin
private OutputFile outputFile;
@Mixin
private MaxFiles maxFiles;
@Option(
names = {"-n", "--nc", "--no-clean"},
description = "Does not clean up scratch data. For diagnosis/correction. See documentation. Need to specify custom scratch directory for this."
)
private boolean noClean;
private File toolRunner;
private File scratchDirectory = null;
private boolean fullclean = true;
@Option(
names = {"-r", "--tr", "--tool-runner"},
description = "Path to the tool runner executable.",
required = true,
paramLabel = "<PATH>"
)
private void setToolRunner(File toolRunner) {
toolRunner = FixPath.getAbsolutePath(toolRunner.toPath()).toFile();
if (!toolRunner.exists()) {
throw new CommandLine.ParameterException(spec.commandLine(), "Tool runner does not exist.");
} else if (!Files.isExecutable(toolRunner.toPath())) {
throw new CommandLine.ParameterException(spec.commandLine(), "Tool runner is not executable.");
}
this.toolRunner = toolRunner;
}
@Option(
names = {"-s", "--sd", "--scratch-directory"},
description = "Directory to be used as scratch space. Default is system tmp directory. Can not already exist.",
paramLabel = "<PATH>"
)
private void setScratchDirectory(File scratchDirectory) {
if (scratchDirectory.exists()) {
throw new CommandLine.ParameterException(spec.commandLine(), "Scratch directory already exists. Must specify a new one (to protect against accidental data loss).");
}
this.scratchDirectory = scratchDirectory;
this.fullclean = false;
}
private void panic() {
new CommandLine(this).usage(System.err);
System.exit(-1);
}
public static void main(String[] args) {
new CommandLine(new DetectClones()).execute(args);
}
public Void call() {
Path dataset = Paths.get("ijadataset/bcb_reduced/");
// Setup output
try {
outputFile.file.delete();
outputFile.file.getParentFile().mkdirs();
outputFile.file.createNewFile();
} catch (Exception e) {
System.err.println("Failed to create output file.");
e.printStackTrace(System.err);
panic();
return null;
}
// Setup Scratch Directory
try {
if (scratchDirectory != null) {
scratchDirectory.mkdirs();
} else {
scratchDirectory = Files.createTempDirectory("DetectClones").toFile();
}
} catch (IOException e) {
System.err.println("Failed to create scratch directory.");
panic();
return null;
}
try {
DetectClones.detect(outputFile.file.toPath(), dataset, toolRunner.toPath(), scratchDirectory.toPath(), maxFiles.maxFiles, !noClean);
} catch (IOException e1) {
System.err.println("An exeception occured during detection:");
e1.printStackTrace(System.err);
panic();
return null;
}
// Cleanup
if (fullclean) {
try {
FileUtils.deleteDirectory(scratchDirectory);
} catch (IOException e) {
System.err.println("Failed to delete scratch (temporary) directory, please do so manually: " + scratchDirectory.toString());
}
}
return null;
}
public static void detect(Path output, Path dataset, Path tool, Path scratchdir, int maxFiles, boolean cleanup) throws IOException {
output = output.toAbsolutePath();
dataset = dataset.toAbsolutePath();
tool = tool.toAbsolutePath();
scratchdir = scratchdir.toAbsolutePath();
// Open Output for Writing
BufferedWriter writer = new BufferedWriter(new FileWriter(output.toFile()));
// Build list of inputs
List<Path> inputs = new ArrayList<Path>();
for (File file : dataset.toFile().listFiles(File::isDirectory)) {//FileUtils.listFilesAndDirs(dataset.toFile(), DirectoryFileFilter.INSTANCE, null)) {
inputs.add(file.toPath());
}
Collections.sort(inputs);
Collections.reverse(inputs);
// Run Detection
for (Path input : inputs) {
System.out.println("Detecting clones in: " + input.toAbsolutePath());
if (FileUtils.listFiles(input.toFile(), TrueFileFilter.INSTANCE, TrueFileFilter.INSTANCE).size() > maxFiles) {
detect(tool, input, writer, scratchdir, maxFiles, cleanup);
} else {
try {
int retval = detect(tool, input, writer);
if (0 != retval) {
System.err.println("Execution for input: " + input + " had a non-zero return value: " + retval + ".");
}
} catch (InterruptedException | IOException e) {
System.err.println("Execution for input: " + input + " failed with exception: ");
e.printStackTrace(System.err);
}
}
}
// Close Output
writer.flush();
writer.close();
}
public static int detect(Path tool, Path input, Writer out) throws IOException, InterruptedException {
ProcessBuilder pb;
if (SystemUtils.IS_OS_WINDOWS) {
String[] exec = {"cmd.exe", "/c", tool.toString() + " " + input.toString()};
pb = new ProcessBuilder(exec);
} else {
String[] exec = {"bash", "-c", "\"" + tool.toString() + "\" \"" + input.toString() + "\""};
pb = new ProcessBuilder(exec);
}
Process p = pb.start();
new StreamGobbler(p.getErrorStream()).start();
String line = null;
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((line = br.readLine()) != null) {
line = line.trim();
if (!line.equals("")) {
out.write(line + "\n");
}
}
br.close();
int retval = p.waitFor();
return retval;
}
public static void detect(Path tool, Path input, Writer out, Path scratchdir, int maxFiles, boolean cleanup) throws IOException {
// Partition
Path tmpdir = Files.createDirectories(scratchdir.resolve(input.getFileName().toString() + "_partition"));
DetectClones.partition(input, tmpdir, maxFiles);
// Run for each partition
for (File partition : tmpdir.toFile().listFiles()) {
System.out.println("\tExecuting for partition: " + partition.getAbsolutePath());
try {
int retval = detect(tool, partition.toPath(), out);
if (0 != retval) {
System.err.println("Execution for input: " + input + " partition: " + partition.getName() + " had non-zero return value.");
}
} catch (Exception e) {
System.err.println("Execution for input: " + input + " partition: " + partition.getName() + " failed with exception:");
e.printStackTrace(System.err);
}
}
// Cleanup
if (cleanup) {
try {
FileUtils.deleteDirectory(tmpdir.toFile());
} catch (Exception e) {
System.err.println("Failed to delete a temporary directory, please do so manually: " + tmpdir.toAbsolutePath());
}
}
}
public static void partition(Path dir, Path split, int maxfiles) throws IOException {
// Get and Shuffle the files
List<File> files = new ArrayList<File>(FileUtils.listFiles(dir.toFile(), TrueFileFilter.INSTANCE, TrueFileFilter.INSTANCE));
Collections.shuffle(files);
// halve the maxfiles to get the max size of each partition
maxfiles = Math.max((int) Math.ceil(1.0 * maxfiles / 2), 1);
// Number of partitions
int numpartitions = (int) Math.ceil(1.0 * files.size() / maxfiles);
// Split files into the partitions
List<List<File>> partitions = new ArrayList<List<File>>(numpartitions);
int count = 0;
for (int i = 0; i < numpartitions; i++) {
partitions.add(new ArrayList<File>(maxfiles));
List<File> partition = partitions.get(i);
for (int j = 0; j < maxfiles && count < files.size(); j++) {
partition.add(files.get(count));
count++;
}
}
// Build each pair of partitions on disk, considering symmetry
for (int i = 0; i < partitions.size(); i++) {
List<File> partition1 = partitions.get(i);
for (int j = i + 1; j < partitions.size(); j++) {
List<File> partition2 = partitions.get(j);
Path idir = split.resolve(i + "-" + j);
Files.createDirectories(idir);
for (int k = 0; k < partition1.size(); k++) {
Path ofile = partition1.get(k).getCanonicalFile().toPath();
Path nfile = idir.resolve(dir.relativize(ofile));
Files.createDirectories(nfile.getParent());
Files.copy(ofile, nfile);
}
for (int k = 0; k < partition2.size(); k++) {
Path ofile = partition2.get(k).getCanonicalFile().toPath();
Path nfile = idir.resolve(dir.relativize(ofile));
Files.createDirectories(nfile.getParent());
Files.copy(ofile, nfile);
}
}
}
}
}