[Swift-commit] r3648 - in trunk: libexec src/org/griphyn/vdl/engine src/org/griphyn/vdl/karajan/lib/swiftscript tests/functions

noreply at svn.ci.uchicago.edu noreply at svn.ci.uchicago.edu
Mon Sep 27 13:51:55 CDT 2010


Author: wozniak
Date: 2010-09-27 13:51:54 -0500 (Mon, 27 Sep 2010)
New Revision: 3648

Added:
   trunk/src/org/griphyn/vdl/karajan/lib/swiftscript/Fprintf.java
   trunk/tests/functions/400-fprintf.check.sh
   trunk/tests/functions/400-fprintf.clean.sh
   trunk/tests/functions/400-fprintf.setup.sh
   trunk/tests/functions/400-fprintf.swift
Modified:
   trunk/libexec/vdl-lib.xml
   trunk/src/org/griphyn/vdl/engine/ProcedureSignature.java
Log:
New fprintf() and test


Modified: trunk/libexec/vdl-lib.xml
===================================================================
--- trunk/libexec/vdl-lib.xml	2010-09-27 17:56:09 UTC (rev 3647)
+++ trunk/libexec/vdl-lib.xml	2010-09-27 18:51:54 UTC (rev 3648)
@@ -20,6 +20,8 @@
 	<export name="assert"><elementDef classname="org.griphyn.vdl.karajan.lib.swiftscript.Assert"/></export>
 	<export name="trace"><elementDef classname="org.griphyn.vdl.karajan.lib.swiftscript.Misc"/></export>
 	<export name="tracef"><elementDef classname="org.griphyn.vdl.karajan.lib.swiftscript.Tracef"/></export>
+	<export name="fprintf"><elementDef classname="org.griphyn.vdl.karajan.lib.swiftscript.Fprintf"/></export>
+
 	<export name="java"><elementDef classname="org.griphyn.vdl.karajan.lib.swiftscript.Java"/></export>
 	<export name="fileName"><elementDef classname="org.griphyn.vdl.karajan.lib.swiftscript.FileName"/></export>
 	<export name="fileNames"><elementDef classname="org.griphyn.vdl.karajan.lib.swiftscript.FileNames"/></export>

Modified: trunk/src/org/griphyn/vdl/engine/ProcedureSignature.java
===================================================================
--- trunk/src/org/griphyn/vdl/engine/ProcedureSignature.java	2010-09-27 17:56:09 UTC (rev 3647)
+++ trunk/src/org/griphyn/vdl/engine/ProcedureSignature.java	2010-09-27 18:51:54 UTC (rev 3648)
@@ -143,6 +143,11 @@
 		tracef.setInvocationMode(INVOCATION_INTERNAL);
 		proceduresMap.put("tracef", tracef);
 		
+		ProcedureSignature fprintf = new ProcedureSignature("ftracef");
+		fprintf.setAnyNumOfInputArgs();
+		fprintf.setInvocationMode(INVOCATION_INTERNAL);
+		proceduresMap.put("fprintf", fprintf);
+		
 		ProcedureSignature assrt = new ProcedureSignature("assert");
 		assrt.setAnyNumOfInputArgs();
 		assrt.setInvocationMode(INVOCATION_INTERNAL);

Added: trunk/src/org/griphyn/vdl/karajan/lib/swiftscript/Fprintf.java
===================================================================
--- trunk/src/org/griphyn/vdl/karajan/lib/swiftscript/Fprintf.java	                        (rev 0)
+++ trunk/src/org/griphyn/vdl/karajan/lib/swiftscript/Fprintf.java	2010-09-27 18:51:54 UTC (rev 3648)
@@ -0,0 +1,107 @@
+package org.griphyn.vdl.karajan.lib.swiftscript;
+
+import java.io.FileWriter;
+import java.io.IOException;
+import java.util.concurrent.ConcurrentHashMap;
+
+import org.apache.log4j.Logger;
+import org.globus.cog.karajan.arguments.Arg;
+import org.globus.cog.karajan.stack.VariableStack;
+import org.globus.cog.karajan.workflow.ExecutionException;
+import org.griphyn.vdl.karajan.lib.SwiftArg;
+import org.griphyn.vdl.karajan.lib.VDLFunction;
+import org.griphyn.vdl.mapping.DSHandle;
+import org.griphyn.vdl.type.Types;
+
+/**
+    Formatted file output. <br>
+    Example: fprintf("tmp.log", "\t%s\n", "hello"); <br>
+    Appends to file.
+    @see Tracef, Sprintf
+    @author wozniak
+ */
+public class Fprintf extends VDLFunction {
+
+    private static final Logger logger = 
+        Logger.getLogger(Fprintf.class);
+    
+    static {
+        setArguments(Fprintf.class, new Arg[] { Arg.VARGS });
+    }
+
+    static ConcurrentHashMap<String, Object> openFiles = 
+        new ConcurrentHashMap<String, Object>();
+    
+    @Override
+    protected Object function(VariableStack stack) 
+    throws ExecutionException {
+        DSHandle[] args = SwiftArg.VARGS.asDSHandleArray(stack);
+
+        for (int i = 0; i < args.length; i++) {
+            DSHandle handle = args[i];
+            VDLFunction.waitFor(stack, handle);
+        }
+
+        check(args);
+        
+        String filename = args[0].toString();
+        String spec = args[1].toString(); 
+        DSHandle[] vars = Sprintf.copyArray(args, 2, args.length-2);
+        
+        StringBuilder output = new StringBuilder();
+        Sprintf.format(spec, vars, output);
+        String msg = output.toString();
+ 
+        logger.debug("file: " + filename + " msg: " + msg);        
+        write(filename, msg);
+        return null;
+    }
+    
+    private static void check(DSHandle[] args) 
+    throws ExecutionException {
+        if (args.length < 2)
+            throw new ExecutionException
+            ("fprintf(): requires at least 2 arguments!");
+        if (! args[0].getType().equals(Types.STRING))
+            throw new ExecutionException
+            ("fprintf(): first argument is a string filename!");
+        if (! args[0].getType().equals(Types.STRING))
+            throw new ExecutionException
+            ("fprintf(): second argument is a string specifier!");
+    }
+    
+    private static void write(String filename, String msg) 
+    throws ExecutionException {
+        acquire(filename);
+        
+        try {
+            FileWriter writer = new FileWriter(filename, true);
+            writer.write(msg);
+            writer.close();
+        }
+        catch (IOException e) {
+            throw new ExecutionException
+            ("write(): problem writing to: " + filename, e); 
+        }
+        
+        openFiles.remove(filename);
+    }
+    
+    private static void acquire(String filename) 
+    throws ExecutionException {
+        int count = 0;
+        Object marker = new Object();
+        while (openFiles.putIfAbsent(filename, marker) != null && 
+                count < 10) {
+            try {
+                Thread.sleep(count);
+            }
+            catch (InterruptedException e) 
+            {}
+            count++;
+        }
+        if (count == 10)
+            throw new ExecutionException
+            ("write(): could not acquire: " + filename);
+    }
+}

Added: trunk/tests/functions/400-fprintf.check.sh
===================================================================
--- trunk/tests/functions/400-fprintf.check.sh	                        (rev 0)
+++ trunk/tests/functions/400-fprintf.check.sh	2010-09-27 18:51:54 UTC (rev 3648)
@@ -0,0 +1,15 @@
+#!/bin/bash
+
+set -x
+
+FILES=( $( ls 400-fprintf-*.out ) )
+(( ${#FILES[@]} == 3 )) || exit 1
+
+grep hello 400-fprintf-1.out || exit 1
+
+LINES=$( wc -l 400-fprintf-2.out | cut -d ' ' -f 1 )
+(( ${LINES} == 3 )) || exit 1
+
+grep "hello: 32" 400-fprintf-3.out || exit 1
+
+exit 0


Property changes on: trunk/tests/functions/400-fprintf.check.sh
___________________________________________________________________
Name: svn:executable
   + *

Added: trunk/tests/functions/400-fprintf.clean.sh
===================================================================
--- trunk/tests/functions/400-fprintf.clean.sh	                        (rev 0)
+++ trunk/tests/functions/400-fprintf.clean.sh	2010-09-27 18:51:54 UTC (rev 3648)
@@ -0,0 +1,5 @@
+#!/bin/bash
+
+rm -v 400-fprintf-*.out || exit 1
+
+exit 0


Property changes on: trunk/tests/functions/400-fprintf.clean.sh
___________________________________________________________________
Name: svn:executable
   + *

Added: trunk/tests/functions/400-fprintf.setup.sh
===================================================================
--- trunk/tests/functions/400-fprintf.setup.sh	                        (rev 0)
+++ trunk/tests/functions/400-fprintf.setup.sh	2010-09-27 18:51:54 UTC (rev 3648)
@@ -0,0 +1,7 @@
+#!/bin/bash
+
+# fprintf() uses append mode, so ensure target files do not exist
+
+rm -fv 400-fprintf-*.out
+
+exit 0


Property changes on: trunk/tests/functions/400-fprintf.setup.sh
___________________________________________________________________
Name: svn:executable
   + *

Added: trunk/tests/functions/400-fprintf.swift
===================================================================
--- trunk/tests/functions/400-fprintf.swift	                        (rev 0)
+++ trunk/tests/functions/400-fprintf.swift	2010-09-27 18:51:54 UTC (rev 3648)
@@ -0,0 +1,4 @@
+
+fprintf("400-fprintf-1.out", "hello\n");
+fprintf("400-fprintf-2.out", "%s\n%s\n%s\n", "hi", "ok", "bye");
+fprintf("400-fprintf-3.out", "hello: %i\n", 32);




More information about the Swift-commit mailing list