batch.py 3.66 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import os, sys,subprocess,time
import argparse
import shutil

if __name__ == '__main__':
    #Parameters definition
    parser = argparse.ArgumentParser(description='''
      Watermark and encrypts exams and prepares everything for moodle upload. 
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
    -d, --dpi       dpi parameter for conversion from pdf to images. Default: 250
    -q, --quality   quality parameter for jpeg. Default: 25
    -t, --tmp       tmp folder. Default: ./tmp
      ''')
    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 the folder names for moodle zip")
    parser.add_argument("-o", "--outfolder", default="./out", 
        help="output folder containing passwords.csv and moodle_feedbacks.zip. Default: ./out")
    parser.add_argument("-e", "--cores", default="2", 
        help="Number of cores for parallel processing. Default: 2")
    parser.add_argument("-p", "--password", default=" ", 
        help="sets global password. Default: empty, such that each PDF gets a custom password generated with 'pwgen'")
    parser.add_argument("-d", "--dpi", default="250", 
        help="dpi parameter for conversion from pdf to images. Default: 250")
    parser.add_argument("-t", "--tmp", default="./tmp", 
        help="tmp folder. Default: ./tmp/")

    args = parser.parse_args()
    infolder =args.infolder
    csv = args.csv
    outfolder = args.outfolder
    cores = args.cores
    dpi = args.dpi
    tmp = args.tmp
    starttime = time.time()

    #Empty 'out' folder
    for root, dirs, files in os.walk(outfolder):
        for f in files:
            os.unlink(os.path.join(root, f))
        for d in dirs:
            shutil.rmtree(os.path.join(root, d))

    #Empty 'tmp' folder
    for root, dirs, files in os.walk(tmp):
        for f in files:
            os.unlink(os.path.join(root, f))
        for d in dirs:
            shutil.rmtree(os.path.join(root, d))

    # Watermarking process    
    watermark_outfolder = tmp+'/pdfs_watermarked'
    if not os.path.exists(watermark_outfolder):
        os.makedirs(watermark_outfolder)
    subprocess.call((sys.executable, 'watermark.py', '--in', infolder,'--out', watermark_outfolder, '--cores', cores))
    
    encrypt_outfolder = tmp+'/pdfs_encrypted'
    if not os.path.exists(encrypt_outfolder):
        os.makedirs(encrypt_outfolder)
    if args.password==" ":
        subprocess.call((sys.executable, 'encrypt.py', '--in', watermark_outfolder, '--out',encrypt_outfolder,'--passwordout',outfolder))
    else:
        subprocess.call((sys.executable, 'encrypt.py', '--in', watermark_outfolder, '--out',encrypt_outfolder,'--passwordout',outfolder,'--password',args.password))

    #ZIP Archive preparation process
    if not os.path.exists('./tmp/tmp'):
        os.makedirs('./tmp/tmp')
    subprocess.call((sys.executable, 'preparemoodle.py','--in',encrypt_outfolder,'--csv',csv,'--batch','1', '--tmp', './tmp/tmp', '--out',outfolder+'/moodle_feedbacks.zip'))
    
    endtime = time.time()
    print(f'\nTotal time taken: {endtime-starttime:.2f}s\n')