DocBook: make LilyPond work more reliably.

This commit is contained in:
Thibaut Cuvelier 2021-09-27 00:39:10 +02:00
parent 508badc78a
commit e22f52e731

View File

@ -33,7 +33,6 @@ def need_lilypond(file):
def copy_docbook(args): def copy_docbook(args):
print(args)
if len(args) != 4: if len(args) != 4:
print('Exactly four arguments are expected, only %s found: %s.' % (len(args), args)) print('Exactly four arguments are expected, only %s found: %s.' % (len(args), args))
sys.exit(1) sys.exit(1)
@ -43,23 +42,27 @@ def copy_docbook(args):
in_file = args[2] in_file = args[2]
out_file = args[3] out_file = args[3]
has_lilypond = lilypond_command != "" and lilypond_command != "none" has_lilypond = lilypond_command not in {'', 'none'}
in_folder = os.path.split(in_file)[0]
# Guess the path for LilyPond. # Guess the path for LilyPond.
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') + '.')
print(">> LilyPond path: " + lilypond_folder) print('>> LilyPond callable as: ' + lilypond_command + '.')
print(">> Input file: " + in_file) print('>> LilyPond path: ' + lilypond_folder + '.')
print(">> Output file: " + out_file) print('>> Input file: ' + in_file + '.')
print('>> Input folder: ' + in_folder + '.')
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)
print('>> 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.
@ -78,28 +81,38 @@ def copy_docbook(args):
f_lily.write(line) f_lily.write(line)
os.unlink(in_file) os.unlink(in_file)
# Add LilyPond to the PATH. # Add LilyPond to the PATH. lilypond-book uses a direct call to lilypond from 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
# Make LilyPond believe it is working from the temporary LyX directory. Otherwise, it tries to find files
# starting from LyX's working directory...
os.chdir(in_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_args = ['--format=docbook', '--output=' + in_folder, in_lily_file]
command_python = ['python', lilypond_command, '--format=docbook', in_lily_file] command_raw = [lilypond_command] + command_args
command_python = ['python', lilypond_command] + command_args
print('>> Running LilyPond.')
sys.stdout.flush() # So that the LilyPond output is at the right place in the logs.
failed = False failed = False
try: try:
subprocess.check_call(command_raw, stdout=sys.stdout.fileno(), stderr=sys.stdout.fileno()) subprocess.check_call(command_raw, stdout=sys.stdout.fileno(), stderr=sys.stdout.fileno())
print(">> Success running LilyPond with " + str(command_raw)) print('>> Success running LilyPond with ')
print('>> ' + str(command_raw))
except (subprocess.CalledProcessError, OSError) as e1: except (subprocess.CalledProcessError, OSError) as e1:
try: try:
subprocess.check_call(command_python, stdout=sys.stdout.fileno(), stderr=sys.stdout.fileno()) subprocess.check_call(command_python, stdout=sys.stdout.fileno(), stderr=sys.stdout.fileno())
print(">> Success running LilyPond with " + str(command_python)) print('>> Success running LilyPond with ')
print('>> ' + str(command_python) + '.')
except (subprocess.CalledProcessError, OSError) as e2: except (subprocess.CalledProcessError, OSError) as e2:
print('>> Error from LilyPond') print('>> Error from LilyPond. The successive calls were:')
print('>> Error from trying ' + str(command_raw) + ':') print('>> (1) Error from trying ' + str(command_raw) + ':')
print(e1) print('>> (1) ' + str(e1))
print('>> Error from trying ' + str(command_python) + ':') print('>> (2) Error from trying ' + str(command_python) + ':')
print(e2) print('>> (2) ' + str(e2))
failed = True failed = True
if failed: if failed: