[Swift-commit] r7471 - in trunk/src/org/griphyn/vdl: engine karajan

hategan at ci.uchicago.edu hategan at ci.uchicago.edu
Fri Jan 10 16:07:40 CST 2014


Author: hategan
Date: 2014-01-10 16:07:39 -0600 (Fri, 10 Jan 2014)
New Revision: 7471

Modified:
   trunk/src/org/griphyn/vdl/engine/Karajan.java
   trunk/src/org/griphyn/vdl/karajan/Loader.java
Log:
allow user to specify a string to run on the command line using the -e option

Modified: trunk/src/org/griphyn/vdl/engine/Karajan.java
===================================================================
--- trunk/src/org/griphyn/vdl/engine/Karajan.java	2014-01-10 21:47:54 UTC (rev 7470)
+++ trunk/src/org/griphyn/vdl/engine/Karajan.java	2014-01-10 22:07:39 UTC (rev 7471)
@@ -107,7 +107,7 @@
 		compile(args[0], System.out, false);
 	}
 
-	public static void compile(String in, PrintStream out, boolean provenanceEnabled) throws CompilationException {
+	public static void compile(Object in, PrintStream out, boolean provenanceEnabled) throws CompilationException {
 		Karajan karajan = new Karajan();
 		StringTemplateGroup templates;
 		try {
@@ -140,7 +140,7 @@
 		out.println(code.toString());
 	}
 
-	public static ProgramDocument parseProgramXML(String defs)
+	public static ProgramDocument parseProgramXML(Object in)
 		throws XmlException, IOException {
 
 		XmlOptions options = new XmlOptions();
@@ -150,7 +150,15 @@
 		options.setLoadLineNumbers();
 
 		ProgramDocument programDoc;
-		programDoc  = ProgramDocument.Factory.parse(new File(defs), options);
+		if (in instanceof File) {
+		    programDoc  = ProgramDocument.Factory.parse((File) in, options);
+		}
+		else if (in instanceof String) {
+		    programDoc  = ProgramDocument.Factory.parse((String) in, options);
+		}
+		else {
+		    throw new IllegalArgumentException("Don't know how to parse a " + in.getClass().getName());
+		}
 
 		if(programDoc.validate(options)) {
 			logger.debug("Validation of XML intermediate file was successful");

Modified: trunk/src/org/griphyn/vdl/karajan/Loader.java
===================================================================
--- trunk/src/org/griphyn/vdl/karajan/Loader.java	2014-01-10 21:47:54 UTC (rev 7470)
+++ trunk/src/org/griphyn/vdl/karajan/Loader.java	2014-01-10 22:07:39 UTC (rev 7471)
@@ -21,6 +21,8 @@
 package org.griphyn.vdl.karajan;
 
 import java.io.BufferedReader;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileNotFoundException;
@@ -53,7 +55,6 @@
 import org.globus.cog.karajan.scheduler.WeightedHostScoreScheduler;
 import org.globus.cog.karajan.util.KarajanProperties;
 import org.globus.cog.util.ArgumentParser;
-import org.globus.cog.util.ArgumentParserException;
 import org.globus.cog.util.TextFileLoader;
 import org.globus.swift.data.Director;
 import org.griphyn.vdl.engine.Karajan;
@@ -89,6 +90,7 @@
     public static final String ARG_REDUCED_LOGGING = "reduced.logging";
     public static final String ARG_MINIMAL_LOGGING = "minimal.logging";
     public static final String ARG_PAUSE_ON_START = "pause.on.start";
+    public static final String ARG_EXECUTE = "e";
 
     public static final String CONST_VDL_OPERATION = "vdl:operation";
     public static final String VDL_OPERATION_RUN = "run";
@@ -106,9 +108,23 @@
         String runID = makeRunId(ap);
         
         try {
-            String project = ap.getStringValue(ArgumentParser.DEFAULT);
-            checkValidProject(project);
-            String projectName = projectName(project);
+            String project;
+            String source;
+            String projectName;
+            if (ap.isPresent(ARG_EXECUTE)) {
+                project = "<string>";
+                projectName = "<string>";
+                source = ap.getStringValue(ARG_EXECUTE);
+                if (ap.hasValue(ArgumentParser.DEFAULT)) {
+                    throw new IllegalArgumentException("-" + ARG_EXECUTE + " and <file> are mutually exclusive");
+                }
+            }
+            else {
+                project = ap.getStringValue(ArgumentParser.DEFAULT);
+                checkValidProject(project);
+                projectName = projectName(project);
+                source = null;
+            }
        
             setupLogging(ap, projectName, runID);
             VDL2Config config = loadConfig(ap);
@@ -123,6 +139,7 @@
                 loadCDM(ap);
             }
             
+            WrapperNode tree = null;
             if (project.endsWith(".swift")) {
                 try {
                     project = compile(project, ap.isPresent(ARG_RECOMPILE), provenanceEnabled);
@@ -134,8 +151,21 @@
                     logger.debug("Exception when compiling " + project, pe);
                     System.exit(3);
                 }
+                tree = load(project);
             }
-            WrapperNode tree = load(project);
+            else if (source != null) {
+                try {
+                    String kml = compileString(source, provenanceEnabled);
+                    tree = loadFromString(kml);
+                }
+                catch (ParsingException pe) {
+                    // the compiler should have already logged useful
+                    // error messages, so this log line is just for
+                    // debugging
+                    logger.debug("Exception when compiling " + project, pe);
+                    System.exit(3);
+                }
+            }
             
             tree.setProperty("name", projectName + "-" + runID);
             tree.setProperty(WrapperNode.FILENAME, project);
@@ -230,7 +260,7 @@
                 System.out.println(loadVersion());
                 System.exit(0);
             }
-            if (!ap.hasValue(ArgumentParser.DEFAULT)) {
+            if (!ap.hasValue(ArgumentParser.DEFAULT) && !ap.isPresent(ARG_EXECUTE)) {
                 System.out.println(loadVersion());
                 error("No Swift script specified");
             }
@@ -364,7 +394,7 @@
 
             try {
                 FileOutputStream f = new FileOutputStream(kml);
-                Karajan.compile(xml.getAbsolutePath(), new PrintStream(f), provenanceEnabled);
+                Karajan.compile(new File(xml.getAbsolutePath()), new PrintStream(f), provenanceEnabled);
                 f.close();
             }
             catch (Error e) {
@@ -392,7 +422,34 @@
         }
         return kml.getAbsolutePath();
     }
+    
+    public static String compileString(String source, boolean provenanceEnabled) throws
+            ParsingException, IncorrectInvocationException,
+            CompilationException, IOException {
+        debugText("SWIFTSCRIPT", source);
 
+        ByteArrayOutputStream swiftx = new ByteArrayOutputStream();
+        VDLt2VDLx.compile(new ByteArrayInputStream(source.getBytes()),
+            new PrintStream(swiftx));
+
+        ByteArrayOutputStream kml = new ByteArrayOutputStream();
+        try {
+            Karajan.compile(swiftx.toString(), new PrintStream(kml), provenanceEnabled);
+        }
+        catch (Error e) {
+            throw e;
+        }
+        catch (CompilationException e) {
+            throw e;
+        }
+        catch (Exception e) {
+            throw new CompilationException(
+                "Failed to convert .xml to .kml for input string", e);
+        }
+        return kml.toString();
+    }
+
+
     /**
        Enter the text content of given files into the log
        @param name A token printed in the log
@@ -412,6 +469,14 @@
     		logger.warn("Could not open: " + file);
     	}
 	}
+    
+    public static void debugText(String name, String source) {
+        Logger textLogger = Logger.getLogger("swift.textfiles");
+        if (textLogger.isDebugEnabled()) {
+            textLogger.debug("BEGIN " + name + ":\n" + source + "\n");
+            textLogger.debug("END " + name + ":");
+        }
+    }
 
     static void debugSitesText(VDL2Config config) {
 		String poolFile = config.getPoolFile();
@@ -555,7 +620,8 @@
                  "reports warnings only");
         ap.addFlag(ARG_PAUSE_ON_START, "Pauses execution on start. Useful for " +
         		"attaching a debugger or profiler to the swift process");
-        
+        ap.addOption(ARG_EXECUTE, "Runs the swift script code contained in <string>", 
+            "string", ArgumentParser.OPTIONAL);
 
         Map<String, PropInfo> desc = VDL2ConfigProperties.getPropertyDescriptions();
         for (Map.Entry<String, PropInfo> e : desc.entrySet()) {




More information about the Swift-commit mailing list