10 lilypond_flags = "-dno-print-pages -dsafe"
12 lys_from_lsr = os.path.join ('Documentation', 'snippets')
13 new_lys = os.path.join ('Documentation', 'snippets', 'new')
14 ly_output = os.path.join (tempfile.gettempdir (), 'lsrtest')
16 # which convert-ly and lilypond to use
17 P = os.path.join (os.environ.get ("LILYPOND_BUILD_DIR", ""),
19 if os.path.isfile (P):
20 conv_path = os.path.dirname (P)
21 elif os.path.isfile ("build/out/bin/convert-ly"):
22 conv_path = "build/out/bin/"
25 convert_ly = os.path.join (conv_path, 'convert-ly')
26 lilypond_bin = os.path.join (conv_path, 'lilypond')
30 LY_HEADER_LSR = '''%% DO NOT EDIT this file manually; it is automatically
31 %% generated from LSR http://lsr.dsi.unimi.it
32 %% Make any changes in LSR itself, or in Documentation/snippets/new/ ,
33 %% and then run scripts/auxiliar/makelsr.py
35 %% This file is in the public domain.
38 LY_HEADER_NEW = '''%% DO NOT EDIT this file manually; it is automatically
40 %% Make any changes in Documentation/snippets/new/
41 %% and then run scripts/auxiliar/makelsr.py
43 %% This file is in the public domain.
46 options_parser = optparse.OptionParser (
47 description = "makelsr - update snippets directory from LSR",
48 usage = '''%%prog [options] [LSR_SNIPPETS_DIR]
49 Unless -s option is specified, this script must be run from top of the
50 source tree. If LSR_SNIPPETS_DIR is not specified, it defaults to
53 Remove snippets in TOP_SOURCE_DIR/%(lys_from_lsr)s and put in snippets
54 from LSR_SNIPPETS_DIR run through convert-ly or from
55 TOP_SOURCE_DIR/%(new_lys)s; if a snippet is present in both
56 directories, the one from TOP_SOURCE_DIR/%(new_lys)s is preferred.
57 All written snippets are copied in LY_OUTPUT
58 with appending translations from .texidoc files and are tested with
59 lilypond with flags %(lilypond_flags)s
63 options_parser.add_option ('-s', '--top-source',
64 dest="top_source_dir",
66 metavar="TOP_SOURCE_DIR",
68 help="set LilyPond top source directory")
70 options_parser.add_option ('-o', '--ly-output',
75 help="set LilyPond output files temporary directory")
77 options_parser.add_option ('-p', '--path',
82 help="directory where looking for LilyPond binaries")
84 options_parser.add_option ('-c', '--convert-ly',
88 default="LY_PATH/convert-ly",
89 help="convert-ly binary to use")
91 options_parser.add_option ('-l', '--lilypond-binary',
94 metavar="LILYPOND_BIN",
95 default="LY_PATH/lilypond",
96 help="lilypond binary to use")
98 (options, args) = options_parser.parse_args ()
100 if not os.path.isdir (options.top_source_dir):
101 sys.stderr.write ("Error: top source: %s: not a directory\n" % options.top_source_dir)
104 lys_from_lsr = os.path.normpath (os.path.join (options.top_source_dir, lys_from_lsr))
105 new_lys = os.path.normpath (os.path.join (options.top_source_dir, new_lys))
106 sys.path.append (os.path.normpath (os.path.join (options.top_source_dir, 'python')))
109 os.path.normpath (os.path.join (options.top_source_dir, 'Documentation', language_code, 'texidocs'))
110 for language_code in langdefs.LANGDICT]
112 if not os.path.isdir (lys_from_lsr):
113 sys.stderr.write ("Error: snippets path: %s: not a directory\n" % lys_from_lsr)
115 if not os.path.isdir (new_lys):
116 sys.stderr.write ("Error: new snippets path: %s: not a directory\n" % lys_from_lsr)
120 if os.path.isdir (options.ly_output):
121 ly_output = options.ly_output
123 elif os.path.exists (options.ly_output):
125 os.unlink (options.ly_output)
126 except Exception as e:
127 sys.stderr.write ("Warning: could not delete file before creating directory: %s\n" % e)
130 os.makedirs (options.ly_output)
131 except Exception as e:
132 sys.stderr.write ("Warning: could not create directory: %s\n" % e)
134 ly_output = options.ly_output
138 os.makedirs (options.ly_output)
139 except Exception as e:
140 sys.stderr.write ("Warning: could not create directory: %s\n" % e)
142 ly_output = options.ly_output
145 ly_output = tempfile.gettempdir ()
146 sys.stderr.write ("Warning: could not use or create directory %s, using default %s\n" % (options.ly_output, ly_output))
148 def exit_with_usage (n=0):
149 options_parser.print_help (sys.stderr)
155 if not (os.path.isdir (in_dir)):
156 sys.stderr.write ("Error: %s: not a directory\n" % in_dir)
160 tags = os.listdir (in_dir)
161 ## Make sure all users get the same ordering of tags
166 if options.convert_ly == "LY_PATH/convert-ly":
167 convert_ly = os.path.join (options.bin_path, "convert-ly")
169 convert_ly = options.convert_ly
170 if not os.path.exists (convert_ly):
171 sys.stderr.write ("Warning: %s: no such file\n" % convert_ly)
172 convert_ly = "convert-ly"
173 if options.lilypond_bin == "LY_PATH/lilypond":
174 lilypond_bin = os.path.join (options.bin_path, "lilypond")
176 lilypond_bin = options.lilypond_bin
177 if not os.path.exists (lilypond_bin):
178 sys.stderr.write ("Warning: %s: no such file\n" % lilypond_bin)
179 lilypond_bin = "lilypond"
180 sys.stderr.write ("Using %s, %s\n" % (convert_ly, lilypond_bin))
186 # mark the section that will be printed verbatim by lilypond-book
187 end_header_re = re.compile ('(\\header {.+?doctitle = ".+?})\n', re.M | re.S)
189 doctitle_re = re.compile (r'(doctitle[a-zA-Z_]{0,6}\s*=\s*")((?:\\"|[^"\n])*)"')
190 texinfo_q_re = re.compile (r'@q{(.*?)}')
191 texinfo_qq_re = re.compile (r'@qq{(.*?)}')
192 def doctitle_sub (title_match):
193 # Comma forbidden in Texinfo node name
194 title = title_match.group (2).replace (',', '')
195 title = texinfo_q_re.sub (r"`\1'", title)
196 title = texinfo_qq_re.sub (r'\"\1\"', title)
197 return title_match.group (1) + title + '"'
199 def mark_verbatim_section (ly_code):
200 return end_header_re.sub ('\\1 % begin verbatim\n\n', ly_code, 1)
202 # '% LSR' comments are to be stripped
203 lsr_comment_re = re.compile (r'\s*%+\s*LSR.*')
204 begin_header_re = re.compile (r'\\header\s*{', re.M)
205 ly_new_version_re = re.compile (r'\\version\s*"(.+?)"')
206 strip_white_spaces_re = re.compile (r'[ \t]+(?=\n)')
207 final_empty_lines_re = re.compile (r'\n{2,}$')
209 # add tags to ly files from LSR
210 def add_tags (ly_code, tags):
211 return begin_header_re.sub ('\\g<0>\n lsrtags = "' + tags + '"\n',
214 # for snippets from input/new, add message for earliest working version
215 def add_version (ly_code):
216 return '''%% Note: this file works from version ''' + \
217 ly_new_version_re.search (ly_code).group (1) + '\n'
219 def escape_backslashes_in_header(snippet):
220 # ASSUME: the \header exists.
221 header_char_number_start = snippet.find('\header {')
222 header_char_number_end = snippet.find('} % begin verbatim')
224 header = snippet[header_char_number_start:header_char_number_end]
225 # only one level of escaping happening here
226 # thanks to raw strings
227 new_header = re.sub(r"@code\{\\([a-zA-Z])", r"@code{\\\\\1", header)
228 escaped_snippet = (snippet[:header_char_number_start] +
229 new_header + snippet[header_char_number_end:])
230 return escaped_snippet
232 def copy_ly (srcdir, name, tags):
235 dest = os.path.join (lys_from_lsr, name)
236 tags = ', '.join (tags)
237 file_path = os.path.join (srcdir, name)
238 sys.stderr.write ("\nmakelsr.py: reading %s\n" % file_path)
239 s = open (file_path).read ()
241 s = doctitle_re.sub (doctitle_sub, s)
243 s = LY_HEADER_NEW + add_version (s) + s
245 s = LY_HEADER_LSR + add_tags (s, tags)
247 s = mark_verbatim_section (s)
248 s = lsr_comment_re.sub ('', s)
249 s = strip_white_spaces_re.sub ('', s)
250 s = final_empty_lines_re.sub ('\n', s)
251 s = escape_backslashes_in_header (s)
252 sys.stderr.write ("makelsr.py: writing %s\n" % dest)
253 open (dest, 'w').write (s)
255 e = os.system (convert_ly+(" -d -e '%s'" % dest))
257 unconverted.append (dest)
258 if os.path.exists (dest + '~'):
259 os.remove (dest + '~')
260 # no need to check snippets from Documentation/snippets/new
261 if not "new" in srcdir:
264 (lilypond_bin, lilypond_flags, ly_output, dest))
268 def read_source_with_dirs (src):
272 srcdir = os.path.join (src, tag)
273 l[tag] = set (map (os.path.basename,
274 glob.glob (os.path.join (srcdir, '*.ly'))))
279 s[f] = (srcdir, [tag])
283 tags_re = re.compile ('lsrtags\\s*=\\s*"(.+?)"')
285 def read_source (src):
287 l = dict ([(tag, set()) for tag in tags])
288 for f in glob.glob (os.path.join (src, '*.ly')):
289 basename = os.path.basename (f)
290 m = tags_re.search (open (f, 'r').read ())
292 file_tags = [tag.strip() for tag in m.group (1). split(',')]
293 s[basename] = (src, file_tags)
294 [l[tag].add (basename) for tag in file_tags if tag in tags]
296 notags_files.append (f)
300 def dump_file_list (file, file_list, update=False):
302 old_list = set (open (file, 'r').read ().splitlines ())
303 old_list.update (file_list)
304 new_list = list (old_list)
308 f.write ('\n'.join (sorted (new_list)) + '\n')
310 ## clean out existing lys and generated files - but only when we're
311 ## completely recreating them from the tarball. Otherwise
312 ## tags will be empty and so we can use this to skip this step
315 map (os.remove, glob.glob (os.path.join (lys_from_lsr, '*.ly')) +
316 glob.glob (os.path.join (lys_from_lsr, '*.snippet-list')))
318 # read LSR source where tags are defined by subdirs
319 snippets, tag_lists = read_source_with_dirs (in_dir)
321 # read input/new where tags are directly defined
322 s, l = read_source (new_lys)
325 tag_lists[t].update (l[t])
327 for (name, (srcdir, file_tags)) in snippets.items ():
328 copy_ly (srcdir, name, file_tags)
329 for (tag, file_set) in tag_lists.items ():
330 dump_file_list (os.path.join (lys_from_lsr, tag + '.snippet-list'),
331 file_set, update=not(in_dir))
333 sys.stderr.write ('These files could not be converted successfully by convert-ly:\n')
334 sys.stderr.write ('\n'.join (unconverted) + '\n\n')
336 sys.stderr.write ('No tags could be found in these files:\n')
337 sys.stderr.write ('\n'.join (notags_files) + '\n\n')
339 dump_file_list ('lsr-unsafe.txt', unsafe)
340 sys.stderr.write ('''
342 Unsafe files printed in lsr-unsafe.txt: CHECK MANUALLY!
343 git add %(lys_from_lsr)s/*.ly
344 xargs git diff HEAD < lsr-unsafe.txt