/usr/bin/lilypond-book is in lilypond 2.14.2-2.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 | #!/usr/bin/python
# -*- coding: utf-8 -*-
# This file is part of LilyPond, the GNU music typesetter.
#
# LilyPond is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# LilyPond is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with LilyPond. If not, see <http://www.gnu.org/licenses/>.
'''
Example usage:
test:
lilypond-book --filter="tr '[a-z]' '[A-Z]'" BOOK
convert-ly on book:
lilypond-book --filter="convert-ly --no-version --from=1.6.11 -" BOOK
classic lilypond-book:
lilypond-book --process="lilypond" BOOK.tely
TODO:
* ly-options: intertext?
* --line-width?
* eps in latex / eps by lilypond -b ps?
* check latex parameters, twocolumn, multicolumn?
* use --png --ps --pdf for making images?
* Converting from lilypond-book source, substitute:
@mbinclude foo.itely -> @include foo.itely
\mbinput -> \input
'''
# TODO: Better solve the global_options copying to the snippets...
import glob
import os
import re
import stat
import sys
import tempfile
import imp
from optparse import OptionGroup
"""
This generic code used for all python scripts.
The quotes are to ensure that the source .py file can still be
run as a python script, but does not include any sys.path handling.
Otherwise, the lilypond-book calls inside the build
might modify installed .pyc files.
"""
for d in ['/usr/share/lilypond/2.14.2',
'/usr/lib/lilypond/2.14.2']:
sys.path.insert (0, os.path.join (d, 'python'))
# dynamic relocation, for GUB binaries.
bindir = os.path.abspath (os.path.dirname (sys.argv[0]))
for p in ['share', 'lib']:
datadir = os.path.abspath (bindir + '/../%s/lilypond/current/python/' % p)
sys.path.insert (0, datadir)
"""
"""
import lilylib as ly
import fontextract
import langdefs
global _;_=ly._
import book_base as BookBase
import book_snippets as BookSnippet
import book_html
import book_docbook
import book_texinfo
import book_latex
ly.require_python_version ()
original_dir = os.getcwd ()
backend = 'ps'
help_summary = (
_ ("Process LilyPond snippets in hybrid HTML, LaTeX, texinfo or DocBook document.")
+ '\n\n'
+ _ ("Examples:")
+ '''
$ lilypond-book --filter="tr '[a-z]' '[A-Z]'" %(BOOK)s
$ lilypond-book -F "convert-ly --no-version --from=2.0.0 -" %(BOOK)s
$ lilypond-book --process='lilypond -I include' %(BOOK)s
''' % {'BOOK': _ ("BOOK")})
authors = ('Jan Nieuwenhuizen <janneke@gnu.org>',
'Han-Wen Nienhuys <hanwen@xs4all.nl>')
################################################################
def exit (i):
if global_options.verbose:
raise Exception (_ ('Exiting (%d)...') % i)
else:
sys.exit (i)
def identify ():
ly.encoded_write (sys.stdout, '%s (GNU LilyPond) %s\n' % (ly.program_name, ly.program_version))
progress = ly.progress
warning = ly.warning
error = ly.error
def warranty ():
identify ()
ly.encoded_write (sys.stdout, '''
%s
%s
%s
%s
''' % ( _ ('Copyright (c) %s by') % '2001--2011',
'\n '.join (authors),
_ ("Distributed under terms of the GNU General Public License."),
_ ("It comes with NO WARRANTY.")))
def get_option_parser ():
p = ly.get_option_parser (usage=_ ("%s [OPTION]... FILE") % 'lilypond-book',
description=help_summary,
conflict_handler="resolve",
add_help_option=False)
p.add_option ('-F', '--filter', metavar=_ ("FILTER"),
action="store",
dest="filter_cmd",
help=_ ("pipe snippets through FILTER [default: `convert-ly -n -']"),
default=None)
p.add_option ('-f', '--format',
help=_ ("use output format FORMAT (texi [default], texi-html, latex, html, docbook)"),
metavar=_ ("FORMAT"),
action='store')
p.add_option("-h", "--help",
action="help",
help=_ ("show this help and exit"))
p.add_option ("-I", '--include', help=_ ("add DIR to include path"),
metavar=_ ("DIR"),
action='append', dest='include_path',
default=[os.path.abspath (os.getcwd ())])
p.add_option ('--info-images-dir',
help=_ ("format Texinfo output so that Info will "
"look for images of music in DIR"),
metavar=_ ("DIR"),
action='store', dest='info_images_dir',
default='')
p.add_option ('--left-padding',
metavar=_ ("PAD"),
dest="padding_mm",
help=_ ("pad left side of music to align music inspite of uneven bar numbers (in mm)"),
type="float",
default=3.0)
p.add_option ('--lily-output-dir',
help=_ ("write lily-XXX files to DIR, link into --output dir"),
metavar=_ ("DIR"),
action='store', dest='lily_output_dir',
default=None)
p.add_option ('--load-custom-package', help=_ ("Load the additional python PACKAGE (containing e.g. a custom output format)"),
metavar=_ ("PACKAGE"),
action='append', dest='custom_packages',
default=[])
p.add_option ("-o", '--output', help=_ ("write output to DIR"),
metavar=_ ("DIR"),
action='store', dest='output_dir',
default='')
p.add_option ('-P', '--process', metavar=_ ("COMMAND"),
help = _ ("process ly_files using COMMAND FILE..."),
action='store',
dest='process_cmd', default='')
p.add_option ('-s', '--safe', help=_ ("Compile snippets in safe mode"),
action="store_true",
default=False,
dest="safe_mode")
p.add_option ('--skip-lily-check',
help=_ ("do not fail if no lilypond output is found"),
metavar=_ ("DIR"),
action='store_true', dest='skip_lilypond_run',
default=False)
p.add_option ('--skip-png-check',
help=_ ("do not fail if no PNG images are found for EPS files"),
metavar=_ ("DIR"),
action='store_true', dest='skip_png_check',
default=False)
p.add_option ('--use-source-file-names',
help=_ ("write snippet output files with the same base name as their source file"),
action='store_true', dest='use_source_file_names',
default=False)
p.add_option ('-V', '--verbose', help=_ ("be verbose"),
action="store_true",
default=False,
dest="verbose")
p.version = "2.14.2"
p.add_option("--version",
action="version",
help=_ ("show version number and exit"))
p.add_option ('-w', '--warranty',
help=_ ("show warranty and copyright"),
action='store_true')
group = OptionGroup (p, "Options only for the latex and texinfo backends")
group.add_option ('--latex-program',
help=_ ("run executable PROG instead of latex, or in\n\
case --pdf option is set instead of pdflatex"),
metavar=_ ("PROG"),
action='store', dest='latex_program',
default='latex')
group.add_option ('--pdf',
action="store_true",
dest="create_pdf",
help=_ ("create PDF files for use with PDFTeX"),
default=False)
p.add_option_group (group)
p.add_option_group ('',
description=(
_ ("Report bugs via %s")
% ' http://post.gmane.org/post.php'
'?group=gmane.comp.gnu.lilypond.bugs') + '\n')
for formatter in BookBase.all_formats:
formatter.add_options (p)
return p
lilypond_binary = os.path.join ('/usr/bin', 'lilypond')
# If we are called with full path, try to use lilypond binary
# installed in the same path; this is needed in GUB binaries, where
# @bindir is always different from the installed binary path.
if 'bindir' in globals () and bindir:
lilypond_binary = os.path.join (bindir, 'lilypond')
# Only use installed binary when we are installed too.
if '/usr/bin' == ('@' + 'bindir@') or not os.path.exists (lilypond_binary):
lilypond_binary = 'lilypond'
global_options = None
def find_linestarts (s):
nls = [0]
start = 0
end = len (s)
while 1:
i = s.find ('\n', start)
if i < 0:
break
i = i + 1
nls.append (i)
start = i
nls.append (len (s))
return nls
def find_toplevel_snippets (input_string, formatter):
res = {}
types = formatter.supported_snippet_types ()
for t in types:
res[t] = re.compile (formatter.snippet_regexp (t))
snippets = []
index = 0
found = dict ([(t, None) for t in types])
line_starts = find_linestarts (input_string)
line_start_idx = 0
# We want to search for multiple regexes, without searching
# the string multiple times for one regex.
# Hence, we use earlier results to limit the string portion
# where we search.
# Since every part of the string is traversed at most once for
# every type of snippet, this is linear.
while 1:
first = None
endex = 1 << 30
for type in types:
if not found[type] or found[type][0] < index:
found[type] = None
m = res[type].search (input_string[index:endex])
if not m:
continue
klass = global_options.formatter.snippet_class (type)
start = index + m.start ('match')
line_number = line_start_idx
while (line_starts[line_number] < start):
line_number += 1
line_number += 1
snip = klass (type, m, formatter, line_number, global_options)
found[type] = (start, snip)
if (found[type]
and (not first
or found[type][0] < found[first][0])):
first = type
# FIXME.
# Limiting the search space is a cute
# idea, but this *requires* to search
# for possible containing blocks
# first, at least as long as we do not
# search for the start of blocks, but
# always/directly for the entire
# @block ... @end block.
endex = found[first][0]
if not first:
snippets.append (BookSnippet.Substring (input_string, index, len (input_string), line_start_idx))
break
while (start > line_starts[line_start_idx+1]):
line_start_idx += 1
(start, snip) = found[first]
snippets.append (BookSnippet.Substring (input_string, index, start, line_start_idx + 1))
snippets.append (snip)
found[first] = None
index = start + len (snip.match.group ('match'))
return snippets
def system_in_directory (cmd, directory):
"""Execute a command in a different directory.
Because of win32 compatibility, we can't simply use subprocess.
"""
current = os.getcwd()
os.chdir (directory)
ly.system(cmd, be_verbose=global_options.verbose,
progress_p=1)
os.chdir (current)
def process_snippets (cmd, snippets,
formatter, lily_output_dir):
"""Run cmd on all of the .ly files from snippets."""
if not snippets:
return
cmd = formatter.adjust_snippet_command (cmd)
checksum = snippet_list_checksum (snippets)
contents = '\n'.join (['snippet-map-%d.ly' % checksum]
+ list (set ([snip.basename() + '.ly' for snip in snippets])))
name = os.path.join (lily_output_dir,
'snippet-names-%d.ly' % checksum)
file (name, 'wb').write (contents)
system_in_directory (' '.join ([cmd, ly.mkarg (name)]),
lily_output_dir)
def snippet_list_checksum (snippets):
return hash (' '.join([l.basename() for l in snippets]))
def write_file_map (lys, name):
snippet_map = file (os.path.join (
global_options.lily_output_dir,
'snippet-map-%d.ly' % snippet_list_checksum (lys)), 'w')
snippet_map.write ("""
#(define version-seen #t)
#(define output-empty-score-list #f)
#(ly:add-file-name-alist '(%s
))\n
""" % '\n'.join(['("%s.ly" . "%s")\n' % (ly.basename (), name)
for ly in lys]))
def split_output_files(directory):
"""Returns directory entries in DIRECTORY/XX/ , where XX are hex digits.
Return value is a set of strings.
"""
files = []
for subdir in glob.glob (os.path.join (directory, '[a-f0-9][a-f0-9]')):
base_subdir = os.path.split (subdir)[1]
sub_files = [os.path.join (base_subdir, name)
for name in os.listdir (subdir)]
files += sub_files
return set (files)
def do_process_cmd (chunks, input_name, options):
snippets = [c for c in chunks if isinstance (c, BookSnippet.LilypondSnippet)]
output_files = split_output_files (options.lily_output_dir)
outdated = [c for c in snippets if c.is_outdated (options.lily_output_dir, output_files)]
write_file_map (outdated, input_name)
progress (_ ("Writing snippets..."))
for snippet in outdated:
snippet.write_ly()
progress ('\n')
if outdated:
progress (_ ("Processing..."))
progress ('\n')
process_snippets (options.process_cmd, outdated,
options.formatter, options.lily_output_dir)
else:
progress (_ ("All snippets are up to date..."))
if options.lily_output_dir != options.output_dir:
output_files = split_output_files (options.lily_output_dir)
for snippet in snippets:
snippet.link_all_output_files (options.lily_output_dir,
output_files,
options.output_dir)
progress ('\n')
###
# Format guessing data
def guess_format (input_filename):
format = None
e = os.path.splitext (input_filename)[1]
for formatter in BookBase.all_formats:
if formatter.can_handle_extension (e):
return formatter
error (_ ("cannot determine format for: %s" % input_filename))
exit (1)
def write_if_updated (file_name, lines):
try:
f = file (file_name)
oldstr = f.read ()
new_str = ''.join (lines)
if oldstr == new_str:
progress (_ ("%s is up to date.") % file_name)
progress ('\n')
# this prevents make from always rerunning lilypond-book:
# output file must be touched in order to be up to date
os.utime (file_name, None)
return
except:
pass
output_dir = os.path.dirname (file_name)
if not os.path.exists (output_dir):
os.makedirs (output_dir)
progress (_ ("Writing `%s'...") % file_name)
file (file_name, 'w').writelines (lines)
progress ('\n')
def note_input_file (name, inputs=[]):
## hack: inputs is mutable!
inputs.append (name)
return inputs
def samefile (f1, f2):
try:
return os.path.samefile (f1, f2)
except AttributeError: # Windoze
f1 = re.sub ("//*", "/", f1)
f2 = re.sub ("//*", "/", f2)
return f1 == f2
def do_file (input_filename, included=False):
# Ugh.
input_absname = input_filename
if not input_filename or input_filename == '-':
in_handle = sys.stdin
input_fullname = '<stdin>'
else:
if os.path.exists (input_filename):
input_fullname = input_filename
else:
input_fullname = global_options.formatter.input_fullname (input_filename)
# Normalize path to absolute path, since we will change cwd to the output dir!
# Otherwise, "lilypond-book -o out test.tex" will complain that it is
# overwriting the input file (which it is actually not), since the
# input filename is relative to the CWD...
input_absname = os.path.abspath (input_fullname)
note_input_file (input_fullname)
in_handle = file (input_fullname)
if input_filename == '-':
input_base = 'stdin'
elif included:
input_base = os.path.splitext (input_filename)[0]
else:
input_base = os.path.basename (
os.path.splitext (input_filename)[0])
# don't complain when global_options.output_dir is existing
if not global_options.output_dir:
global_options.output_dir = os.getcwd()
else:
global_options.output_dir = os.path.abspath(global_options.output_dir)
if not os.path.isdir (global_options.output_dir):
os.mkdir (global_options.output_dir, 0777)
os.chdir (global_options.output_dir)
output_filename = os.path.join(global_options.output_dir,
input_base + global_options.formatter.default_extension)
if (os.path.exists (input_filename)
and os.path.exists (output_filename)
and samefile (output_filename, input_absname)):
error (
_ ("Output would overwrite input file; use --output."))
exit (2)
try:
progress (_ ("Reading %s...") % input_fullname)
source = in_handle.read ()
progress ('\n')
if not included:
global_options.formatter.init_default_snippet_options (source)
progress (_ ("Dissecting..."))
chunks = find_toplevel_snippets (source, global_options.formatter)
# Let the formatter modify the chunks before further processing
chunks = global_options.formatter.process_chunks (chunks)
progress ('\n')
if global_options.filter_cmd:
write_if_updated (output_filename,
[c.filter_text () for c in chunks])
elif global_options.process_cmd:
do_process_cmd (chunks, input_fullname, global_options)
progress (_ ("Compiling %s...") % output_filename)
progress ('\n')
write_if_updated (output_filename,
[s.replacement_text ()
for s in chunks])
def process_include (snippet):
os.chdir (original_dir)
name = snippet.substring ('filename')
progress (_ ("Processing include: %s") % name)
progress ('\n')
return do_file (name, included=True)
include_chunks = map (process_include,
filter (lambda x: isinstance (x, BookSnippet.IncludeSnippet),
chunks))
return chunks + reduce (lambda x, y: x + y, include_chunks, [])
except BookSnippet.CompileError:
os.chdir (original_dir)
progress (_ ("Removing `%s'") % output_filename)
progress ('\n')
raise BookSnippet.CompileError
def do_options ():
global global_options
opt_parser = get_option_parser()
(global_options, args) = opt_parser.parse_args ()
global_options.information = {'program_version': ly.program_version, 'program_name': ly.program_name }
global_options.include_path = map (os.path.abspath, global_options.include_path)
# Load the python packages (containing e.g. custom formatter classes)
# passed on the command line
nr = 0
for i in global_options.custom_packages:
nr += 1
print imp.load_source ("book_custom_package%s" % nr, i)
if global_options.warranty:
warranty ()
exit (0)
if not args or len (args) > 1:
opt_parser.print_help ()
exit (2)
return args
def main ():
# FIXME: 85 lines of `main' macramee??
files = do_options ()
basename = os.path.splitext (files[0])[0]
basename = os.path.split (basename)[1]
if global_options.format:
# Retrieve the formatter for the given format
for formatter in BookBase.all_formats:
if formatter.can_handle_format (global_options.format):
global_options.formatter = formatter
else:
global_options.formatter = guess_format (files[0])
global_options.format = global_options.formatter.format
# make the global options available to the formatters:
global_options.formatter.global_options = global_options
formats = global_options.formatter.image_formats
if global_options.process_cmd == '':
global_options.process_cmd = (lilypond_binary
+ ' --formats=%s -dbackend=eps ' % formats)
if global_options.process_cmd:
includes = global_options.include_path
if global_options.lily_output_dir:
# This must be first, so lilypond prefers to read .ly
# files in the other lybookdb dir.
includes = [os.path.abspath(global_options.lily_output_dir)] + includes
global_options.process_cmd += ' '.join ([' -I %s' % ly.mkarg (p)
for p in includes])
global_options.formatter.process_options (global_options)
if global_options.verbose:
global_options.process_cmd += " --verbose "
if global_options.padding_mm:
global_options.process_cmd += " -deps-box-padding=%f " % global_options.padding_mm
global_options.process_cmd += " -dread-file-list -dno-strip-output-dir"
if global_options.lily_output_dir:
global_options.lily_output_dir = os.path.abspath(global_options.lily_output_dir)
if not os.path.isdir (global_options.lily_output_dir):
os.makedirs (global_options.lily_output_dir)
else:
global_options.lily_output_dir = os.path.abspath(global_options.output_dir)
identify ()
try:
chunks = do_file (files[0])
except BookSnippet.CompileError:
exit (1)
inputs = note_input_file ('')
inputs.pop ()
base_file_name = os.path.splitext (os.path.basename (files[0]))[0]
dep_file = os.path.join (global_options.output_dir, base_file_name + '.dep')
final_output_file = os.path.join (global_options.output_dir,
base_file_name
+ '.%s' % global_options.format)
os.chdir (original_dir)
file (dep_file, 'w').write ('%s: %s'
% (final_output_file, ' '.join (inputs)))
if __name__ == '__main__':
main ()
|