Expand source code
def run_hum2pdf(
humdrum_path,
pdf_path,
make_dirs=True,
has_colors: bool = False,
keep_intermediate_files: bool = False,
capture_output: bool = False,
):
return_code = 1
assert os.path.exists(HUM2PDF)
assert os.path.exists(HUM2PDF_NO_COLOR)
if make_dirs:
dirs = os.path.dirname(pdf_path)
if dirs:
os.makedirs(dirs, exist_ok=True)
try:
if has_colors:
cmd = ["bash", HUM2PDF, humdrum_path, pdf_path]
if keep_intermediate_files:
cmd.append("y")
print("+ " + " ".join(cmd))
subprocess.run(cmd, check=True)
else:
if keep_intermediate_files:
raise NotImplementedError(
"I need to add a flag to the underlying shell script"
)
subprocess.run(
["bash", HUM2PDF_NO_COLOR, humdrum_path, pdf_path],
check=True,
capture_output=capture_output,
)
return_code = 0
except subprocess.CalledProcessError:
# (Malcolm 2023-12-25) Some part of the pipeline seems to sometimes fail when
# using autobeam, so we remove any line that says `!!!filter: autobeam`
# and then try again
if keep_intermediate_files:
tmp_krn_path = os.path.join(
os.path.expanduser("~"), "tmp", "hum2pdf", "temp.krn"
)
else:
_, tmp_krn_path = tempfile.mkstemp(suffix=".krn")
try:
with open(humdrum_path) as inf:
contents = inf.readlines()
with open(tmp_krn_path, "w") as outf:
for line in contents:
if line.startswith("!!!filter: autobeam"):
continue
outf.write(line)
# For unknown reasons capture_output=True in the next column seems to cause
# verovio to experience segmentation faults
if has_colors:
cmd = ["bash", HUM2PDF, tmp_krn_path, pdf_path]
if keep_intermediate_files:
cmd.append("y")
print("+ " + " ".join(cmd))
subprocess.run(cmd, check=True, capture_output=capture_output)
else:
if keep_intermediate_files:
raise NotImplementedError(
"I need to add a flag to the underlying shell script"
)
subprocess.run(
["bash", HUM2PDF_NO_COLOR, tmp_krn_path, pdf_path],
check=True,
capture_output=capture_output,
)
return_code = 0
except subprocess.CalledProcessError:
print("hum2pdf failed, skipping")
# else:
# print("autobeam failed")
finally:
if not keep_intermediate_files:
os.remove(tmp_krn_path)
# TODO: (Malcolm 2024-01-11) the test of this function is failing,
# work out why
return return_code