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 ly_new_version_re = re.compile (r'\\version\s*"(.+?)"')
71 # add tags to ly files from LSR
72 def add_tags (ly_code, tags):
73 return begin_header_re.sub ('\\g<0>\n lsrtags = "' + tags + '"\n', ly_code, 1)
75 # for snippets from input/new, add message for earliest working version
76 def add_version (ly_code):
77 return '''%% Note: this file works from version ''' + ly_new_version_re.search (ly_code).group (1) + '\n'
79 def copy_ly (srcdir, name, tags):
82 dest = os.path.join (DEST, name)
83 tags = ', '.join (tags)
84 s = open (os.path.join (srcdir, name)).read ()
86 texidoc_translations_path = os.path.join (TEXIDOCS,
87 os.path.splitext (name)[0] + '.texidoc')
88 if os.path.exists (texidoc_translations_path):
89 texidoc_translations = open (texidoc_translations_path).read ()
90 # Since we want to insert the translations verbatim using a
91 # regexp, \\ is understood as ONE escaped backslash. So we have
92 # to escape those backslashes once more...
93 texidoc_translations = texidoc_translations.replace ('\\', '\\\\')
94 s = begin_header_re.sub ('\\g<0>\n' + texidoc_translations, s, 1)
96 if in_dir and in_dir in srcdir:
97 s = LY_HEADER_LSR + add_tags (s, tags)
99 s = LY_HEADER_NEW + add_version (s) + s
101 s = mark_verbatim_section (s)
102 s = lsr_comment_re.sub ('', s)
103 open (dest, 'w').write (s)
105 e = os.system ("convert-ly -e '%s'" % dest)
107 unconverted.append (dest)
108 if os.path.exists (dest + '~'):
109 os.remove (dest + '~')
110 # no need to check snippets from input/new
111 if in_dir and in_dir in srcdir:
112 # -V seems to make unsafe snippets fail nicer/sooner
113 e = os.system ("lilypond -V -dno-print-pages -dsafe -o /tmp/lsrtest '%s'" % dest)
117 def read_source_with_dirs (src):
121 srcdir = os.path.join (src, tag)
122 l[tag] = set (map (os.path.basename,
123 glob.glob (os.path.join (srcdir, '*.ly'))))
128 s[f] = (srcdir, [tag])
132 tags_re = re.compile ('lsrtags\\s*=\\s*"(.+?)"')
134 def read_source (src):
136 l = dict ([(tag, set()) for tag in TAGS])
137 for f in glob.glob (os.path.join (src, '*.ly')):
138 basename = os.path.basename (f)
139 m = tags_re.search (open (f, 'r').read ())
141 file_tags = [tag.strip() for tag in m.group (1). split(',')]
142 s[basename] = (src, file_tags)
143 [l[tag].add (basename) for tag in file_tags if tag in TAGS]
145 notags_files.append (f)
149 def dump_file_list (file, file_list, update=False):
151 old_list = set (open (file, 'r').read ().splitlines ())
152 old_list.update (file_list)
153 new_list = list (old_list)
157 f.write ('\n'.join (sorted (new_list)) + '\n')
160 ## clean out existing lys and generated files
161 map (os.remove, glob.glob (os.path.join (DEST, '*.ly')) +
162 glob.glob (os.path.join (DEST, '*.snippet-list')))
164 # read LSR source where tags are defined by subdirs
165 snippets, tag_lists = read_source_with_dirs (in_dir)
167 # read input/new where tags are directly defined
168 s, l = read_source (NEW_LYS)
171 tag_lists[t].update (l[t])
173 snippets, tag_lists = read_source (NEW_LYS)
175 for (name, (srcdir, tags)) in snippets.items ():
176 copy_ly (srcdir, name, tags)
177 for (tag, file_set) in tag_lists.items ():
178 dump_file_list (os.path.join (DEST, tag + '.snippet-list'),
179 file_set, update=not(in_dir))
181 sys.stderr.write ('These files could not be converted successfully by convert-ly:\n')
182 sys.stderr.write ('\n'.join (unconverted) + '\n\n')
184 sys.stderr.write ('No tags could be found in these files:\n')
185 sys.stderr.write ('\n'.join (notags_files) + '\n\n')
187 dump_file_list ('lsr-unsafe.txt', unsafe)
188 sys.stderr.write ('''
190 Unsafe files printed in lsr-unsafe.txt: CHECK MANUALLY!
191 git add input/lsr/*.ly
192 xargs git diff HEAD < lsr-unsafe.txt