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