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

Christian Rohlfing's avatar
Christian Rohlfing committed
6
7
8
9
import watermark
import encrypt
import preparemoodle

10
if __name__ == '__main__':
Christian Rohlfing's avatar
Christian Rohlfing committed
11
    # Parameters definition
12
    parser = argparse.ArgumentParser(description='''
Christian Rohlfing's avatar
Christian Rohlfing committed
13
      Watermark and encrypts exams and prepares everything for moodle upload.
14
15
16
17
18
19
20
21
22
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
    -c, --csv       Moodle grading CSV file, needed to construct the folder names for moodle zip
    -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 a custom password generated with 'pwgen'
    -e, --cores     number of cores for watermarking. Default: 1
Christian Rohlfing's avatar
Christian Rohlfing committed
23
    -d, --dpi       dpi parameter for pdf to image conversion. Default: 250
24
25
26
    -q, --quality   quality parameter for jpeg. Default: 25
    -t, --tmp       tmp folder. Default: ./tmp
      ''')
Christian Rohlfing's avatar
Christian Rohlfing committed
27
    parser.add_argument("-i", "--infolder", default="./pdfs",
28
        help="Input folder with PDFs. Default: ./pdfs")
Christian Rohlfing's avatar
Christian Rohlfing committed
29
    parser.add_argument("-c", "--csv", default="Bewertungen.csv",
30
        help="Moodle grading CSV file, needed to construct the folder names for moodle zip")
Christian Rohlfing's avatar
Christian Rohlfing committed
31
    parser.add_argument("-o", "--outfolder", default="./out",
32
        help="output folder containing passwords.csv and moodle_feedbacks.zip. Default: ./out")
Christian Rohlfing's avatar
Christian Rohlfing committed
33
    parser.add_argument("-e", "--cores", default="2",
34
        help="Number of cores for parallel processing. Default: 2")
Christian Rohlfing's avatar
Christian Rohlfing committed
35
    parser.add_argument("-p", "--password", default="",
36
        help="sets global password. Default: empty, such that each PDF gets a custom password generated with 'pwgen'")
Christian Rohlfing's avatar
Christian Rohlfing committed
37
    parser.add_argument("-d", "--dpi", default="250",
38
        help="dpi parameter for conversion from pdf to images. Default: 250")
Christian Rohlfing's avatar
Christian Rohlfing committed
39
    parser.add_argument("-t", "--tmp", default="./tmp",
40
41
42
        help="tmp folder. Default: ./tmp/")

    args = parser.parse_args()
Christian Rohlfing's avatar
Christian Rohlfing committed
43
    infolder = args.infolder
44
45
46
47
48
    csv = args.csv
    outfolder = args.outfolder
    cores = args.cores
    dpi = args.dpi
    tmp = args.tmp
Christian Rohlfing's avatar
Christian Rohlfing committed
49
    password = args.password
50

Christian Rohlfing's avatar
Christian Rohlfing committed
51
    starttime = time.time()
52

Christian Rohlfing's avatar
Christian Rohlfing committed
53
54
    # Watermarking process
    watermark_outfolder = os.path.join(tmp, 'pdfs_watermarked')
55
56
    if not os.path.exists(watermark_outfolder):
        os.makedirs(watermark_outfolder)
Christian Rohlfing's avatar
Christian Rohlfing committed
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
    watermark.main(['--in', infolder, '--out', watermark_outfolder,
                    '--cores', cores])

    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])
75
76
77

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