DocBook: redirect LilyPond output to main LyX output to ease debugging.

This commit is contained in:
Thibaut Cuvelier 2021-09-26 21:13:17 +02:00
parent e983676f6c
commit 508badc78a

View File

@ -18,6 +18,7 @@
# /!\ The original file may be modified by this script! # /!\ The original file may be modified by this script!
import subprocess
import os import os
import os.path import os.path
import re import re
@ -48,17 +49,17 @@ def copy_docbook(args):
lilypond_folder = os.path.split(lilypond_command)[0] if has_lilypond else '' lilypond_folder = os.path.split(lilypond_command)[0] if has_lilypond else ''
# Help debugging. # Help debugging.
print("Given arguments:") print(">> Given arguments:")
print("LilyPond: " + ("present" if has_lilypond else "not found") + " " + lilypond_command) print(">> LilyPond: " + ("present" if has_lilypond else "not found") + " " + lilypond_command)
print("LilyPond path: " + lilypond_folder) print(">> LilyPond path: " + lilypond_folder)
print("Input file: " + in_file) print(">> Input file: " + in_file)
print("Output file: " + out_file) print(">> Output file: " + out_file)
# Apply LilyPond to the original file if available and needed. # Apply LilyPond to the original file if available and needed.
if has_lilypond and need_lilypond(in_file): if has_lilypond and need_lilypond(in_file):
in_lily_file = in_file.replace(".xml", ".lyxml") in_lily_file = in_file.replace(".xml", ".lyxml")
print("The input file needs a LilyPond pass and LilyPond is available.") print(">> The input file needs a LilyPond pass and LilyPond is available.")
print("Rewriting " + in_file + " as " + in_lily_file) print(">> Rewriting " + in_file + " as " + in_lily_file)
# LilyPond requires that its input file has the .lyxml extension. Due to a bug in LilyPond, # LilyPond requires that its input file has the .lyxml extension. Due to a bug in LilyPond,
# use " instead of ' to encode XML attributes. # use " instead of ' to encode XML attributes.
@ -69,36 +70,40 @@ def copy_docbook(args):
with open(in_file, 'r', encoding='utf-8') as f, open(in_lily_file, 'w', encoding='utf-8') as f_lily: with open(in_file, 'r', encoding='utf-8') as f, open(in_lily_file, 'w', encoding='utf-8') as f_lily:
for line in f: for line in f:
if "language='lilypond'" in line: if "language='lilypond'" in line:
# print(line)
# print(re.match('<programlisting\\s+language=\'lilypond\'.*?(role=\'(?P<options>.*?)\')?>', line))
line = re.sub( line = re.sub(
'<programlisting\\s+language=\'lilypond\'.*?(role=\'(?P<options>.*?)\')?>', '<programlisting\\s+language=\'lilypond\'.*?(role=\'(?P<options>.*?)\')?>',
'<programlisting language="lilypond" role="\\g<options>">', '<programlisting language="lilypond" role="\\g<options>">',
line line
) )
# print(line)
f_lily.write(line) f_lily.write(line)
os.unlink(in_file) os.unlink(in_file)
# shutil.move(in_file, in_lily_file)
# Add LilyPond to the PATH. # Add LilyPond to the PATH.
if os.path.isdir(lilypond_folder): if os.path.isdir(lilypond_folder):
os.environ['PATH'] += os.pathsep + lilypond_folder os.environ['PATH'] += os.pathsep + lilypond_folder
# Start LilyPond on the copied file. First test the binary, then check if adding Python helps. # Start LilyPond on the copied file. First test the binary, then check if adding Python helps.
command_raw = lilypond_command + ' --format=docbook ' + in_lily_file command_raw = [lilypond_command, '--format=docbook', in_lily_file]
command_python = 'python -tt "' + lilypond_command + '" --format=docbook ' + in_lily_file command_python = ['python', lilypond_command, '--format=docbook', in_lily_file]
if os.system(command_raw) == 0: failed = False
print("Success running LilyPond:") try:
print(command_raw) subprocess.check_call(command_raw, stdout=sys.stdout.fileno(), stderr=sys.stdout.fileno())
else: print(">> Success running LilyPond with " + str(command_raw))
if os.system(command_python) == 0: except (subprocess.CalledProcessError, OSError) as e1:
print("Success running LilyPond:") try:
print(command_python) subprocess.check_call(command_python, stdout=sys.stdout.fileno(), stderr=sys.stdout.fileno())
else: print(">> Success running LilyPond with " + str(command_python))
print('Error from LilyPond') except (subprocess.CalledProcessError, OSError) as e2:
sys.exit(1) print('>> Error from LilyPond')
print('>> Error from trying ' + str(command_raw) + ':')
print(e1)
print('>> Error from trying ' + str(command_python) + ':')
print(e2)
failed = True
if failed:
sys.exit(1)
# Now, in_file should have the LilyPond-processed contents. # Now, in_file should have the LilyPond-processed contents.