package scratch;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Script to rip classnames from a moodle page, and concatonate the results into a tex file
* @author twak
*/
public class Tidy
{
Map<Integer, String> idToName = new HashMap();
Set<Integer> all = new HashSet();
public Tidy()
{
try
{
Pattern p = Pattern.compile( "userid\\=(\\d*)(.*?)Grades\\sfor\\s([\\s\\w]*)" );
// class.php is created by downloading the page which you use for grading (with editing switched off)
Matcher m = p.matcher( readFile( "class.php" ) );
while ( m.find() )
idToName.put( Integer.parseInt( m.group( 1 ) ), m.group( 3 ) );
System.out.println( "found people:" );
for ( int i : idToName.keySet() )
System.out.println( i + " " + idToName.get( i ) );
System.out.println( ">>>" + idToName.size() );
// replace the location of your downloaded assignments
File loc = new File ("/home/twak/Downloads/ads2/fims.moodle.gla.ac.uk/file.php/158/moddata/assignment/50/");
PrintWriter pw = new PrintWriter( new File ("output.tex") );
pw.println( "\\date{\\today}" );
pw.println( "\\documentclass[8pt]{article}" );
pw.println( "\\usepackage[a4paper]{geometry}");
pw.println( "\\usepackage{multicol}");
pw.println( "\\begin{document}" );
pw.println ( "/setlength{/topmargin}{0in}/setlength{/footskip}{0in}/setlength{/headheight}{0in}/setlength{/headsep}{0in}/setlength{/textheight}{9.5in}/setlength{/textwidth}{6.5in}/setlength{/oddsidemargin}{-0.5in}/setlength{/evensidemargin}{-0.5in}/setlength{/parindent}{0.25in}/setlength{/parskip}{0.25in}".replace( "/","\\" ) );
Set<Integer> missing = new HashSet();
person:
for ( int i : idToName.keySet() )
{
for ( File f : loc.listFiles() )
if ( f.getName().startsWith( Integer.toString( i ) ) )
{
System.out.println("processing "+idToName.get( i ));
pw.println( "{\\bf " + idToName.get( i )+"} \\newline" );
new File ("./problem/FBook.java").delete(); // on fail, don't use last person's work!
if ( !copyfile( f.getAbsolutePath(), "./problem/FBook.java" ) )
System.out.println( "error copying " + f );
else
{
String res = runCmd( "javac -cp problem problem/FBook.java" );
pw.println("compile results: \\newline");
if ( res.length() <= 1 )
pw.println( "ok! \\newline" );
else
{
System.out.println( res );
printTiny( pw, res );
}
pw.println("run results:");
res = runCmd( "java -cp problem Marker");
System.out.println( res );
printTiny( pw, res );
}
pw.println("code:");
pw.println("\\begin{multicols}{2}");
printTiny( pw, readFile( f.getAbsolutePath() ).replace("\t"," ") );
pw.println("\\end{multicols}\\clearpage");
continue person;
}
missing.add(i);
}
pw.println(" {\\bf We're missing work from:} \\newline \\newline ");
for ( int i : missing )
{
pw.println(idToName.get(i)+" \\newline ");
}
pw.println( "\\end{document}" );
pw.close();
Runtime.getRuntime().exec( "pdflatex output.tex"); // <-- might not work on your system
}
catch ( Exception ex )
{
ex.printStackTrace();
}
}
private void printTiny( PrintWriter pw, String res )
{
pw.println( "\\begin{tiny} \\begin{verbatim}" );
pw.println( res );
pw.println( "\\end{verbatim} \\end{tiny}" );
}
// public String getFileNamed(String name, File zipFile)
// {
// StringBuffer buffer = new StringBuffer();
// ZipInputStream zipinputstream;
// try
// {
// zipinputstream = new ZipInputStream( new FileInputStream( zipFile ) );
// ZipEntry e = zipinputstream.getNextEntry();
// while ( e != null )
// {
//
//
// RandomAccessFile rf = new RandomAccessFile ( e.getName(), "r" );
// String line;
//
// while ((line =rf.readLine()) !=null)
// buffer.append(line);
//
// e = zipinputstream.getNextEntry();
// }
// }
// catch ( Throwable ex )
// {
// ex.printStackTrace();
// }
//
// return buffer.toString();
// }
private String runCmd (String cmd)
{
Runtime rt = Runtime.getRuntime();
class OK
{
boolean okay = true;
}
try
{
final Process pr = rt.exec( cmd );
InputStream is = pr.getErrorStream();
final OK error = new OK();
new Thread()
{
@Override
public void run()
{
try
{
Thread.sleep( 5000 );
}
catch ( Throwable ex )
{
ex.printStackTrace();
}
pr.destroy();
error.okay = false;
}
}.start();
pr.waitFor();
return
(error.okay ? isToString( pr.getErrorStream() ) + "\n" + isToString( pr.getInputStream() ) : "\n terminated after 5 sec");
}
catch ( Throwable ex )
{
ex.printStackTrace();
return "error on call "+cmd;
}
}
private String isToString(InputStream is)
{
try
{
StringBuffer sb = new StringBuffer();
BufferedReader br = new BufferedReader( new InputStreamReader( is ) );
String line = null;
while ( (line = br.readLine()) != null )
sb.append( line +"\n");
return sb.toString();
}
catch ( Throwable ex )
{
ex.printStackTrace();
return "[error]";
}
}
private static String readFile( String path ) throws IOException
{
FileInputStream stream = new FileInputStream( new File( path ) );
try
{
FileChannel fc = stream.getChannel();
MappedByteBuffer bb = fc.map( FileChannel.MapMode.READ_ONLY, 0, fc.size() );
/* Instead of using default, pass in a decoder. */
return Charset.defaultCharset().decode( bb ).toString();
}
finally
{
stream.close();
}
}
public static boolean copyfile( String srFile, String dtFile )
{
try
{
File f1 = new File( srFile );
File f2 = new File( dtFile );
InputStream in = new FileInputStream( f1 );
//For Overwrite the file.
OutputStream out = new FileOutputStream( f2 );
byte[] buf = new byte[1024];
int len;
while ( (len = in.read( buf )) > 0 )
out.write( buf, 0, len );
in.close();
out.close();
return true;
}
catch ( Throwable ex )
{
ex.printStackTrace();
return false;
}
}
public static void main( String[] args )
{
new Tidy();
}
}
Add a code snippet to your website: www.paste.org