Cooja: Make paths more configurable

This commit is contained in:
Moritz 'Morty' Strübe 2012-10-30 11:41:58 +01:00
parent 50346251a2
commit 142fa4a9a5
2 changed files with 86 additions and 35 deletions

View file

@ -60,13 +60,16 @@ import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.io.PrintStream; import java.io.PrintStream;
import java.io.StringReader; import java.io.StringReader;
import java.lang.reflect.Array;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.net.URL; import java.net.URL;
import java.net.URLClassLoader; import java.net.URLClassLoader;
import java.security.AccessControlException; import java.security.AccessControlException;
import java.text.DecimalFormat; import java.text.DecimalFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.Comparator;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.List; import java.util.List;
import java.util.Observable; import java.util.Observable;
@ -244,7 +247,7 @@ public class GUI extends Observable {
public static Properties currentExternalToolsSettings; public static Properties currentExternalToolsSettings;
private static final String externalToolsSettingNames[] = new String[] { private static final String externalToolsSettingNames[] = new String[] {
"PATH_CONTIKI", "PATH_COOJA_CORE_RELATIVE", "PATH_CONTIKI", "PATH_COOJA_CORE_RELATIVE", "PATH_COOJA", "PATH_APPS",
"PATH_MAKE", "PATH_MAKE",
"PATH_SHELL", "PATH_SHELL",
@ -4122,23 +4125,44 @@ public class GUI extends Observable {
return file; return file;
} }
private final static String PATH_CONTIKI_IDENTIFIER = "[CONTIKI_DIR]"; private final static String[][] PATH_IDENTIFIER = {
{"[CONTIKI_DIR]","PATH_CONTIKI",""},
{"[COOJA_DIR]","PATH_COOJA","/tools/cooja"},
{"[APPS_DIR]","PATH_APPS","/tools/cooja/apps"}
};
private File createContikiRelativePath(File file) { private File createContikiRelativePath(File file) {
try { try {
File contikiPath = new File(GUI.getExternalToolsSetting("PATH_CONTIKI", null)); int elem = PATH_IDENTIFIER.length;
String contikiCanonical = contikiPath.getCanonicalPath(); File path[] = new File [elem];
String canonicals[] = new String[elem];
int match = -1;
int mlength = 0;
String fileCanonical = file.getCanonicalPath(); String fileCanonical = file.getCanonicalPath();
if (!fileCanonical.startsWith(contikiCanonical)) {
/* File is not in a Contiki subdirectory */ //No so nice, but goes along with GUI.getExternalToolsSetting
/*logger.info("File is not in a Contiki subdirectory: " + file.getAbsolutePath());*/ String defp = GUI.getExternalToolsSetting("PATH_CONTIKI", null);
return null;
for(int i = 0; i < elem; i++){
path[i] = new File(GUI.getExternalToolsSetting(PATH_IDENTIFIER[i][1], defp + PATH_IDENTIFIER[i][2]));
canonicals[i] = path[i].getCanonicalPath();
if (fileCanonical.startsWith(canonicals[i])){
if(mlength < canonicals[i].length()){
mlength = canonicals[i].length();
match = i;
} }
}
}
if(match == -1) return null;
/* Replace Contiki's canonical path with Contiki identifier */ /* Replace Contiki's canonical path with Contiki identifier */
String portablePath = fileCanonical.replaceFirst( String portablePath = fileCanonical.replaceFirst(
java.util.regex.Matcher.quoteReplacement(contikiCanonical), java.util.regex.Matcher.quoteReplacement(canonicals[match]),
java.util.regex.Matcher.quoteReplacement(PATH_CONTIKI_IDENTIFIER)); java.util.regex.Matcher.quoteReplacement(PATH_IDENTIFIER[match][0]));
File portable = new File(portablePath); File portable = new File(portablePath);
/* Verify conversion */ /* Verify conversion */
@ -4154,17 +4178,43 @@ public class GUI extends Observable {
return null; return null;
} }
} }
private File restoreContikiRelativePath(File portable) { private File restoreContikiRelativePath(File portable) {
int elem = PATH_IDENTIFIER.length;
File path = null;
String canonical = null;
try { try {
File contikiPath = new File(GUI.getExternalToolsSetting("PATH_CONTIKI", null));
String contikiCanonical = contikiPath.getCanonicalPath();
String portablePath = portable.getPath(); String portablePath = portable.getPath();
if (!portablePath.startsWith(PATH_CONTIKI_IDENTIFIER)) {
return null; int i = 0;
//logger.info("PPATH: " + portablePath);
for(; i < elem; i++){
if (portablePath.startsWith(PATH_IDENTIFIER[i][0])) break;
} }
File absolute = new File(portablePath.replace(PATH_CONTIKI_IDENTIFIER, contikiCanonical));
if(i == elem) return null;
//logger.info("Found: " + PATH_IDENTIFIER[i][0]);
//No so nice, but goes along with GUI.getExternalToolsSetting
String defp = GUI.getExternalToolsSetting("PATH_CONTIKI", null);
path = new File(GUI.getExternalToolsSetting(PATH_IDENTIFIER[i][1], defp + PATH_IDENTIFIER[i][2]));
//logger.info("Config: " + PATH_IDENTIFIER[i][1] + ", " + defp + PATH_IDENTIFIER[i][2] + " = " + path.toString());
canonical = path.getCanonicalPath();
File absolute = new File(portablePath.replace(PATH_IDENTIFIER[i][0], canonical));
if(!absolute.exists()){
logger.warn("Replaced " + portable + " with " + absolute.toString() + " (default: "+ defp + PATH_IDENTIFIER[i][2] +"), but could not find it. This does not have to be an error, as the file might be created later.");
}
return absolute; return absolute;
} catch (IOException e) { } catch (IOException e) {
return null; return null;
@ -4623,3 +4673,4 @@ public class GUI extends Observable {
}; };
} }

View file

@ -237,7 +237,7 @@ public class ProjectConfig {
public boolean appendConfigFile(File propertyFile) public boolean appendConfigFile(File propertyFile)
throws FileNotFoundException, IOException { throws FileNotFoundException, IOException {
if (!propertyFile.exists()) { if (!propertyFile.exists()) {
logger.warn("Trying to import non-existant project configuration"); logger.warn("Trying to import non-existant project configuration: " + propertyFile.toString());
return true; return true;
} }