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.
11 If a snippet is present in both directories, the one from input/new is preferred.
14 LY_HEADER_LSR = '''%% Do not edit this file; it is auto-generated from LSR http://lsr.dsi.unimi.it
15 %% This file is in the public domain.
18 LY_HEADER_NEW = '''%% Do not edit this file; it is auto-generated from input/new
19 %% This file is in the public domain.
22 DEST = os.path.join ('input', 'lsr')
23 NEW_LYS = os.path.join ('input', 'new')
24 TEXIDOCS = os.path.join ('input', 'texidocs')
28 TAGS.extend (['pitches', 'rhythms', 'expressive-marks',
29 'repeats', 'simultaneous-notes', 'staff-notation',
30 'editorial-annotations', 'text'])
32 TAGS.extend (['vocal-music', 'chords', 'keyboards',
33 'percussion', 'fretted-strings', 'unfretted-strings',
34 'ancient-notation', 'winds', 'world-music'
38 TAGS.extend (['contexts-and-engravers', 'tweaks-and-overrides',
39 'paper-and-layout', 'breaks', 'spacing', 'midi', 'titles', 'template'])
41 def exit_with_usage (n=0):
42 sys.stderr.write (USAGE)
45 if len (sys.argv) >= 2:
47 if len (sys.argv) >= 3:
49 if not (os.path.isdir (DEST) and os.path.isdir (NEW_LYS)):
58 # mark the section that will be printed verbatim by lilypond-book
59 end_header_re = re.compile ('(\\header {.+?doctitle = ".+?})\n', re.M | re.S)
61 def mark_verbatim_section (ly_code):
62 return end_header_re.sub ('\\1 % begin verbatim\n\n', ly_code, 1)
64 # '% LSR' comments are to be stripped
65 lsr_comment_re = re.compile (r'\s*%+\s*LSR.*')
67 begin_header_re = re.compile (r'\\header\s*{', re.M)
69 # add tags to ly files from LSR
70 def add_tags (ly_code, tags):
71 return begin_header_re.sub ('\\g<0>\n lsrtags = "' + tags + '"\n', ly_code, 1)
73 def copy_ly (srcdir, name, tags):
76 dest = os.path.join (DEST, name)
77 tags = ', '.join (tags)
78 s = open (os.path.join (srcdir, name)).read ()
80 texidoc_translations_path = os.path.join (TEXIDOCS,
81 os.path.splitext (name)[0] + '.texidoc')
82 if os.path.exists (texidoc_translations_path):
83 texidoc_translations = open (texidoc_translations_path).read ()
84 # Since we want to insert the translations verbatim using a
85 # regexp, \\ is understood as ONE escaped backslash. So we have
86 # to escape those backslashes once more...
87 texidoc_translations = texidoc_translations.replace ('\\', '\\\\')
88 s = begin_header_re.sub ('\\g<0>\n' + texidoc_translations, s, 1)
90 if in_dir and in_dir in srcdir:
91 s = LY_HEADER_LSR + add_tags (s, tags)
95 s = mark_verbatim_section (s)
96 s = lsr_comment_re.sub ('', s)
97 open (dest, 'w').write (s)
99 e = os.system ("convert-ly -e '%s'" % dest)
101 unconverted.append (dest)
102 if os.path.exists (dest + '~'):
103 os.remove (dest + '~')
104 # -V seems to make unsafe snippets fail nicer/sooner
105 e = os.system ("lilypond -V -dno-print-pages -dsafe -o /tmp/lsrtest '%s'" % dest)
109 def read_source_with_dirs (src):
113 srcdir = os.path.join (src, tag)
114 l[tag] = set (map (os.path.basename, glob.glob (os.path.join (srcdir, '*.ly'))))
119 s[f] = (srcdir, [tag])
123 tags_re = re.compile ('lsrtags\\s*=\\s*"(.+?)"')
125 def read_source (src):
127 l = dict ([(tag, set()) for tag in TAGS])
128 for f in glob.glob (os.path.join (src, '*.ly')):
129 basename = os.path.basename (f)
130 m = tags_re.search (open (f, 'r').read ())
132 file_tags = [tag.strip() for tag in m.group (1). split(',')]
133 s[basename] = (src, file_tags)
134 [l[tag].add (basename) for tag in file_tags if tag in TAGS]
136 notags_files.append (f)
140 def dump_file_list (file, file_list, update=False):
142 old_list = set (open (file, 'r').read ().splitlines ())
143 old_list.update (file_list)
144 new_list = list (old_list)
148 f.write ('\n'.join (sorted (new_list)) + '\n')
151 ## clean out existing lys and generated files
152 map (os.remove, glob.glob (os.path.join (DEST, '*.ly')) +
153 glob.glob (os.path.join (DEST, '*.snippet-list')))
155 # read LSR source where tags are defined by subdirs
156 snippets, tag_lists = read_source_with_dirs (in_dir)
158 # read input/new where tags are directly defined
159 s, l = read_source (NEW_LYS)
162 tag_lists[t].update (l[t])
164 snippets, tag_lists = read_source (NEW_LYS)
166 for (name, (srcdir, tags)) in snippets.items ():
167 copy_ly (srcdir, name, tags)
168 for (tag, file_set) in tag_lists.items ():
169 dump_file_list (os.path.join (DEST, tag + '.snippet-list'), file_set, update=not(in_dir))
171 sys.stderr.write ('These files could not be converted successfully by convert-ly:\n')
172 sys.stderr.write ('\n'.join (unconverted) + '\n\n')
174 sys.stderr.write ('No tags could be found in these files:\n')
175 sys.stderr.write ('\n'.join (notags_files) + '\n\n')
177 dump_file_list ('lsr-unsafe.txt', unsafe)
178 sys.stderr.write ('''
180 Unsafe files printed in lsr-unsafe.txt: CHECK MANUALLY!
181 git add input/lsr/*.ly
182 xargs git diff HEAD < lsr-unsafe.txt