2021-07-23 17:30:15 -04:00
|
|
|
# Copyright Sage Vaillancourt 2021
|
|
|
|
|
|
|
|
import os
|
|
|
|
import subprocess
|
2021-07-23 23:09:08 -04:00
|
|
|
from dataclasses import dataclass
|
|
|
|
from flask import send_from_directory
|
2021-07-23 17:30:15 -04:00
|
|
|
|
|
|
|
|
2021-07-27 23:03:03 -04:00
|
|
|
def get_unique():
|
|
|
|
import uuid
|
|
|
|
unique = str(uuid.uuid1().hex)
|
|
|
|
return unique
|
|
|
|
|
2021-07-28 18:42:02 -04:00
|
|
|
def get_datetime():
|
|
|
|
from datetime import datetime
|
|
|
|
now = datetime.now()
|
|
|
|
return now.strftime("%Y-%m-%d %H:%M:%S")
|
|
|
|
|
2021-07-29 22:15:07 -04:00
|
|
|
root_dir = os.path.dirname(os.getcwd())
|
|
|
|
proj_dir = root_dir + '/undercover/'
|
|
|
|
output_dir = proj_dir + 'outputs/'
|
|
|
|
base_tex_text = open(proj_dir + "/writing_templates/base.tex", "r").read()
|
|
|
|
|
2021-07-23 17:30:15 -04:00
|
|
|
@dataclass
|
|
|
|
class CLData():
|
|
|
|
username: str
|
|
|
|
company: str
|
|
|
|
jobandpronoun: str
|
|
|
|
skilltypes: str
|
|
|
|
myskills: str
|
|
|
|
closingtext: str
|
|
|
|
body: str
|
|
|
|
|
2021-07-24 01:08:18 -04:00
|
|
|
def get_pairs(self):
|
2021-07-24 00:26:44 -04:00
|
|
|
return [
|
2021-07-24 01:08:18 -04:00
|
|
|
("username", self.username),
|
|
|
|
("company", self.company),
|
|
|
|
("jobandpronoun", self.jobandpronoun),
|
|
|
|
("skilltypes", self.skilltypes),
|
|
|
|
("myskills", self.myskills),
|
|
|
|
("closingtext", self.closingtext),
|
|
|
|
("body", self.body),
|
2021-07-24 00:26:44 -04:00
|
|
|
]
|
|
|
|
|
2021-07-27 23:03:03 -04:00
|
|
|
def generate_pdf(self):
|
|
|
|
import threading
|
|
|
|
unique_id = get_unique()
|
|
|
|
unique_file = output_dir + unique_id + ".tex"
|
|
|
|
f = open(unique_file, "w")
|
|
|
|
for pair in self.get_pairs():
|
|
|
|
f.write("\\def \\" + pair[0] + "{" + pair[1] + "}\n")
|
2021-07-29 22:15:07 -04:00
|
|
|
f.write(base_tex_text)
|
2021-07-27 23:03:03 -04:00
|
|
|
f.close()
|
2021-07-23 23:09:08 -04:00
|
|
|
|
2021-07-27 23:39:30 -04:00
|
|
|
output_arg="-jobname=outputs/" + unique_id + " " + unique_file
|
|
|
|
com = "pdflatex -halt-on-error " + output_arg
|
2021-07-28 18:42:02 -04:00
|
|
|
build_text = "[" + get_datetime() + "] Building for " + unique_id + " "
|
|
|
|
result = subprocess.run(
|
|
|
|
['bash', '-c', com],
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
text=True
|
|
|
|
)
|
2021-07-27 23:39:30 -04:00
|
|
|
|
|
|
|
if result.returncode == 0:
|
2021-07-28 18:42:02 -04:00
|
|
|
print(build_text + "[SUCCESS]")
|
2021-07-28 19:21:02 -04:00
|
|
|
threading.Timer(60 * 5, cleanup, [output_dir + unique_id]).start()
|
2021-07-31 17:38:32 -04:00
|
|
|
compress_com = "gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.5 -dNOPAUSE -dQUIET -dBATCH -dPrinted=false -sOutputFile=outputs/" + unique_id + ".compressed.pdf outputs/" + unique_id + ".pdf"
|
|
|
|
|
|
|
|
result = subprocess.run(
|
|
|
|
['bash', '-c', compress_com],
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
text=True
|
|
|
|
)
|
2021-07-23 23:09:08 -04:00
|
|
|
|
2021-07-27 23:39:30 -04:00
|
|
|
return (send_from_directory(
|
2021-07-27 23:03:03 -04:00
|
|
|
output_dir,
|
2021-07-31 17:38:32 -04:00
|
|
|
unique_id + ".compressed.pdf",
|
2021-07-27 23:03:03 -04:00
|
|
|
attachment_filename=self.username.replace(" ", "") + "_CoverLetter.pdf",
|
|
|
|
as_attachment=True
|
2021-07-27 23:39:30 -04:00
|
|
|
), None)
|
|
|
|
else:
|
2021-07-28 18:42:02 -04:00
|
|
|
print(build_text + "[FAIL]")
|
|
|
|
# Collect output but delete boilerplate text
|
2021-07-28 19:21:02 -04:00
|
|
|
errors = list(map(str.strip, result.stdout.split("\n")))
|
2021-07-27 23:39:30 -04:00
|
|
|
del errors[:13]
|
|
|
|
del errors[-2:]
|
|
|
|
return (None, errors)
|
2021-07-24 15:25:58 -04:00
|
|
|
|
|
|
|
def cleanup(unique):
|
|
|
|
subprocess.run(['bash', '-c', "rm " + unique + ".*"])
|