8 USAGE = ''' Usage: makelsr.py LSR_SNIPPETS_DIR
9 This script must be run from top of the source tree;
10 it updates snippets input/lsr with snippets in input/new or LSR_SNIPPETS_DIR.
13 LY_HEADER_LSR = '''%%%% Do not edit this file; it is auto-generated from LSR http://lsr.dsi.unimi.it
14 %%%% This file is in the public domain.
18 LY_HEADER_NEW = '''%%%% Do not edit this file; it is auto-generated from input/new
22 DEST = os.path.join ('input', 'lsr')
23 NEW_LYS = os.path.join ('input', 'new')
27 TAGS.extend (['pitches', 'rhythms', 'expressive-marks',
28 'repeats', 'simultaneous-notes', 'staff-notation',
29 'editorial-and-educational-use', 'text'])
31 TAGS.extend (['vocal-music', 'chords', 'piano-music',
32 'percussion', 'guitar', 'strings', 'bagpipes', 'ancient-notation'])
35 TAGS.extend (['contexts-and-engravers', 'tweaks-and-overrides',
36 'paper-and-layout', 'breaks', 'spacing', 'midi', 'titles', 'template', 'other'])
38 def exit_with_usage (n=0):
39 sys.stderr.write (USAGE)
47 if not (os.path.isdir (DEST) and os.path.isdir (NEW_LYS)):
53 # mark the section that will be printed verbatim by lilypond-book
54 end_header_re = re.compile ('(\\header {.*?}\n)\n', re.M | re.S)
56 def mark_verbatim_section (ly_code):
57 return end_header_re.sub ('\\1% begin verbatim\n', ly_code)
59 def copy_ly (srcdir, name, tags):
62 dest = os.path.join (DEST, name)
68 f.write (h % ', '.join (tags))
69 f.write (mark_verbatim_section (open (os.path.join (srcdir, name)).read ()))
71 e = os.system('convert-ly -e ' + dest)
73 unconverted.append (dest)
74 if os.path.exists (dest + '~'):
75 os.remove (dest + '~')
76 # -V seems to make unsafe snippets fail nicer/sooner
77 e = os.system ('nice lilypond -V -dno-print-pages -dsafe -o /tmp/lsrtest ' + dest)
81 def read_source (src):
85 srcdir = os.path.join (src, tag)
86 l[tag] = set (map (os.path.basename, glob.glob (os.path.join (srcdir, '*.ly'))))
91 s[f] = (srcdir, [tag])
94 def dump_file_list (file, list):
96 f.write ('\n'.join (list) + '\n')
98 ## clean out existing lys and generated files
99 map (os.remove, glob.glob (os.path.join (DEST, '*.ly')) +
100 glob.glob (os.path.join (DEST, '*.snippet-list')))
103 snippets, tag_lists = read_source (in_dir)
105 s, l = read_source (NEW_LYS)
108 tag_lists[t].update (l[t])
110 for (name, (srcdir, tags)) in snippets.items ():
111 copy_ly (srcdir, name, tags)
113 for (tag, file_set) in tag_lists.items ():
114 dump_file_list (os.path.join (DEST, tag + '.snippet-list'), file_set)
117 sys.stderr.write ('These files could not be converted successfully by convert-ly:\n')
118 sys.stderr.write ('\n'.join (unconverted))
120 dump_file_list ('lsr-unsafe.txt', unsafe)
121 sys.stderr.write ('''
123 Unsafe files printed in lsr-unsafe.txt: CHECK MANUALLY!
124 xargs git-diff < lsr-unsafe.txt