3 # convert-ly.py -- Update old LilyPond input files (fix name?)
4 # converting rules are found in python/convertrules.py
6 # This file is part of LilyPond, the GNU music typesetter.
8 # Copyright (C) 1998--2014 Han-Wen Nienhuys <hanwen@xs4all.nl>
9 # Jan Nieuwenhuizen <janneke@gnu.org>
11 # LilyPond is free software: you can redistribute it and/or modify
12 # it under the terms of the GNU General Public License as published by
13 # the Free Software Foundation, either version 3 of the License, or
14 # (at your option) any later version.
16 # LilyPond is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 # GNU General Public License for more details.
21 # You should have received a copy of the GNU General Public License
22 # along with LilyPond. If not, see <http://www.gnu.org/licenses/>.
36 ly.require_python_version ()
40 lilypond_version_re_str = '\\\\version *\"([0-9.]+)"'
41 lilypond_version_re = re.compile (lilypond_version_re_str)
43 lilypond_version_strict_re_str = '\\\\version *\"([0-9]+[.][0-9]+[.][0-9]+)"'
44 lilypond_version_strict_re = re.compile (lilypond_version_strict_re_str)
47 _ ('''Update LilyPond input to newer version. By default, update from the
48 version taken from the \\version command, to the current LilyPond version.''')
50 + _ ("If FILE is `-', read from standard input.")
54 $ convert-ly -e old.ly
55 $ convert-ly --from=2.3.28 --to=2.5.21 foobar.ly > foobar-new.ly
58 copyright = ('Jan Nieuwenhuizen <janneke@gnu.org>',
59 'Han-Wen Nienhuys <hanwen@xs4all.nl>')
61 program_name = os.path.basename (sys.argv[0])
62 program_version = '@TOPLEVEL_VERSION@'
64 authors = ('Jan Nieuwenhuizen <janneke@gnu.org>',
65 'Han-Wen Nienhuys <hanwen@xs4all.nl>')
68 ly.progress ('%s (GNU LilyPond) %s\n' % (program_name, program_version))
72 ly.encoded_write (sys.stdout, '''
79 ''' % ( _ ('Copyright (c) %s by') % '2001--2014',
81 _ ('Distributed under terms of the GNU General Public License.'),
82 _ ('It comes with NO WARRANTY.')))
84 def get_option_parser ():
85 p = ly.get_option_parser (usage=_ ("%s [OPTION]... FILE") % 'convert-ly',
86 description=help_summary,
87 add_help_option=False)
89 p.version="@TOPLEVEL_VERSION@"
90 p.add_option("--version",
92 help=_ ("show version number and exit"))
94 p.add_option("-h", "--help",
96 help=_ ("show this help and exit"))
98 p.add_option ('-f', '--from',
100 metavar=_ ("VERSION"),
102 help=_ ("start from VERSION [default: \\version found in file]"),
105 p.add_option ('-e', '--edit', help=_ ("edit in place"),
108 p.add_option ("-l", "--loglevel",
109 help=_ ("Print log messages according to LOGLEVEL "
110 "(NONE, ERROR, WARNING, PROGRESS (default), DEBUG)"),
111 metavar=_ ("LOGLEVEL"),
113 callback=ly.handle_loglevel_option,
116 p.add_option ('-n', '--no-version',
117 help=_ ("do not add \\version command if missing"),
119 dest='skip_version_add',
122 p.add_option ('-c', '--current-version',
123 help=_ ("force updating \\version number to %s") % program_version,
125 dest='force_current_version',
128 p.add_option ('-d', '--diff-version-update',
129 help=_ ("only update \\version number if file is modified"),
131 dest='diff_version_update',
134 p.add_option ("-s", '--show-rules',
135 help=_ ("show rules [default: -f 0, -t %s]") % program_version,
137 action='store_true', default=False)
139 p.add_option ('-t', '--to',
140 help=_ ("convert to VERSION [default: %s]") % program_version,
141 metavar=_ ('VERSION'),
146 p.add_option ('-b', '--backup-numbered',
147 help=_ ("make a numbered backup [default: filename.ext~]"),
149 dest="backup_numbered",
152 p.add_option ('-w', '--warranty', help=_ ("show warranty and copyright"),
155 p.add_option_group ('',
157 _ ("Report bugs via %s")
158 % 'http://post.gmane.org/post.php'
159 '?group=gmane.comp.gnu.lilypond.bugs') + '\n')
163 def str_to_tuple (s):
164 return tuple ([int(n) for n in s.split ('.')])
167 return '.'.join (['%s' % x for x in t])
169 def version_cmp (t1, t2):
175 def get_conversions (from_version, to_version):
176 def is_applicable (v, f = from_version, t = to_version):
177 return version_cmp (v[0], f) > 0 and version_cmp (v[0], t) <= 0
178 return filter (is_applicable, convertrules.conversions)
180 def latest_version ():
181 return convertrules.conversions[-1][0]
183 def show_rules (file, from_version, to_version):
184 for x in convertrules.conversions:
185 if (not from_version or x[0] > from_version) \
186 and (not to_version or x[0] <= to_version):
187 ly.encoded_write (file, '%s: %s\n' % (tup_to_str (x[0]), x[2]))
189 def do_conversion (str, from_version, to_version):
190 """Apply conversions from FROM_VERSION to TO_VERSION. Return
191 tuple (LAST,LASTCHANGED,STR,ERRORS), with the last applied conversion,
192 the last conversion resulting in a change, the resulting
193 string and the number of errors."""
194 conv_list = get_conversions (from_version, to_version)
196 ly.progress (_ ("Applying conversion: "), newline = False)
198 last_conversion = None
203 if x != conv_list[-1]:
204 ly.progress (tup_to_str (x[0]), newline = False)
205 ly.progress (', ', newline = False)
207 ly.progress (tup_to_str (x[0]))
209 last_conversion = x[0]
211 last_change = last_conversion
214 except convertrules.FatalConversionError:
215 ly.error (_ ("Error while converting")
217 + _ ("Stopping at last successful rule"))
220 return (last_conversion, last_change, str, errors)
222 def guess_lilypond_version (input):
223 m = lilypond_version_strict_re.search (input)
226 m = lilypond_version_re.search (input)
228 raise InvalidVersion (m.group (1))
232 class FatalConversionError (Exception):
235 class UnknownVersion (Exception):
238 class InvalidVersion (Exception):
239 def __init__ (self, version):
240 self.version = version
242 def back_up (file, numbered):
247 back_up = file + '.~' + str(n) + '~'
248 if not os.path.exists (back_up):
252 shutil.copy2 (file, back_up)
255 def do_one_file (infile_name):
256 ly.progress (_ (u"Processing `%s\'... ") % infile_name, True)
259 infile = open (infile_name, 'r')
260 input = infile.read ()
263 input = sys.stdin.read ()
267 if global_options.from_version:
268 from_version = global_options.from_version
270 guess = guess_lilypond_version (input)
272 raise UnknownVersion ()
273 from_version = str_to_tuple (guess)
275 if global_options.to_version:
276 to_version = global_options.to_version
278 to_version = latest_version ()
280 if len (from_version) != 3:
281 raise InvalidVersion (".".join ([str(n) for n in from_version]))
284 (last, last_change, result, errors) = \
285 do_conversion (input, from_version, to_version)
287 if global_options.force_current_version and \
288 (last is None or last == to_version):
289 last = str_to_tuple (program_version)
291 if global_options.diff_version_update:
292 # Note that last_change can be set even if the result is
293 # the same if two conversion rules cancelled out
295 # make no (actual) change to the version number
299 # If the last update was to an unstable version
300 # number, and the final update target is no longer in
301 # the same unstable series, we update to the stable
302 # series following the unstable version.
303 if last[1]%2: # unstable
304 next_stable = (last[0], last[1]+1, 0)
305 if next_stable <= to_version:
308 newversion = r'\version "%s"' % tup_to_str (last)
309 if lilypond_version_re.search (result):
310 result = re.sub (lilypond_version_re_str,
311 '\\' + newversion, result)
312 elif not global_options.skip_version_add:
313 result = newversion + '\n' + result
317 if global_options.edit:
318 backup = back_up (infile_name, global_options.backup_numbered)
319 outfile = open (infile_name, 'w')
323 outfile.write (result)
330 opt_parser = get_option_parser()
331 (options, args) = opt_parser.parse_args ()
337 if options.from_version:
338 options.from_version = str_to_tuple (options.from_version)
339 if options.to_version:
340 options.to_version = str_to_tuple (options.to_version)
342 options.outfile_name = ''
343 global global_options
344 global_options = options
346 if not args and not options.show_rules:
347 opt_parser.print_help ()
353 files = do_options ()
355 # should parse files[] to read \version?
356 if global_options.show_rules:
357 show_rules (sys.stdout, global_options.from_version, global_options.to_version)
364 f = f.decode (sys.stdin.encoding or "utf-8")
367 elif not os.path.isfile (f):
368 ly.error (_ (u"%s: Unable to open file") % f)
372 errors += do_one_file (f)
373 except UnknownVersion:
374 ly.error (_ (u"%s: Unable to determine version. Skipping") % f)
376 except InvalidVersion:
377 # Compat code for 2.x and 3.0 syntax ("except .. as v" doesn't
378 # work in python 2.4!):
379 t, v, b = sys.exc_info ()
380 ly.error (_ (u"%s: Invalid version string `%s' \n"
381 "Valid version strings consist of three numbers, "
382 "separated by dots, e.g. `2.8.12'") % (f, v.version) )
386 ly.warning (ly.ungettext ("There was %d error.",
387 "There were %d errors.", errors) % errors)