]> git.donarmstrong.com Git - lilypond.git/blob - scripts/auxiliar/makelsr.py
Merge branch 'lilypond/translation' of ssh://git.sv.gnu.org/srv/git/lilypond into...
[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 sys.path.append ('python')
9 import langdefs
10
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]
15
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.
22 ''' % vars ()
23
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.
27 '''
28
29 LY_HEADER_NEW = '''%% Do not edit this file; it is automatically
30 %% generated from %s
31 %% This file is in the public domain.
32 ''' % NEW_LYS
33
34 TAGS = []
35 # NR 1
36 TAGS.extend (['pitches', 'rhythms', 'expressive-marks',
37 'repeats', 'simultaneous-notes', 'staff-notation',
38 'editorial-annotations', 'text'])
39 # NR 2
40 TAGS.extend (['vocal-music', 'chords', 'keyboards',
41 'percussion', 'fretted-strings', 'unfretted-strings',
42 'ancient-notation', 'winds', 'world-music'
43 ])
44
45 # other
46 TAGS.extend (['contexts-and-engravers', 'tweaks-and-overrides',
47 'paper-and-layout', 'breaks', 'spacing', 'midi', 'titles', 'template'])
48
49 def exit_with_usage (n=0):
50     sys.stderr.write (USAGE)
51     sys.exit (n)
52
53 if len (sys.argv) >= 2:
54     in_dir = sys.argv[1]
55     if len (sys.argv) >= 3:
56         exit_with_usage (2)
57     if not (os.path.isdir (DEST) and os.path.isdir (NEW_LYS)):
58         exit_with_usage (3)
59 else:
60     in_dir = ''
61
62 unsafe = []
63 unconverted = []
64 notags_files = []
65
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)
68
69 def mark_verbatim_section (ly_code):
70     return end_header_re.sub ('\\1 % begin verbatim\n\n', ly_code, 1)
71
72 # '% LSR' comments are to be stripped
73 lsr_comment_re = re.compile (r'\s*%+\s*LSR.*')
74 begin_header_re = re.compile (r'\\header\s*{', re.M)
75 ly_new_version_re = re.compile (r'\\version\s*"(.+?)"')
76
77 # add tags to ly files from LSR
78 def add_tags (ly_code, tags):
79     return begin_header_re.sub ('\\g<0>\n  lsrtags = "' + tags + '"\n',
80                                 ly_code, 1)
81
82 # for snippets from input/new, add message for earliest working version
83 def add_version (ly_code):
84     return '''%% Note: this file works from version ''' + \
85         ly_new_version_re.search (ly_code).group (1) + '\n'
86
87 s = 'Translation of GIT [Cc]ommittish'
88 texidoc_chunk_re = re.compile (r'^(?:%+\s*' + s + \
89     r'.+)?\s*(?:texidoc|doctitle)([a-zA-Z]{2,4})\s+=(?:.|\n)*?(?=%+\s*' + \
90     s + r'|\n\} % begin verbatim|\n  (?:doctitle|texidoc|lsrtags) |$(?!.|\n))', re.M)
91
92 def update_translated_texidoc (m, snippet_path, visited_languages):
93     base = os.path.splitext (os.path.basename (snippet_path))[0]
94     language_code = m.group (1)
95     visited_languages.append (language_code)
96     texidoc_path = os.path.join ('Documentation', language_code,
97                                  'texidocs', base + '.texidoc')
98     if os.path.isfile (texidoc_path):
99         return open (texidoc_path).read ()
100     else:
101         return m.group (0)
102
103 def copy_ly (srcdir, name, tags):
104     global unsafe
105     global unconverted
106     dest = os.path.join (DEST, name)
107     tags = ', '.join (tags)
108     s = open (os.path.join (srcdir, name)).read ()
109
110     for path in TEXIDOCS:
111         texidoc_translation_path = \
112             os.path.join (path, os.path.splitext (name)[0] + '.texidoc')
113         if os.path.exists (texidoc_translation_path):
114             texidoc_translation = open (texidoc_translation_path).read ()
115             # Since we want to insert the translations verbatim using a 
116             # regexp, \\ is understood as ONE escaped backslash. So we have
117             # to escape those backslashes once more...
118             texidoc_translation = texidoc_translation.replace ('\\', '\\\\')
119             s = begin_header_re.sub ('\\g<0>\n' + texidoc_translation, s, 1)
120
121     if in_dir and in_dir in srcdir:
122         s = LY_HEADER_LSR + add_tags (s, tags)
123     else:
124         s = LY_HEADER_NEW + add_version (s) + s
125
126     s = mark_verbatim_section (s)
127     s = lsr_comment_re.sub ('', s)
128     open (dest, 'w').write (s)
129
130     e = os.system ("convert-ly -e '%s'" % dest)
131     if e:
132         unconverted.append (dest)
133     if os.path.exists (dest + '~'):
134         os.remove (dest + '~')
135     # no need to check snippets from input/new
136     if in_dir and in_dir in srcdir:
137         # -V seems to make unsafe snippets fail nicer/sooner
138         e = os.system ("lilypond -V -dno-print-pages -dsafe -o /tmp/lsrtest '%s'" % dest)
139         if e:
140             unsafe.append (dest)
141
142 def read_source_with_dirs (src):
143     s = {}
144     l = {}
145     for tag in TAGS:
146         srcdir = os.path.join (src, tag)
147         l[tag] = set (map (os.path.basename,
148                            glob.glob (os.path.join (srcdir, '*.ly'))))
149         for f in l[tag]:
150             if f in s:
151                 s[f][1].append (tag)
152             else:
153                 s[f] = (srcdir, [tag])
154     return s, l
155
156
157 tags_re = re.compile ('lsrtags\\s*=\\s*"(.+?)"')
158
159 def read_source (src):
160     s = {}
161     l = dict ([(tag, set()) for tag in TAGS])
162     for f in glob.glob (os.path.join (src, '*.ly')):
163         basename = os.path.basename (f)
164         m = tags_re.search (open (f, 'r').read ())
165         if m:
166             file_tags = [tag.strip() for tag in m.group (1). split(',')]
167             s[basename] = (src, file_tags)
168             [l[tag].add (basename) for tag in file_tags if tag in TAGS]
169         else:
170             notags_files.append (f)
171     return s, l
172
173
174 def dump_file_list (file, file_list, update=False):
175     if update:
176         old_list = set (open (file, 'r').read ().splitlines ())
177         old_list.update (file_list)
178         new_list = list (old_list)
179     else:
180         new_list = file_list
181     f = open (file, 'w')
182     f.write ('\n'.join (sorted (new_list)) + '\n')
183
184 def update_ly_in_place (snippet_path):
185     visited_languages = []
186     contents = open (snippet_path).read ()
187     contents = texidoc_chunk_re.sub \
188         (lambda m: update_translated_texidoc (m,
189                                               snippet_path,
190                                               visited_languages),
191          contents)
192     for language_code in langdefs.LANGDICT:
193         if not language_code in visited_languages:
194             base = os.path.splitext (os.path.basename (snippet_path))[0]
195             texidoc_path = os.path.join ('Documentation', language_code,
196                          'texidocs', base + '.texidoc')
197             if os.path.isfile (texidoc_path):
198                 texidoc_translation = open (texidoc_path).read ()
199                 texidoc_translation = texidoc_translation.replace ('\\', '\\\\')
200                 contents = begin_header_re.sub ('\\g<0>\n' + texidoc_translation, contents, 1)
201     open (snippet_path, 'w').write (contents)
202
203 if in_dir:
204     ## clean out existing lys and generated files
205     map (os.remove, glob.glob (os.path.join (DEST, '*.ly')) +
206          glob.glob (os.path.join (DEST, '*.snippet-list')))
207
208     # read LSR source where tags are defined by subdirs
209     snippets, tag_lists = read_source_with_dirs (in_dir)
210
211     # read input/new where tags are directly defined
212     s, l = read_source (NEW_LYS)
213     snippets.update (s)
214     for t in TAGS:
215         tag_lists[t].update (l[t])
216 else:
217     snippets, tag_lists = read_source (NEW_LYS)
218     ## update texidocs of snippets that don't come from NEW_LYS
219     for snippet_path in glob.glob (os.path.join (DEST, '*.ly')):
220         if not os.path.basename (snippet_path) in snippets:
221             update_ly_in_place (snippet_path)
222
223 for (name, (srcdir, tags)) in snippets.items ():
224     copy_ly (srcdir, name, tags)
225 for (tag, file_set) in tag_lists.items ():
226     dump_file_list (os.path.join (DEST, tag + '.snippet-list'),
227                     file_set, update=not(in_dir))
228 if unconverted:
229     sys.stderr.write ('These files could not be converted successfully by convert-ly:\n')
230     sys.stderr.write ('\n'.join (unconverted) + '\n\n')
231 if notags_files:
232     sys.stderr.write ('No tags could be found in these files:\n')
233     sys.stderr.write ('\n'.join (notags_files) + '\n\n')
234 if unsafe:
235     dump_file_list ('lsr-unsafe.txt', unsafe)
236     sys.stderr.write ('''
237
238 Unsafe files printed in lsr-unsafe.txt: CHECK MANUALLY!
239   git add %s/*.ly
240   xargs git diff HEAD < lsr-unsafe.txt
241
242 ''' % DEST)