]> git.donarmstrong.com Git - lilypond.git/blob - buildscripts/makelsr.py
Merge branch 'lilypond/translation' of ssh://jomand@git.sv.gnu.org/srv/git/lilypond
[lilypond.git] / buildscripts / 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 '''
12
13 LY_HEADER_LSR = '''%% Do not edit this file; it is auto-generated from LSR http://lsr.dsi.unimi.it
14 %% This file is in the public domain.
15 '''
16
17 LY_HEADER_NEW = '''%% Do not edit this file; it is auto-generated from input/new
18 %% This file is in the public domain.
19 '''
20
21 DEST = os.path.join ('input', 'lsr')
22 NEW_LYS = os.path.join ('input', 'new')
23 TEXIDOCS = os.path.join ('input', 'texidocs')
24
25 TAGS = []
26 # NR 1
27 TAGS.extend (['pitches', 'rhythms', 'expressive-marks',
28 'repeats', 'simultaneous-notes', 'staff-notation',
29 'editorial-and-educational-use', 'text'])
30 # NR 2
31 TAGS.extend (['vocal-music', 'chords', 'keyboards',
32 'percussion', 'fretted-strings', 'unfretted-strings', 'ancient-notation' # ,'winds'
33 ])
34
35 # other
36 TAGS.extend (['contexts-and-engravers', 'tweaks-and-overrides',
37 'paper-and-layout', 'breaks', 'spacing', 'midi', 'titles', 'template'])
38
39 def exit_with_usage (n=0):
40         sys.stderr.write (USAGE)
41         sys.exit (n)
42
43 try:
44         in_dir = sys.argv[1]
45 except:
46         exit_with_usage (2)
47
48 if not (os.path.isdir (DEST) and os.path.isdir (NEW_LYS)):
49         exit_with_usage (3)
50
51 unsafe = []
52 unconverted = []
53 notags_files = []
54
55 # mark the section that will be printed verbatim by lilypond-book
56 end_header_re = re.compile ('(\\header {.+?doctitle = ".+?})\n', re.M | re.S)
57
58 def mark_verbatim_section (ly_code):
59         return end_header_re.sub ('\\1 % begin verbatim\n', ly_code, 1)
60
61 begin_header_re = re.compile ('\\header\\s*{', re.M)
62
63 # add tags to ly files from LSR
64 def add_tags (ly_code, tags):
65         return begin_header_re.sub ('\\g<0>\n  lsrtags = "' + tags + '"\n', ly_code, 1)
66
67 def copy_ly (srcdir, name, tags):
68         global unsafe
69         global unconverted
70         dest = os.path.join (DEST, name)
71         tags = ', '.join (tags)
72         s = open (os.path.join (srcdir, name)).read ()
73
74         texidoc_translations_path = os.path.join (TEXIDOCS,
75                                                   os.path.splitext (name)[0] + '.texidoc')
76         if os.path.exists (texidoc_translations_path):
77                 texidoc_translations = open (texidoc_translations_path).read ()
78                 s = begin_header_re.sub ('\\g<0>\n' + texidoc_translations, s, 1)
79
80         if in_dir in srcdir:
81                 s = LY_HEADER_LSR + add_tags (s, tags)
82         else:
83                 s = LY_HEADER_NEW + s
84
85         s = mark_verbatim_section (s)
86         open (dest, 'w').write (s)
87
88         e = os.system ("convert-ly -e '%s'" % dest)
89         if e:
90                 unconverted.append (dest)
91         if os.path.exists (dest + '~'):
92                 os.remove (dest + '~')
93         # -V seems to make unsafe snippets fail nicer/sooner
94         e = os.system ("lilypond -V -dno-print-pages -dsafe -o /tmp/lsrtest '%s'" % dest)
95         if e:
96                 unsafe.append (dest)
97
98 def read_source_with_dirs (src):
99         s = {}
100         l = {}
101         for tag in TAGS:
102                 srcdir = os.path.join (src, tag)
103                 l[tag] = set (map (os.path.basename, glob.glob (os.path.join (srcdir, '*.ly'))))
104                 for f in l[tag]:
105                         if f in s.keys ():
106                                 s[f][1].append (tag)
107                         else:
108                                 s[f] = (srcdir, [tag])
109         return s, l
110
111
112 tags_re = re.compile ('lsrtags\\s*=\\s*"(.+?)"')
113
114 def read_source (src):
115         s = {}
116         l = dict ([(tag, set()) for tag in TAGS])
117         for f in glob.glob (os.path.join (src, '*.ly')):
118                 basename = os.path.basename (f)
119                 m = tags_re.search (open (f, 'r').read ())
120                 if m:
121                         file_tags = [tag.strip() for tag in m.group (1). split(',')]
122                         s[basename] = (src, file_tags)
123                         [l[tag].add (basename) for tag in file_tags if tag in TAGS]
124                 else:
125                         notags_files.append (f)
126         return s, l
127
128
129 def dump_file_list (file, list):
130         f = open (file, 'w')
131         f.write ('\n'.join (list) + '\n')
132
133 ## clean out existing lys and generated files
134 map (os.remove, glob.glob (os.path.join (DEST, '*.ly')) +
135      glob.glob (os.path.join (DEST, '*.snippet-list')))
136
137 # read LSR source where tags are defined by subdirs
138 snippets, tag_lists = read_source_with_dirs (in_dir)
139 # read input/new where tags are directly
140 s, l = read_source (NEW_LYS)
141 snippets.update (s)
142 for t in TAGS:
143         tag_lists[t].update (l[t])
144
145 for (name, (srcdir, tags)) in snippets.items ():
146         copy_ly (srcdir, name, tags)
147
148 for (tag, file_set) in tag_lists.items ():
149         dump_file_list (os.path.join (DEST, tag + '.snippet-list'), file_set)
150
151 if unconverted:
152         sys.stderr.write ('These files could not be converted successfully by convert-ly:\n')
153         sys.stderr.write ('\n'.join (unconverted) + '\n\n')
154
155 if notags_files:
156         sys.stderr.write ('No tags could be found in these files:\n')
157         sys.stderr.write ('\n'.join (notags_files) + '\n\n')
158
159 dump_file_list ('lsr-unsafe.txt', unsafe)
160 sys.stderr.write ('''
161
162 Unsafe files printed in lsr-unsafe.txt: CHECK MANUALLY!
163   git add input/lsr/*.ly
164   xargs git-diff HEAD < lsr-unsafe.txt
165
166 ''')
167