Commit b663d8b3 authored by Christian Rohlfing's avatar Christian Rohlfing
Browse files

Bugfixes in batch.py

parent 959c35a8
Pipeline #455330 failed with stage
in 1 minute and 37 seconds
......@@ -14,49 +14,39 @@ def main(args):
# Argument handling
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 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
-e, --cores number of cores for watermarking. Default: 1
-d, --dpi dpi parameter for pdf to image conversion. Default: 250
-q, --quality quality parameter for jpeg. Default: 25
-t, --tmp tmp folder. Default: ./tmp
Attention: contents of folder 'out' will be deleted in the beginning!
''')
parser.add_argument("-i", "--infolder", default="./pdfs",
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 " +
"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 custom password")
parser.add_argument("-d", "--dpi", default="250",
help="DPI parameter for pdf to image conversion. " +
"Default: 250")
parser.add_argument("-t", "--tmp", default="./tmp",
help="tmp folder. Default: ./tmp/")
parser.add_argument("-u", "--supinfolder", default="./supplements",
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("-w", "--sup", default="0",
help="Flag for watermarking sample solutions. 0 means no 1 means yes. Default: 0")
parser.add_argument("-x", "--zip", default="0",
parser.add_argument(
"--supp", action='store_true', help="Watermarking supplements flag.")
parser.add_argument(
"-z", "--zip", default="0",
help="Input zip file. Default: 0")
args = parser.parse_args(args)
infolder = args.infolder
csv = args.csv
......@@ -65,30 +55,38 @@ Options:
dpi = args.dpi
tmp = args.tmp
password = args.password
sup = int(args.sup)
watermark_supp = args.supp
supinfolder = args.supinfolder
inzip = args.zip
starttime = time.time()
# Unzip submissions if provided zip archive
if inzip != "0" :
if inzip != "0":
if not os.path.exists(infolder):
os.makedirs(infolder)
preparepdf.main(['--in', inzip, '--out', infolder,'--csv', csv])
preparepdf.main(
['--inzip', inzip, '--outfolder', infolder, '--csv', csv])
# Watermarking
watermark_outfolder = os.path.join(tmp, 'pdfs_watermarked')
if not os.path.exists(watermark_outfolder):
os.makedirs(watermark_outfolder)
watermark.main(['--in', infolder, '--out', watermark_outfolder,
watermark.main([
'--infolder', infolder, '--outfolder', watermark_outfolder,
'--cores', cores, '--dpi', dpi])
if sup == 1:
# Watermarking of supplementary material
if watermark_supp:
supoutfolder = os.path.join(tmp, 'supplements_out')
if not os.path.exists(supoutfolder):
os.makedirs(supoutfolder)
supplements.main(['--in', supinfolder, '--out', supoutfolder])
watermark.main(['--in', supoutfolder, '--out', watermark_outfolder,
supplements.main([
'--infolder', supinfolder, '--outfolder', supoutfolder])
watermark.main([
'--infolder', supoutfolder, '--outfolder', watermark_outfolder,
'--cores', cores, '--dpi', dpi])
# Encryption
......@@ -97,7 +95,8 @@ Options:
os.makedirs(enc_out)
passwordcsv = os.path.join(outfolder, 'passwords.csv')
encrypt.main(['--in', watermark_outfolder, '--out', enc_out,
encrypt.main([
'--infolder', watermark_outfolder, '--outfolder', enc_out,
'--passwordout', passwordcsv, '--password', password])
# ZIP Archive preparation process
......@@ -105,8 +104,10 @@ Options:
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])
preparemoodle.main([
'--infolder', enc_out, '--csv', csv, '--tmp', moodle_tmp,
'--outfolder', moodle_out])
endtime = time.time()
print(f'\nTotal time taken: {endtime-starttime:.2f}s\n')
......
......@@ -38,7 +38,7 @@ def main(args):
"""Main function
1) Lists all PDFs to be encrypted from input folder
2) Encrypt pdf witn randomly generated 8 character long password
2) Encrypt pdf with randomly generated 8 character long password
3) Prepare a csv file that contains matriculation number and password
"""
......
......@@ -166,7 +166,8 @@ Processing {} students'''.format(num_students))
else:
dryout.append(
"- {old} -> {new}"
.format(old=pdffile, new=os.path.join(folder, pdffile)))
.format(old=pdffile,
new=os.path.join(folder, pdffile)))
elif not no_warn: # No PDF found
print("Warning: PDF for {matnum} (id={id}, name={name}) not found."
......@@ -181,7 +182,6 @@ Processing {} students'''.format(num_students))
print("Found {num_pdf} PDFs (CSV had {num_csv} entries)"
.format(num_pdf=num_found_pdfs, num_csv=num_students))
# Sanity check:
# Check for PDFs not reflected in CSV (student not registered in Moodle)
sanity_check(matnums_csv, matnums_folder)
......
......@@ -195,7 +195,7 @@ def combine_all_pdfs(pdf_pages, out_dir):
def watermark_pdf(input_dir, tmp_dir, output_dir,
fontsize, dpi, quality, pdf_file):
"""Watermarkes each page of a given PDF file
"""Watermarks each page of a given PDF file
Args:
input_dir (str): path to input directory
......@@ -212,7 +212,7 @@ def watermark_pdf(input_dir, tmp_dir, output_dir,
# img_files = convert_pdf_to_img(pdf_file, input_dir, tmp_dir, dpi)
img_files = convert_pdf_to_img(pdf_file, input_dir, tmp_dir, dpi)
# Extracting matriculation numebers
# Extracting matriculation numbers
matnum = matnum_utils.get_matnum(pdf_file)
# Watermarking PDF page images
......@@ -282,7 +282,8 @@ def main(args):
# Print status
starttime = time.time()
pdf_folder = os.listdir(infolder)
pdf_files = [_ for _ in pdf_folder
pdf_files = [
_ for _ in pdf_folder
if _.lower().endswith(".pdf") and matnum_utils.starts_with_matnum(_)]
print("""
Available PDFs to be watermarked:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment