|
| 1 | +package org.variantsync.vevos.extraction; |
| 2 | + |
| 3 | +import org.tinylog.Logger; |
| 4 | +import org.variantsync.diffdetective.AnalysisRunner; |
| 5 | +import org.variantsync.diffdetective.datasets.PatchDiffParseOptions; |
| 6 | +import org.variantsync.diffdetective.diff.git.DiffFilter; |
| 7 | +import org.variantsync.diffdetective.variation.diff.parse.VariationDiffParseOptions; |
| 8 | +import org.variantsync.vevos.extraction.gt.GroundTruth; |
| 9 | + |
| 10 | +import java.io.File; |
| 11 | +import java.io.FileInputStream; |
| 12 | +import java.io.IOException; |
| 13 | +import java.nio.file.Path; |
| 14 | +import java.util.Properties; |
| 15 | + |
| 16 | +import static org.variantsync.vevos.extraction.ConfigProperties.*; |
| 17 | + |
| 18 | +public class ExecutionUtilities { |
| 19 | + |
| 20 | + /** |
| 21 | + * Loads the properties in the given file. |
| 22 | + * |
| 23 | + * @param propertiesFile The file to load |
| 24 | + * @return The loaded properties |
| 25 | + */ |
| 26 | + public static Properties getProperties(File propertiesFile) { |
| 27 | + Properties props = new Properties(); |
| 28 | + try (FileInputStream input = new FileInputStream(propertiesFile)) { |
| 29 | + props.load(input); |
| 30 | + } catch (IOException e) { |
| 31 | + Logger.error("problem while loading properties"); |
| 32 | + Logger.error(e); |
| 33 | + quitOnError(); |
| 34 | + } |
| 35 | + return props; |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Options for the execution of DiffDetective |
| 40 | + * |
| 41 | + * @param properties The properties loaded by main() |
| 42 | + * @return The options instance |
| 43 | + */ |
| 44 | + public static AnalysisRunner.Options diffdetectiveOptions(Properties properties) { |
| 45 | + |
| 46 | + return new AnalysisRunner.Options( |
| 47 | + Path.of(properties.getProperty(REPO_SAVE_DIR)), |
| 48 | + Path.of(properties.getProperty(DD_OUTPUT_DIR)), |
| 49 | + Path.of(properties.getProperty(DATASET_FILE)), |
| 50 | + repo -> { |
| 51 | + final PatchDiffParseOptions repoDefault = repo.getParseOptions(); |
| 52 | + return new PatchDiffParseOptions( |
| 53 | + PatchDiffParseOptions.DiffStoragePolicy.DO_NOT_REMEMBER, |
| 54 | + new VariationDiffParseOptions( |
| 55 | + repoDefault.variationDiffParseOptions().annotationParser(), |
| 56 | + false, |
| 57 | + false |
| 58 | + ) |
| 59 | + ); |
| 60 | + }, |
| 61 | + repo -> new DiffFilter.Builder() |
| 62 | + .allowMerge(true) |
| 63 | + // TODO: make configurable |
| 64 | + .allowedFileExtensions("h", "hpp", "c", "cpp") |
| 65 | + .build(), |
| 66 | + true, |
| 67 | + false |
| 68 | + ); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Throws an error if the host OS is Windows. |
| 73 | + */ |
| 74 | + public static void checkOS() { |
| 75 | + boolean isWindows = System.getProperty("os.name") |
| 76 | + .toLowerCase().startsWith("windows"); |
| 77 | + Logger.info("OS NAME: " + System.getProperty("os.name")); |
| 78 | + if (isWindows) { |
| 79 | + Logger.error("Running the analysis under Windows is not supported as the Linux/BusyBox sources are not" + |
| 80 | + "checked out correctly."); |
| 81 | + quitOnError(); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Logs an error message and quits the extraction with an exception. |
| 87 | + */ |
| 88 | + public static void quitOnError() { |
| 89 | + Logger.error("An error occurred and the program has to quit."); |
| 90 | + throw new IllegalStateException("Not able to continue analysis due to previous error"); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Parses the file in which the properties are located from the arguments. |
| 95 | + * |
| 96 | + * @param args the arguments to parse |
| 97 | + * @return the properties file |
| 98 | + */ |
| 99 | + public static File getPropertiesFile(String[] args) { |
| 100 | + File propertiesFile = null; |
| 101 | + if (args.length > 0) { |
| 102 | + propertiesFile = new File(args[0]); |
| 103 | + } |
| 104 | + |
| 105 | + if (propertiesFile == null) { |
| 106 | + Logger.error("You must specify a .properties file as first argument"); |
| 107 | + quitOnError(); |
| 108 | + } |
| 109 | + |
| 110 | + return propertiesFile; |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Prints the given ground truth to console. |
| 115 | + * |
| 116 | + * @param groundTruth GT to print |
| 117 | + * @param commitName The id of the commit for which the GT has been calculated |
| 118 | + */ |
| 119 | + public static void print(GroundTruth groundTruth, String commitName) { |
| 120 | + System.out.println(); |
| 121 | + System.out.printf("***************** %s ******************", commitName); |
| 122 | + System.out.println(); |
| 123 | + for (String file : groundTruth.fileGTs().keySet()) { |
| 124 | + System.out.println(groundTruth.get(file)); |
| 125 | + } |
| 126 | + } |
| 127 | +} |
0 commit comments