8 os.environ['PYTHONPATH'] += ':python'
11 DEST = os.path.join ('Documentation', 'snippets')
12 NEW_LYS = os.path.join ('Documentation', 'snippets', 'new')
13 TEXIDOCS = [os.path.join ('Documentation', language_code, 'texidocs')
14 for language_code in langdefs.LANGDICT]
16 USAGE = ''' Usage: makelsr.py [LSR_SNIPPETS_DIR]
17 This script must be run from top of the source tree;
18 it updates snippets %(DEST)s with snippets
19 from %(NEW_LYS)s or LSR_SNIPPETS_DIR.
20 If a snippet is present in both directories, the one
21 from %(NEW_LYS)s is preferred.
24 LY_HEADER_LSR = '''%% Do not edit this file; it is automatically
25 %% generated from LSR http://lsr.dsi.unimi.it
26 %% This file is in the public domain.
29 LY_HEADER_NEW = '''%% Do not edit this file; it is automatically
31 %% This file is in the public domain.
36 TAGS.extend (['pitches', 'rhythms', 'expressive-marks',
37 'repeats', 'simultaneous-notes', 'staff-notation',
38 'editorial-annotations', 'text'])
40 TAGS.extend (['vocal-music', 'chords', 'keyboards',
41 'percussion', 'fretted-strings', 'unfretted-strings',
42 'ancient-notation', 'winds', 'world-music'
46 TAGS.extend (['contexts-and-engravers', 'tweaks-and-overrides',
47 'paper-and-layout', 'breaks', 'spacing', 'midi', 'titles', 'template'])
49 def exit_with_usage (n=0):
50 sys.stderr.write (USAGE)
53 if len (sys.argv) >= 2:
55 if len (sys.argv) >= 3:
57 if not (os.path.isdir (DEST) and os.path.isdir (NEW_LYS)):
66 # mark the section that will be printed verbatim by lilypond-book
67 end_header_re = re.compile ('(\\header {.+?doctitle = ".+?})\n', re.M | re.S)
69 def mark_verbatim_section (ly_code):
70 return end_header_re.sub ('\\1 % begin verbatim\n\n', ly_code, 1)
72 # '% LSR' comments are to be stripped
73 lsr_comment_re = re.compile (r'\s*%+\s*LSR.*')
75 begin_header_re = re.compile (r'\\header\s*{', re.M)
77 ly_new_version_re = re.compile (r'\\version\s*"(.+?)"')
79 # add tags to ly files from LSR
80 def add_tags (ly_code, tags):
81 return begin_header_re.sub ('\\g<0>\n lsrtags = "' + tags + '"\n',
84 # for snippets from input/new, add message for earliest working version
85 def add_version (ly_code):
86 return '''%% Note: this file works from version ''' + \
87 ly_new_version_re.search (ly_code).group (1) + '\n'
89 def copy_ly (srcdir, name, tags):
92 dest = os.path.join (DEST, name)
93 tags = ', '.join (tags)
94 s = open (os.path.join (srcdir, name)).read ()
97 texidoc_translation_path = \
98 os.path.join (path, os.path.splitext (name)[0] + '.texidoc')
99 if os.path.exists (texidoc_translation_path):
100 texidoc_translation = open (texidoc_translation_path).read ()
101 # Since we want to insert the translations verbatim using a
102 # regexp, \\ is understood as ONE escaped backslash. So we have
103 # to escape those backslashes once more...
104 texidoc_translation = texidoc_translation.replace ('\\', '\\\\')
105 s = begin_header_re.sub ('\\g<0>\n' + texidoc_translation, s, 1)
107 if in_dir and in_dir in srcdir:
108 s = LY_HEADER_LSR + add_tags (s, tags)
110 s = LY_HEADER_NEW + add_version (s) + s
112 s = mark_verbatim_section (s)
113 s = lsr_comment_re.sub ('', s)
114 open (dest, 'w').write (s)
116 e = os.system ("convert-ly -e '%s'" % dest)
118 unconverted.append (dest)
119 if os.path.exists (dest + '~'):
120 os.remove (dest + '~')
121 # no need to check snippets from input/new
122 if in_dir and in_dir in srcdir:
123 # -V seems to make unsafe snippets fail nicer/sooner
124 e = os.system ("lilypond -V -dno-print-pages -dsafe -o /tmp/lsrtest '%s'" % dest)
128 def read_source_with_dirs (src):
132 srcdir = os.path.join (src, tag)
133 l[tag] = set (map (os.path.basename,
134 glob.glob (os.path.join (srcdir, '*.ly'))))
139 s[f] = (srcdir, [tag])
143 tags_re = re.compile ('lsrtags\\s*=\\s*"(.+?)"')
145 def read_source (src):
147 l = dict ([(tag, set()) for tag in TAGS])
148 for f in glob.glob (os.path.join (src, '*.ly')):
149 basename = os.path.basename (f)
150 m = tags_re.search (open (f, 'r').read ())
152 file_tags = [tag.strip() for tag in m.group (1). split(',')]
153 s[basename] = (src, file_tags)
154 [l[tag].add (basename) for tag in file_tags if tag in TAGS]
156 notags_files.append (f)
160 def dump_file_list (file, file_list, update=False):
162 old_list = set (open (file, 'r').read ().splitlines ())
163 old_list.update (file_list)
164 new_list = list (old_list)
168 f.write ('\n'.join (sorted (new_list)) + '\n')
171 ## clean out existing lys and generated files
172 map (os.remove, glob.glob (os.path.join (DEST, '*.ly')) +
173 glob.glob (os.path.join (DEST, '*.snippet-list')))
175 # read LSR source where tags are defined by subdirs
176 snippets, tag_lists = read_source_with_dirs (in_dir)
178 # read input/new where tags are directly defined
179 s, l = read_source (NEW_LYS)
182 tag_lists[t].update (l[t])
184 snippets, tag_lists = read_source (NEW_LYS)
186 for (name, (srcdir, tags)) in snippets.items ():
187 copy_ly (srcdir, name, tags)
188 for (tag, file_set) in tag_lists.items ():
189 dump_file_list (os.path.join (DEST, tag + '.snippet-list'),
190 file_set, update=not(in_dir))
192 sys.stderr.write ('These files could not be converted successfully by convert-ly:\n')
193 sys.stderr.write ('\n'.join (unconverted) + '\n\n')
195 sys.stderr.write ('No tags could be found in these files:\n')
196 sys.stderr.write ('\n'.join (notags_files) + '\n\n')
198 dump_file_list ('lsr-unsafe.txt', unsafe)
199 sys.stderr.write ('''
201 Unsafe files printed in lsr-unsafe.txt: CHECK MANUALLY!
203 xargs git diff HEAD < lsr-unsafe.txt