]> git.donarmstrong.com Git - lilypond.git/blob - scripts/auxiliar/makelsr.py
80628cbf164e82a629227a9ea10bbdcd37cc6433
[lilypond.git] / scripts / auxiliar / makelsr.py
1 #!/usr/bin/env python
2
3 import sys
4 import os
5 import glob
6 import re
7
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.
12 '''
13
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.
16 '''
17
18 LY_HEADER_NEW = '''%% Do not edit this file; it is auto-generated from input/new
19 %% This file is in the public domain.
20 '''
21
22 DEST = os.path.join ('input', 'lsr')
23 NEW_LYS = os.path.join ('input', 'new')
24 TEXIDOCS = os.path.join ('input', 'texidocs')
25
26 TAGS = []
27 # NR 1
28 TAGS.extend (['pitches', 'rhythms', 'expressive-marks',
29 'repeats', 'simultaneous-notes', 'staff-notation',
30 'editorial-annotations', 'text'])
31 # NR 2
32 TAGS.extend (['vocal-music', 'chords', 'keyboards',
33 'percussion', 'fretted-strings', 'unfretted-strings',
34 'ancient-notation', 'winds', 'world-music'
35 ])
36
37 # other
38 TAGS.extend (['contexts-and-engravers', 'tweaks-and-overrides',
39 'paper-and-layout', 'breaks', 'spacing', 'midi', 'titles', 'template'])
40
41 def exit_with_usage (n=0):
42     sys.stderr.write (USAGE)
43     sys.exit (n)
44
45 if len (sys.argv) >= 2:
46     in_dir = sys.argv[1]
47     if len (sys.argv) >= 3:
48         exit_with_usage (2)
49     if not (os.path.isdir (DEST) and os.path.isdir (NEW_LYS)):
50         exit_with_usage (3)
51 else:
52     in_dir = ''
53
54 unsafe = []
55 unconverted = []
56 notags_files = []
57
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)
60
61 def mark_verbatim_section (ly_code):
62     return end_header_re.sub ('\\1 % begin verbatim\n\n', ly_code, 1)
63
64 # '% LSR' comments are to be stripped
65 lsr_comment_re = re.compile (r'\s*%+\s*LSR.*')
66
67 begin_header_re = re.compile (r'\\header\s*{', re.M)
68
69 ly_new_version_re = re.compile (r'\\version\s*"(.+?)"')
70
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)
74
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'
78
79 def copy_ly (srcdir, name, tags):
80     global unsafe
81     global unconverted
82     dest = os.path.join (DEST, name)
83     tags = ', '.join (tags)
84     s = open (os.path.join (srcdir, name)).read ()
85
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)
95
96     if in_dir and in_dir in srcdir:
97         s = LY_HEADER_LSR + add_tags (s, tags)
98     else:
99         s = LY_HEADER_NEW + add_version (s) + s
100
101     s = mark_verbatim_section (s)
102     s = lsr_comment_re.sub ('', s)
103     open (dest, 'w').write (s)
104
105     e = os.system ("convert-ly -e '%s'" % dest)
106     if e:
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)
114         if e:
115             unsafe.append (dest)
116
117 def read_source_with_dirs (src):
118     s = {}
119     l = {}
120     for tag in TAGS:
121         srcdir = os.path.join (src, tag)
122         l[tag] = set (map (os.path.basename,
123                            glob.glob (os.path.join (srcdir, '*.ly'))))
124         for f in l[tag]:
125             if f in s:
126                 s[f][1].append (tag)
127             else:
128                 s[f] = (srcdir, [tag])
129     return s, l
130
131
132 tags_re = re.compile ('lsrtags\\s*=\\s*"(.+?)"')
133
134 def read_source (src):
135     s = {}
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 ())
140         if m:
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]
144         else:
145             notags_files.append (f)
146     return s, l
147
148
149 def dump_file_list (file, file_list, update=False):
150     if update:
151         old_list = set (open (file, 'r').read ().splitlines ())
152         old_list.update (file_list)
153         new_list = list (old_list)
154     else:
155         new_list = file_list
156     f = open (file, 'w')
157     f.write ('\n'.join (sorted (new_list)) + '\n')
158
159 if in_dir:
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')))
163
164     # read LSR source where tags are defined by subdirs
165     snippets, tag_lists = read_source_with_dirs (in_dir)
166
167     # read input/new where tags are directly defined
168     s, l = read_source (NEW_LYS)
169     snippets.update (s)
170     for t in TAGS:
171         tag_lists[t].update (l[t])
172 else:
173     snippets, tag_lists = read_source (NEW_LYS)
174
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))
180 if unconverted:
181     sys.stderr.write ('These files could not be converted successfully by convert-ly:\n')
182     sys.stderr.write ('\n'.join (unconverted) + '\n\n')
183 if notags_files:
184     sys.stderr.write ('No tags could be found in these files:\n')
185     sys.stderr.write ('\n'.join (notags_files) + '\n\n')
186 if unsafe:
187     dump_file_list ('lsr-unsafe.txt', unsafe)
188     sys.stderr.write ('''
189
190 Unsafe files printed in lsr-unsafe.txt: CHECK MANUALLY!
191   git add input/lsr/*.ly
192   xargs git diff HEAD < lsr-unsafe.txt
193
194 ''')