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 = '''%%%% Do not edit this file; it is auto-generated from LSR!
17 DEST = os.path.join ('input', 'lsr')
18 NEW_LYS = os.path.join ('input', 'new')
22 TAGS.extend (['pitches', 'rhythms', 'expressive-marks',
23 'repeats', 'simultaneous-notes', 'staff-notation', 'editorial-and-educational-use', 'text'])
25 TAGS.extend (['vocal-music', 'chords', 'piano-music',
26 'percussion', 'guitar', 'strings', 'bagpipes', 'ancient-notation'])
29 TAGS.extend (['contexts-and-engravers', 'tweaks-and-overrides', 'paper-and-layout', 'breaks',
30 'spacing', 'midi', 'titles', 'other'])
32 def exit_with_usage (n=0):
33 sys.stderr.write (USAGE)
41 if not (os.path.isdir (DEST) and os.path.isdir (NEW_LYS)):
47 # mark the section that will be printed verbatim by lilypond-book
48 end_header_re = re.compile ('(\\header {.*?}\n)\n', re.M | re.S)
50 def mark_verbatim_section (ly_code):
51 return end_header_re.sub ('\\1% begin verbatim\n', ly_code)
53 def copy_ly (srcdir, name, tags):
56 dest = os.path.join (DEST, name)
58 f.write (LY_HEADER % ', '.join (tags))
59 f.write (mark_verbatim_section (open (os.path.join (srcdir, name)).read ()))
61 e = os.system('convert-ly -e ' + dest)
63 unconverted.append (dest)
64 if os.path.exists (dest + '~'):
65 os.remove (dest + '~')
66 # -V seems to make unsafe snippets fail nicer/sooner
67 e = os.system ('nice lilypond -V -dno-print-pages -dsafe -o /tmp/lsrtest ' + dest)
71 def read_source (src):
75 srcdir = os.path.join (src, tag)
76 l[tag] = set (map (os.path.basename, glob.glob (os.path.join (srcdir, '*.ly'))))
81 s[f] = (srcdir, [tag])
84 def dump_file_list (file, list):
86 f.write ('\n'.join (list) + '\n')
88 ## clean out existing lys and generated files
89 map (os.remove, glob.glob (os.path.join (DEST, '*.ly')) +
90 glob.glob (os.path.join (DEST, '*.snippet-list')))
93 snippets, tag_lists = read_source (in_dir)
95 s, l = read_source (NEW_LYS)
98 tag_lists[t].update (l[t])
100 for (name, (srcdir, tags)) in snippets.items ():
101 copy_ly (srcdir, name, tags)
103 for (tag, file_set) in tag_lists.items ():
104 dump_file_list (os.path.join (DEST, tag + '.snippet-list'), file_set)
107 sys.stderr.write ('These files could not be converted successfully by convert-ly:\n')
108 sys.stderr.write ('\n'.join (unconverted))
110 dump_file_list ('lsr-unsafe.txt', unsafe)
111 sys.stderr.write ('''
113 Unsafe files printed in lsr-unsafe.txt: CHECK MANUALLY!
114 xargs git-diff < lsr-unsafe.txt