batch.py 4.61 KB
Newer Older
Christian Rohlfing's avatar
Christian Rohlfing committed
1
import os
2
import sys
Christian Rohlfing's avatar
Christian Rohlfing committed
3
import time
4
5
import argparse

Amrita's avatar
Amrita committed
6
import preparepdf
7
import supplements
Christian Rohlfing's avatar
Christian Rohlfing committed
8
9
10
11
import watermark
import encrypt
import preparemoodle

12
13
14

def main(args):
    # Argument handling
15
    parser = argparse.ArgumentParser(description='''
Christian Rohlfing's avatar
Christian Rohlfing committed
16
      Watermark and encrypts exams and prepares everything for moodle upload.
17
18
19
20
21
Attention: contents of folder 'out' will be deleted in the beginning!

Options:
    -h, --help      show this help text
    -i, --in        input folder with PDFs. Default: ./pdfs
22
23
24
25
26
27
    -c, --csv       Moodle grading CSV file, needed to construct folder names
                    for moodle
    -o, --out       output folder containing passwords.csv and
                    moodle_feedbacks.zip. Default: ./out
    -p, --password  sets global password. Default: empty, such that each PDF
                    gets custom password
28
    -e, --cores     number of cores for watermarking. Default: 1
Christian Rohlfing's avatar
Christian Rohlfing committed
29
    -d, --dpi       dpi parameter for pdf to image conversion. Default: 250
30
31
32
    -q, --quality   quality parameter for jpeg. Default: 25
    -t, --tmp       tmp folder. Default: ./tmp
      ''')
Christian Rohlfing's avatar
Christian Rohlfing committed
33
    parser.add_argument("-i", "--infolder", default="./pdfs",
34
                        help="Input folder with PDFs. Default: ./pdfs")
Christian Rohlfing's avatar
Christian Rohlfing committed
35
    parser.add_argument("-c", "--csv", default="Bewertungen.csv",
36
37
                        help="Moodle grading CSV file, needed to construct " +
                        "folder names for moodle zip")
Christian Rohlfing's avatar
Christian Rohlfing committed
38
    parser.add_argument("-o", "--outfolder", default="./out",
39
40
                        help="output folder containing passwords.csv and " +
                        "moodle_feedbacks.zip. Default: ./out")
Christian Rohlfing's avatar
Christian Rohlfing committed
41
    parser.add_argument("-e", "--cores", default="2",
42
43
                        help="Number of cores for parallel processing. " +
                        "Default: 2")
Christian Rohlfing's avatar
Christian Rohlfing committed
44
    parser.add_argument("-p", "--password", default="",
45
46
                        help="sets global password. Default: empty, " +
                        "such that each PDF gets custom password")
Christian Rohlfing's avatar
Christian Rohlfing committed
47
    parser.add_argument("-d", "--dpi", default="250",
48
49
                        help="DPI parameter for pdf to image conversion. " +
                        "Default: 250")
Christian Rohlfing's avatar
Christian Rohlfing committed
50
    parser.add_argument("-t", "--tmp", default="./tmp",
51
                        help="tmp folder. Default: ./tmp/")
52
53
54
55
    parser.add_argument("-u", "--supinfolder", default="./supplements",
                        help="Input folder with sample solutions. Default: ./supplements")
    parser.add_argument("-w", "--sup", default="0",
                        help="Flag for watermarking sample solutions. 0 means no 1 means yes. Default: 0")
Amrita's avatar
Amrita committed
56
57
    parser.add_argument("-x", "--zip", default="0",
                        help="Input zip file. Default: 0")
58

59

60
    args = parser.parse_args(args)
Christian Rohlfing's avatar
Christian Rohlfing committed
61
    infolder = args.infolder
62
63
64
65
66
    csv = args.csv
    outfolder = args.outfolder
    cores = args.cores
    dpi = args.dpi
    tmp = args.tmp
Christian Rohlfing's avatar
Christian Rohlfing committed
67
    password = args.password
68
69
    sup = int(args.sup)
    supinfolder = args.supinfolder
Amrita's avatar
Amrita committed
70
    inzip = args.zip
71

Christian Rohlfing's avatar
Christian Rohlfing committed
72
    starttime = time.time()
73

Amrita's avatar
Amrita committed
74
75
76
77
78
79
    # Unzip submissions if provided zip archive
    if inzip != "0" :
        if not os.path.exists(infolder):
            os.makedirs(infolder)
        preparepdf.main(['--in', inzip, '--out', infolder,'--csv', csv])
    
80
    # Watermarking
Christian Rohlfing's avatar
Christian Rohlfing committed
81
    watermark_outfolder = os.path.join(tmp, 'pdfs_watermarked')
82
83
    if not os.path.exists(watermark_outfolder):
        os.makedirs(watermark_outfolder)
Christian Rohlfing's avatar
Christian Rohlfing committed
84
    watermark.main(['--in', infolder, '--out', watermark_outfolder,
85
                    '--cores', cores, '--dpi', dpi])
86
    if sup == 1:
Amrita Deb's avatar
Amrita Deb committed
87
88
89
        supoutfolder = os.path.join(tmp, 'supplements_out')
        if not os.path.exists(supoutfolder):
            os.makedirs(supoutfolder)
90
        supplements.main(['--in', supinfolder, '--out', supoutfolder])
Amrita Deb's avatar
Amrita Deb committed
91
        watermark.main(['--in', supoutfolder, '--out', watermark_outfolder,
92
                    '--cores', cores, '--dpi', dpi])
Christian Rohlfing's avatar
Christian Rohlfing committed
93

94
    # Encryption
Christian Rohlfing's avatar
Christian Rohlfing committed
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
    enc_out = os.path.join(tmp, 'pdfs_encrypted')
    if not os.path.exists(enc_out):
        os.makedirs(enc_out)

    passwordcsv = os.path.join(outfolder, 'passwords.csv')
    encrypt.main(['--in', watermark_outfolder, '--out', enc_out,
                  '--passwordout', passwordcsv, '--password', password])

    # ZIP Archive preparation process
    moodle_out = os.path.join(outfolder, 'moodle_feedbacks.zip')
    moodle_tmp = os.path.join(tmp, 'tmp')
    if not os.path.exists(moodle_tmp):
        os.makedirs(moodle_tmp)
    preparemoodle.main(['--in', enc_out, '--csv', csv, '--tmp', moodle_tmp,
                        '--out', moodle_out])
110
111
112

    endtime = time.time()
    print(f'\nTotal time taken: {endtime-starttime:.2f}s\n')
113
114
115
116


if __name__ == '__main__':
    main(sys.argv[1:])