]> git.donarmstrong.com Git - lilypond.git/blob - scripts/convert-ly.py
Merge branch 'master' of ssh+git://git.sv.gnu.org/srv/git/lilypond
[lilypond.git] / scripts / convert-ly.py
1 #!@TARGET_PYTHON@
2 #
3 # convert-ly.py -- Update old LilyPond input files (fix name?)
4 #
5 # source file of the GNU LilyPond music typesetter
6 #
7 # (c) 1998--2007  Han-Wen Nienhuys <hanwen@xs4all.nl>
8 #                 Jan Nieuwenhuizen <janneke@gnu.org>
9 #
10 # converting rules are found in python/convertrules.py
11 #
12
13 import os
14 import sys
15 import re
16
17 """
18 @relocate-preamble@
19 """
20
21 import lilylib as ly
22 global _;_=ly._
23
24 import convertrules
25
26 lilypond_version_re_str = '\\\\version *\"([0-9.]+)"'
27 lilypond_version_re = re.compile (lilypond_version_re_str)
28
29
30 help_summary = (
31 _ ('''Update LilyPond input to newer version.  By default, update from the
32 version taken from the \\version command, to the current LilyPond version.''')
33 + _ ("Examples:")
34 + '''
35   $ convert-ly -e old.ly
36   $ convert-ly --from=2.3.28 --to=2.5.21 foobar.ly > foobar-new.ly
37 ''')
38
39 copyright = ('Jan Nieuwenhuizen <janneke@gnu.org>',
40              'Han-Wen Nienhuys <hanwen@xs4all.nl>')
41
42 program_name = os.path.basename (sys.argv[0])
43 program_version = '@TOPLEVEL_VERSION@'
44
45 error_file_write = ly.stderr_write
46
47 def warning (s):
48     ly.stderr_write (program_name + ": " + _ ("warning: %s") % s + '\n')
49
50 def error (s):
51     ly.stderr_write (program_name + ": " + _ ("error: %s") % s + '\n')
52
53 def identify (port=sys.stderr):
54     ly.encoded_write (port, '%s (GNU LilyPond) %s\n' % (program_name, program_version))
55
56 def warranty ():
57     identify ()
58     ly.encoded_write (sys.stdout, '''
59 Copyright (c) %s by
60
61   Han-Wen Nienhuys
62   Jan Nieuwenhuizen
63
64 %s
65 %s
66 '''  ( '2001--2006',
67        _ ("Distributed under terms of the GNU General Public License."),
68        _ ('It comes with NO WARRANTY.')))
69
70
71 def get_option_parser ():
72     p = ly.get_option_parser (usage=_ ("%s [OPTION]... FILE") % 'convert-ly',
73                   description=help_summary,
74                   add_help_option=False)
75
76     p.version="@TOPLEVEL_VERSION@"
77     p.add_option("--version",
78                  action="version",
79                  help=_ ("show version number and exit"))
80
81     p.add_option("-h", "--help",
82                  action="help",
83                  help=_ ("show this help and exit"))
84
85     p.add_option ('-f', '--from', 
86               action="store",
87               metavar=_ ("VERSION"),
88               dest="from_version",
89               help=_ ("start from VERSION [default: \\version found in file]"),
90               default='')
91     
92     p.add_option ('-e', '--edit', help=_ ("edit in place"),
93               action='store_true')
94
95     p.add_option ('-n', '--no-version',
96               help=_ ("do not add \\version command if missing"),
97               action='store_true',
98               dest='skip_version_add',
99               default=False)
100
101     p.add_option ('-c', '--current-version',
102               help=_ ("force updating \\version number to %s") % program_version,
103               action='store_true',
104               dest='force_current_version',
105               default=False)
106     
107     p.add_option ("-s", '--show-rules',
108               help=_ ("show rules [default: -f 0, -t %s]") % program_version,
109               dest='show_rules',
110               action='store_true', default=False)
111     
112     p.add_option ('-t', '--to',
113               help=_ ("convert to VERSION [default: %s]") % program_version,
114               metavar=_ ('VERSION'),
115               action='store',
116               dest="to_version",
117               default='')
118
119     p.add_option_group ('',
120                         description=(_ ("Report bugs via")
121                                      + ''' http://post.gmane.org/post.php'''
122                                      '''?group=gmane.comp.gnu.lilypond.bugs\n'''))
123     
124     return p
125
126
127
128 def str_to_tuple (s):
129     return tuple ([int(n) for n in s.split ('.')])
130
131 def tup_to_str (t):
132     return '.'.join (['%s' % x for x in t])
133
134 def version_cmp (t1, t2):
135     for x in [0, 1, 2]:
136         if t1[x] - t2[x]:
137             return t1[x] - t2[x]
138     return 0
139
140 def get_conversions (from_version, to_version):
141     def is_applicable (v, f = from_version, t = to_version):
142         return version_cmp (v[0], f) > 0 and version_cmp (v[0], t) <= 0
143     return filter (is_applicable, convertrules.conversions)
144
145 def latest_version ():
146     return convertrules.conversions[-1][0]
147
148 def show_rules (file, from_version, to_version):
149     for x in convertrules.conversions:
150         if (not from_version or x[0] > from_version) \
151            and (not to_version or x[0] <= to_version):
152             ly.encoded_write  (file, '%s: %s\n' % (tup_to_str (x[0]), x[2]))
153
154 def do_conversion (str, from_version, to_version):
155     """Apply conversions from FROM_VERSION to TO_VERSION.  Return
156 tuple (LAST,STR), with the last succesful conversion and the resulting
157 string."""
158     conv_list = get_conversions (from_version, to_version)
159
160     error_file_write (_ ("Applying conversion: "))
161         
162     last_conversion = ()
163     try:
164         for x in conv_list:
165             error_file_write (tup_to_str (x[0]))
166             if x != conv_list[-1]:
167                 error_file_write (', ')
168             str = x[1] (str)
169             last_conversion = x[0]
170
171     except convertrules.FatalConversionError:
172         error_file_write ('\n'
173                           + _ ("Error while converting")
174                           + '\n'
175                           + _ ("Stopping at last succesful rule")
176                           + '\n')
177
178     return (last_conversion, str)
179
180
181
182 def guess_lilypond_version (input):
183     m = lilypond_version_re.search (input)
184     if m:
185         return m.group (1)
186     else:
187         return ''
188
189 class FatalConversionError:
190     pass
191
192 class UnknownVersion:
193     pass
194
195 def do_one_file (infile_name):
196     ly.stderr_write (_ ("Processing `%s\'... ") % infile_name)
197     sys.stderr.write ('\n')
198
199     if infile_name:
200         infile = open (infile_name, 'r')
201         input = infile.read ()
202         infile.close ()
203     else:
204         input = sys.stdin.read ()
205
206     from_version = None
207     to_version = None
208     if global_options.from_version:
209         from_version = global_options.from_version
210     else:
211         guess = guess_lilypond_version (input)
212         if not guess:
213             raise UnknownVersion ()
214         from_version = str_to_tuple (guess)
215
216     if global_options.to_version:
217         to_version = global_options.to_version
218     else:
219         to_version = latest_version ()
220
221
222     (last, result) = do_conversion (input, from_version, to_version)
223
224     if last:
225         if global_options.force_current_version and last == to_version:
226             last = str_to_tuple (program_version)
227
228         newversion = r'\version "%s"' % tup_to_str (last)
229         if lilypond_version_re.search (result):
230             result = re.sub (lilypond_version_re_str,
231                      '\\' + newversion, result)
232         elif not global_options.skip_version_add:
233             result = newversion + '\n' + result
234             
235         error_file_write ('\n')            
236     
237         if global_options.edit:
238             try:
239                 os.remove(infile_name + '~')
240             except:
241                 pass
242             os.rename (infile_name, infile_name + '~')
243             outfile = open (infile_name, 'w')
244         else:
245             outfile = sys.stdout
246
247
248         outfile.write (result)
249
250     sys.stderr.flush ()
251
252 def do_options ():
253     opt_parser = get_option_parser()
254     (options, args) = opt_parser.parse_args ()
255
256
257     if options.from_version:
258         options.from_version = str_to_tuple (options.from_version)
259     if options.to_version:
260         options.to_version = str_to_tuple (options.to_version)
261
262     options.outfile_name = ''
263     global global_options
264     global_options = options
265
266     if not args and not options.show_rules:
267         opt_parser.print_help ()
268         sys.exit (2)
269
270     return args
271
272 def main ():
273     files = do_options ()
274
275     # should parse files[] to read \version?
276     if global_options.show_rules:
277         show_rules (sys.stdout, global_options.from_version, global_options.to_version)
278         sys.exit (0)
279
280     identify (sys.stderr)
281
282     for f in files:
283         if f == '-':
284             f = ''
285         elif not os.path.isfile (f):
286             error (_ ("cannot open file: `%s'") % f)
287             if len (files) == 1:
288                 sys.exit (1)
289             continue
290         try:
291             do_one_file (f)
292         except UnknownVersion:
293             error (_ ("cannot determine version for `%s'.  Skipping") % f)
294
295     sys.stderr.write ('\n')
296
297 main ()