Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so dont bother with any of their useless mail servers here and just use oauth login instead. Thank the nice Russians for causing that. :)

Paste

Pasted as Java by twak ( 15 years ago )
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&#40; "class.php" &#41; );
            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 &#40;"/home/twak/Downloads/ads2/fims.moodle.gla.ac.uk/file.php/158/moddata/assignment/50/"&#41;;

            PrintWriter pw = new PrintWriter( new File &#40;"output.tex"&#41; );

            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 &#40;"./problem/FBook.java"&#41;.delete(); // on fail, don't use last person's work!

                        if ( !copyfile&#40; f.getAbsolutePath(&#41;, "./problem/FBook.java" ) )
                            System.out.println( "error copying " + f );
                        else
                        {
                            String res = runCmd&#40; "javac -cp problem problem/FBook.java" &#41;;
                            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&#40; "java  -cp problem Marker"&#41;;
                            System.out.println( res );
                            printTiny( pw, res );
                        }

                        pw.println("code:");
                        pw.println("\\begin{multicols}{2}");
                        printTiny( pw, readFile&#40; f.getAbsolutePath(&#41; ).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&#40; "pdflatex output.tex"&#41;; // <-- 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 &#40; e.getName(&#41;, "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 &#40;String cmd&#41;
    {
        Runtime rt = Runtime.getRuntime();

        class OK
        {
            boolean okay = true;
        }

        try
        {
            final Process pr = rt.exec&#40; cmd &#41;;
            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&#40; String path &#41; throws IOException
    {
        FileInputStream stream = new FileInputStream( new File&#40; path &#41; );
        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&#40; String srFile, String dtFile &#41;
    {
        try
        {
            File f1 = new File&#40; srFile &#41;;
            File f2 = new File&#40; dtFile &#41;;
            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();
    }
}

 

Revise this Paste

Children: 40827 40829
Your Name: Code Language: