Commit fe18727a authored by Amrita Deb's avatar Amrita Deb
Browse files

Merge branch 'rohlfing-patch-batch' into 'master'

Rohlfing batch

See merge request !24
parents 9ac74c6e 83672471
import os, sys,subprocess,time
import os
import sys
import time
import argparse
import shutil
if __name__ == '__main__':
#Parameters definition
import watermark
import encrypt
import preparemoodle
def main(args):
# Argument handling
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!
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 conversion from pdf to images. Default: 250
-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")
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/")
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/")
args = parser.parse_args()
infolder =args.infolder
args = parser.parse_args(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))
password = args.password
#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))
starttime = time.time()
# Watermarking process
watermark_outfolder = tmp+'/pdfs_watermarked'
# Watermarking
watermark_outfolder = os.path.join(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))
watermark.main(['--in', infolder, '--out', watermark_outfolder,
'--cores', cores, '--dpi', dpi])
# Encryption
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])
#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')
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
......
Supports Markdown
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