feat: integrate message to worker

This commit is contained in:
Bertrand Benjamin 2022-04-13 12:26:04 +02:00
parent ca0f498d4e
commit 9c05ef1551
7 changed files with 62 additions and 21 deletions

33
bopytex/message.py Normal file
View File

@ -0,0 +1,33 @@
class Message():
def __init__(self, status, out, err):
self._status = status
self._out = out
self._err = err
@property
def status(self):
return self._status
@property
def out(self):
return self._out
@property
def err(self):
return self._err
class SubprocessMessage(Message):
def __init__(self, process):
self._process = process
@property
def status(self):
return self._process.wait()
@property
def out(self):
return self._process.stdout
@property
def err(self):
return self._process.stderr

View File

@ -1,4 +1,5 @@
import subprocess import subprocess
from ..message import SubprocessMessage
def latexmk(args: dict, deps, output): def latexmk(args: dict, deps, output):
@ -8,5 +9,5 @@ def latexmk(args: dict, deps, output):
stderr=subprocess.PIPE, stderr=subprocess.PIPE,
universal_newlines=True universal_newlines=True
) )
yield from compile_process.stderr return SubprocessMessage(compile_process)

View File

@ -1,12 +1,20 @@
from jinja2.environment import Template from jinja2.environment import Template
from bopytex.message import Message
def generate(args, deps, output): def generate(args, deps, output):
env = args["jinja2"]["environment"] env = args["jinja2"]["environment"]
template = env.get_template(deps[0]) template = env.get_template(deps[0])
try:
with open(output, "w") as out: with open(output, "w") as out:
out.write(tpl2tex(template, metas=args)) out.write(tpl2tex(template, metas=args))
yield f"GENERATE - {deps[0]} to {output}"
return Message(0, [f"GENERATE - {deps[0]} to {output}"], [])
except Exception as e:
return Message(0, [], [e])
def tpl2tex(template: Template, metas: dict = {}) -> str: def tpl2tex(template: Template, metas: dict = {}) -> str:

View File

@ -1,5 +1,7 @@
import subprocess import subprocess
from bopytex.message import SubprocessMessage
def pdfjam(args: dict, deps, output): def pdfjam(args: dict, deps, output):
joining_process = subprocess.Popen( joining_process = subprocess.Popen(
["pdfjam"] + deps + ["-o", output], ["pdfjam"] + deps + ["-o", output],
@ -7,11 +9,5 @@ def pdfjam(args: dict, deps, output):
stderr=subprocess.PIPE, stderr=subprocess.PIPE,
universal_newlines=True, universal_newlines=True,
) )
# exit_code is always 66... return SubprocessMessage(joining_process)
exit_code = joining_process.wait()
if exit_code == 0:
yield "pdfjam success"
else:
yield "pdfjam failed"

View File

@ -22,14 +22,14 @@ extra parameters or packages included.
return source return source
def test_compile(tex_path): def test_latexmk(tex_path):
tmp_path = tex_path.parent tmp_path = tex_path.parent
os.chdir(tmp_path) os.chdir(tmp_path)
texfile = str(tex_path.name) texfile = str(tex_path.name)
output = "source.pdf" output = "source.pdf"
for err in latexmk({}, [texfile], "source.pdf"): message = latexmk({}, [texfile], "source.pdf")
assert 0
assert message.status == 0
assert Path(output).exists assert Path(output).exists

View File

@ -34,12 +34,15 @@ def test_generate(template_path, jinja2_env):
template = str(template_path.name) template = str(template_path.name)
output = "output" output = "output"
result = next(generate( message = generate(
args={"a": 2, "jinja2": {"environment": jinja2_env}}, args={"a": 2, "jinja2": {"environment": jinja2_env}},
deps=[template], deps=[template],
output=output, output=output,
)) )
assert result == "GENERATE - template.j2 to output" print(message.err)
assert message.status == 0
assert message.out == ["GENERATE - template.j2 to output"]
with open(output, "r") as out: with open(output, "r") as out:
lines = out.readlines() lines = out.readlines()
assert lines == ["Plop 2"] assert lines == ["Plop 2"]

View File

@ -31,9 +31,9 @@ def test_join_pdf(multiple_pdf):
output = "joined.pdf" output = "joined.pdf"
for msg in pdfjam({"pwd": Path.cwd()}, deps, output): message = pdfjam({"pwd": Path.cwd()}, deps, output)
assert msg == "pdfjam success"
assert message.status == 0
assert Path(output).exists() assert Path(output).exists()
@ -45,7 +45,7 @@ def test_join_pdf_failed(multiple_pdf):
output = "joined.pdf" output = "joined.pdf"
for msg in pdfjam({"pwd": Path.cwd()}, deps, output): message = pdfjam({"pwd": Path.cwd()}, deps, output)
assert msg == "pdfjam failed"
assert message.status == 66
assert not Path(output).exists() assert not Path(output).exists()