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
...@@ -13,49 +13,39 @@ import preparemoodle ...@@ -13,49 +13,39 @@ import preparemoodle
def main(args): def main(args):
# Argument handling # Argument handling
parser = argparse.ArgumentParser(description=''' parser = argparse.ArgumentParser(description='''
Watermark and encrypts exams and prepares everything for moodle upload. Watermark and encrypts exams and prepares everything for moodle upload.
Attention: contents of folder 'out' will be deleted in the beginning! 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
''') ''')
parser.add_argument("-i", "--infolder", default="./pdfs", parser.add_argument(
help="Input folder with PDFs. Default: ./pdfs") "-i", "--infolder", default="./pdfs",
parser.add_argument("-c", "--csv", default="Bewertungen.csv", help="Input folder with PDFs. Default: ./pdfs")
help="Moodle grading CSV file, needed to construct " + parser.add_argument(
"folder names for moodle zip") "-c", "--csv", default="Bewertungen.csv",
parser.add_argument("-o", "--outfolder", default="./out", help="Moodle grading CSV file, needed to construct Moodle folders.")
help="output folder containing passwords.csv and " + parser.add_argument(
"moodle_feedbacks.zip. Default: ./out") "-o", "--outfolder", default="./out",
parser.add_argument("-e", "--cores", default="2", help="output folder containing passwords CSV and Feedbacks Zip. " +
help="Number of cores for parallel processing. " + "Default: ./out")
"Default: 2") parser.add_argument(
parser.add_argument("-p", "--password", default="", "--cores", default="1",
help="sets global password. Default: empty, " + help="Number of cores for parallel processing. Default: 1")
"such that each PDF gets custom password") parser.add_argument(
parser.add_argument("-d", "--dpi", default="250", "-p", "--password", default="",
help="DPI parameter for pdf to image conversion. " + help="sets global password. Default: empty, each PDF encrypted " +
"Default: 250") "with custom password")
parser.add_argument("-t", "--tmp", default="./tmp", parser.add_argument(
help="tmp folder. Default: ./tmp/") "-d", "--dpi", default="150",
parser.add_argument("-u", "--supinfolder", default="./supplements", help="DPI parameter for pdf to image conversion. Default: 150")
help="Input folder with sample solutions. Default: ./supplements") parser.add_argument(
parser.add_argument("-w", "--sup", default="0", "-t", "--tmp", default="./tmp", help="Temp. folder. Default: ./tmp/")
help="Flag for watermarking sample solutions. 0 means no 1 means yes. Default: 0") parser.add_argument(
parser.add_argument("-x", "--zip", default="0", "--suppinfolder", default="./supplements",
help="Input zip file. Default: 0") 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")
args = parser.parse_args(args) args = parser.parse_args(args)
infolder = args.infolder infolder = args.infolder
...@@ -65,31 +55,39 @@ Options: ...@@ -65,31 +55,39 @@ Options:
dpi = args.dpi dpi = args.dpi
tmp = args.tmp tmp = args.tmp
password = args.password password = args.password
sup = int(args.sup) watermark_supp = args.supp
supinfolder = args.supinfolder supinfolder = args.supinfolder
inzip = args.zip inzip = args.zip
starttime = time.time() starttime = time.time()
# Unzip submissions if provided zip archive # Unzip submissions if provided zip archive
if inzip != "0" : if inzip != "0":
if not os.path.exists(infolder): if not os.path.exists(infolder):
os.makedirs(infolder) os.makedirs(infolder)
preparepdf.main(['--in', inzip, '--out', infolder,'--csv', csv]) preparepdf.main(
['--inzip', inzip, '--outfolder', infolder, '--csv', csv])
# Watermarking # Watermarking
watermark_outfolder = os.path.join(tmp, 'pdfs_watermarked') watermark_outfolder = os.path.join(tmp, 'pdfs_watermarked')
if not os.path.exists(watermark_outfolder): if not os.path.exists(watermark_outfolder):
os.makedirs(watermark_outfolder) os.makedirs(watermark_outfolder)
watermark.main(['--in', infolder, '--out', watermark_outfolder,
'--cores', cores, '--dpi', dpi]) watermark.main([
if sup == 1: '--infolder', infolder, '--outfolder', watermark_outfolder,
'--cores', cores, '--dpi', dpi])
# Watermarking of supplementary material
if watermark_supp:
supoutfolder = os.path.join(tmp, 'supplements_out') supoutfolder = os.path.join(tmp, 'supplements_out')
if not os.path.exists(supoutfolder): if not os.path.exists(supoutfolder):
os.makedirs(supoutfolder) os.makedirs(supoutfolder)
supplements.main(['--in', supinfolder, '--out', supoutfolder])
watermark.main(['--in', supoutfolder, '--out', watermark_outfolder, supplements.main([
'--cores', cores, '--dpi', dpi]) '--infolder', supinfolder, '--outfolder', supoutfolder])
watermark.main([
'--infolder', supoutfolder, '--outfolder', watermark_outfolder,
'--cores', cores, '--dpi', dpi])
# Encryption # Encryption
enc_out = os.path.join(tmp, 'pdfs_encrypted') enc_out = os.path.join(tmp, 'pdfs_encrypted')
...@@ -97,16 +95,19 @@ Options: ...@@ -97,16 +95,19 @@ Options:
os.makedirs(enc_out) os.makedirs(enc_out)
passwordcsv = os.path.join(outfolder, 'passwords.csv') passwordcsv = os.path.join(outfolder, 'passwords.csv')
encrypt.main(['--in', watermark_outfolder, '--out', enc_out, encrypt.main([
'--passwordout', passwordcsv, '--password', password]) '--infolder', watermark_outfolder, '--outfolder', enc_out,
'--passwordout', passwordcsv, '--password', password])
# ZIP Archive preparation process # ZIP Archive preparation process
moodle_out = os.path.join(outfolder, 'moodle_feedbacks.zip') moodle_out = os.path.join(outfolder, 'moodle_feedbacks.zip')
moodle_tmp = os.path.join(tmp, 'tmp') moodle_tmp = os.path.join(tmp, 'tmp')
if not os.path.exists(moodle_tmp): if not os.path.exists(moodle_tmp):
os.makedirs(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() endtime = time.time()
print(f'\nTotal time taken: {endtime-starttime:.2f}s\n') print(f'\nTotal time taken: {endtime-starttime:.2f}s\n')
......
...@@ -38,7 +38,7 @@ def main(args): ...@@ -38,7 +38,7 @@ def main(args):
"""Main function """Main function
1) Lists all PDFs to be encrypted from input folder 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 3) Prepare a csv file that contains matriculation number and password
""" """
......
...@@ -166,7 +166,8 @@ Processing {} students'''.format(num_students)) ...@@ -166,7 +166,8 @@ Processing {} students'''.format(num_students))
else: else:
dryout.append( dryout.append(
"- {old} -> {new}" "- {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 elif not no_warn: # No PDF found
print("Warning: PDF for {matnum} (id={id}, name={name}) not found." print("Warning: PDF for {matnum} (id={id}, name={name}) not found."
...@@ -180,7 +181,6 @@ Processing {} students'''.format(num_students)) ...@@ -180,7 +181,6 @@ Processing {} students'''.format(num_students))
print("done.") print("done.")
print("Found {num_pdf} PDFs (CSV had {num_csv} entries)" print("Found {num_pdf} PDFs (CSV had {num_csv} entries)"
.format(num_pdf=num_found_pdfs, num_csv=num_students)) .format(num_pdf=num_found_pdfs, num_csv=num_students))
# Sanity check: # Sanity check:
# Check for PDFs not reflected in CSV (student not registered in Moodle) # Check for PDFs not reflected in CSV (student not registered in Moodle)
......
...@@ -195,7 +195,7 @@ def combine_all_pdfs(pdf_pages, out_dir): ...@@ -195,7 +195,7 @@ def combine_all_pdfs(pdf_pages, out_dir):
def watermark_pdf(input_dir, tmp_dir, output_dir, def watermark_pdf(input_dir, tmp_dir, output_dir,
fontsize, dpi, quality, pdf_file): fontsize, dpi, quality, pdf_file):
"""Watermarkes each page of a given PDF file """Watermarks each page of a given PDF file
Args: Args:
input_dir (str): path to input directory input_dir (str): path to input directory
...@@ -212,7 +212,7 @@ def watermark_pdf(input_dir, tmp_dir, output_dir, ...@@ -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)
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) matnum = matnum_utils.get_matnum(pdf_file)
# Watermarking PDF page images # Watermarking PDF page images
...@@ -282,8 +282,9 @@ def main(args): ...@@ -282,8 +282,9 @@ def main(args):
# Print status # Print status
starttime = time.time() starttime = time.time()
pdf_folder = os.listdir(infolder) pdf_folder = os.listdir(infolder)
pdf_files = [_ for _ in pdf_folder pdf_files = [
if _.lower().endswith(".pdf") and matnum_utils.starts_with_matnum(_)] _ for _ in pdf_folder
if _.lower().endswith(".pdf") and matnum_utils.starts_with_matnum(_)]
print(""" print("""
Available PDFs to be watermarked: 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