batch.py 3.8 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
17
    Watermark and encrypts exams and prepares everything for moodle upload.
    Attention: contents of folder 'out' will be deleted in the beginning!
18
      ''')
Christian Rohlfing's avatar
Christian Rohlfing committed
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
    parser.add_argument(
        "-i", "--infolder", default="./pdfs",
        help="Input folder with PDFs. Default: ./pdfs")
    parser.add_argument(
        "-c", "--csv", default="Bewertungen.csv",
        help="Moodle grading CSV file, needed to construct Moodle folders.")
    parser.add_argument(
        "-o", "--outfolder", default="./out",
        help="output folder containing passwords CSV and Feedbacks Zip. " +
        "Default: ./out")
    parser.add_argument(
        "--cores", default="1",
        help="Number of cores for parallel processing. Default: 1")
    parser.add_argument(
        "-p", "--password", default="",
        help="sets global password. Default: empty, each PDF encrypted " +
        "with custom password")
    parser.add_argument(
        "-d", "--dpi", default="150",
        help="DPI parameter for pdf to image conversion. Default: 150")
    parser.add_argument(
        "-t", "--tmp", default="./tmp", help="Temp. folder. Default: ./tmp/")
    parser.add_argument(
        "--suppinfolder", default="./supplements",
        help="Input folder with sample solutions. Default: ./supplements")
    parser.add_argument(
        "--supp", action='store_true', help="Watermarking supplements flag.")
    parser.add_argument(
        "-z", "--zip", default="0",
        help="Input zip file. Default: 0")
49

50
    args = parser.parse_args(args)
Christian Rohlfing's avatar
Christian Rohlfing committed
51
    infolder = args.infolder
52
53
54
55
56
    csv = args.csv
    outfolder = args.outfolder
    cores = args.cores
    dpi = args.dpi
    tmp = args.tmp
Christian Rohlfing's avatar
Christian Rohlfing committed
57
    password = args.password
Christian Rohlfing's avatar
Christian Rohlfing committed
58
    watermark_supp = args.supp
59
    supinfolder = args.supinfolder
Amrita's avatar
Amrita committed
60
    inzip = args.zip
61

Christian Rohlfing's avatar
Christian Rohlfing committed
62
    starttime = time.time()
63

Amrita's avatar
Amrita committed
64
    # Unzip submissions if provided zip archive
Christian Rohlfing's avatar
Christian Rohlfing committed
65
    if inzip != "0":
Amrita's avatar
Amrita committed
66
67
        if not os.path.exists(infolder):
            os.makedirs(infolder)
Christian Rohlfing's avatar
Christian Rohlfing committed
68
69
70
        preparepdf.main(
            ['--inzip', inzip, '--outfolder', infolder, '--csv', csv])

71
    # Watermarking
Christian Rohlfing's avatar
Christian Rohlfing committed
72
    watermark_outfolder = os.path.join(tmp, 'pdfs_watermarked')
73
74
    if not os.path.exists(watermark_outfolder):
        os.makedirs(watermark_outfolder)
Christian Rohlfing's avatar
Christian Rohlfing committed
75
76
77
78
79
80
81

    watermark.main([
        '--infolder', infolder, '--outfolder', watermark_outfolder,
        '--cores', cores, '--dpi', dpi])

    # Watermarking of supplementary material
    if watermark_supp:
Amrita Deb's avatar
Amrita Deb committed
82
83
84
        supoutfolder = os.path.join(tmp, 'supplements_out')
        if not os.path.exists(supoutfolder):
            os.makedirs(supoutfolder)
Christian Rohlfing's avatar
Christian Rohlfing committed
85
86
87
88
89
90

        supplements.main([
            '--infolder', supinfolder, '--outfolder', supoutfolder])
        watermark.main([
            '--infolder', supoutfolder, '--outfolder', watermark_outfolder,
            '--cores', cores, '--dpi', dpi])
Christian Rohlfing's avatar
Christian Rohlfing committed
91

92
    # Encryption
Christian Rohlfing's avatar
Christian Rohlfing committed
93
94
95
96
97
    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')
Christian Rohlfing's avatar
Christian Rohlfing committed
98
99
100
    encrypt.main([
        '--infolder', watermark_outfolder, '--outfolder', enc_out,
        '--passwordout', passwordcsv, '--password', password])
Christian Rohlfing's avatar
Christian Rohlfing committed
101
102
103
104
105
106

    # 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)
Christian Rohlfing's avatar
Christian Rohlfing committed
107
108
109
110

    preparemoodle.main([
        '--infolder', enc_out, '--csv', csv, '--tmp', moodle_tmp,
        '--outfolder', moodle_out])
111
112
113

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


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