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

Bugfixes and new test for batch.py

parent 66ace6c1
import os
import sys
import time
import argparse
import shutil
import watermark
import encrypt
import preparemoodle
if __name__ == '__main__':
# Parameters definition
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!
......@@ -16,30 +17,38 @@ 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'
-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",
help="Input folder with PDFs. 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")
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")
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")
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'")
help="sets global password. Default: empty, " +
"such that each PDF gets custom password")
parser.add_argument("-d", "--dpi", default="250",
help="dpi parameter for conversion from pdf to images. Default: 250")
help="DPI parameter for pdf to image conversion. " +
"Default: 250")
parser.add_argument("-t", "--tmp", default="./tmp",
help="tmp folder. Default: ./tmp/")
help="tmp folder. Default: ./tmp/")
args = parser.parse_args()
args = parser.parse_args(args)
infolder = args.infolder
csv = args.csv
outfolder = args.outfolder
......@@ -50,13 +59,14 @@ Options:
starttime = time.time()
# Watermarking process
# 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,
'--cores', cores])
'--cores', cores, '--dpi', dpi])
# Encryption
enc_out = os.path.join(tmp, 'pdfs_encrypted')
if not os.path.exists(enc_out):
os.makedirs(enc_out)
......@@ -75,3 +85,7 @@ Options:
endtime = time.time()
print(f'\nTotal time taken: {endtime-starttime:.2f}s\n')
if __name__ == '__main__':
main(sys.argv[1:])
import unittest
import time
import os
import tempfile
import shutil
class MainTest(unittest.TestCase):
def setUp(self):
self.tic = time.time() # todo this is sooo ugly
self.test_dir = tempfile.mkdtemp()
def tearDown(self):
self.toc = time.time()
t = self.toc - self.tic
print('Time: %.3f' % (t))
def test_batch(self):
import batch
expected_files = ['moodle_feedbacks.zip', 'passwords.csv']
expected_folders = ['Nachname, Vorname_436452_assignsubmission_file_']
expected_pdfs = ['123456_Nachname_w_aes.pdf']
# Prepare parameter
in_dir = './pdfs'
out_dir = os.path.join(self.test_dir, 'out')
os.mkdir(out_dir)
tmp_dir = os.path.join(self.test_dir, 'tmp')
os.mkdir(tmp_dir)
zipout_dir = os.path.join(self.test_dir, 'zipout')
os.mkdir(zipout_dir)
# Copy supplements file
batch.main(["-i", in_dir, "-o", out_dir, "-t", tmp_dir,
"--cores", "1", "--dpi", "100"])
# Assert output
created_files = os.listdir(out_dir)
created_files.sort()
self.assertEqual(expected_files, created_files)
# Unpack zip
zipfullfile = os.path.join(out_dir, 'moodle_feedbacks.zip')
shutil.unpack_archive(zipfullfile, zipout_dir)
# Assert zip output
created_folders = os.listdir(zipout_dir)
created_folders.sort()
self.assertEqual(expected_folders, created_folders)
created_pdfs = os.listdir(os.path.join(zipout_dir, created_folders[0]))
self.assertEqual(created_pdfs, expected_pdfs)
......@@ -12,22 +12,13 @@ def find_file(pattern, path):
list: list of filenames in folder matching pattern
"""
if os.name == "posix":
import subprocess
result = [line[2:] for line in subprocess.check_output(
"find " + path + " -type f -name " + pattern,
shell=True).splitlines()]
result = [tmp.decode("utf-8") for tmp in result]
else:
import fnmatch
result = []
for root, _, files in os.walk(path):
for name in files:
if fnmatch.fnmatch(name, pattern):
result.append(os.path.join(root, name))
import fnmatch
result = []
for root, _, files in os.walk(path):
for name in files:
if fnmatch.fnmatch(name, pattern):
result.append(os.path.join(root, name))
return result
......
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