3 # convert-ly.py -- Update old LilyPond input files (fix name?)
5 # source file of the GNU LilyPond music typesetter
7 # (c) 1998--2008 Han-Wen Nienhuys <hanwen@xs4all.nl>
8 # Jan Nieuwenhuizen <janneke@gnu.org>
10 # converting rules are found in python/convertrules.py
24 ly.require_python_version ()
28 lilypond_version_re_str = '\\\\version *\"([0-9.]+)"'
29 lilypond_version_re = re.compile (lilypond_version_re_str)
33 _ ('''Update LilyPond input to newer version. By default, update from the
34 version taken from the \\version command, to the current LilyPond version.''')
37 $ convert-ly -e old.ly
38 $ convert-ly --from=2.3.28 --to=2.5.21 foobar.ly > foobar-new.ly
41 copyright = ('Jan Nieuwenhuizen <janneke@gnu.org>',
42 'Han-Wen Nienhuys <hanwen@xs4all.nl>')
44 program_name = os.path.basename (sys.argv[0])
45 program_version = '@TOPLEVEL_VERSION@'
47 error_file_write = ly.stderr_write
50 ly.stderr_write (program_name + ": " + _ ("warning: %s") % s + '\n')
53 ly.stderr_write (program_name + ": " + _ ("error: %s") % s + '\n')
55 def identify (port=sys.stderr):
56 ly.encoded_write (port, '%s (GNU LilyPond) %s\n' % (program_name, program_version))
60 ly.encoded_write (sys.stdout, '''
69 _ ("Distributed under terms of the GNU General Public License."),
70 _ ('It comes with NO WARRANTY.')))
73 def get_option_parser ():
74 p = ly.get_option_parser (usage=_ ("%s [OPTION]... FILE") % 'convert-ly',
75 description=help_summary,
76 add_help_option=False)
78 p.version="@TOPLEVEL_VERSION@"
79 p.add_option("--version",
81 help=_ ("show version number and exit"))
83 p.add_option("-h", "--help",
85 help=_ ("show this help and exit"))
87 p.add_option ('-f', '--from',
89 metavar=_ ("VERSION"),
91 help=_ ("start from VERSION [default: \\version found in file]"),
94 p.add_option ('-e', '--edit', help=_ ("edit in place"),
97 p.add_option ('-n', '--no-version',
98 help=_ ("do not add \\version command if missing"),
100 dest='skip_version_add',
103 p.add_option ('-c', '--current-version',
104 help=_ ("force updating \\version number to %s") % program_version,
106 dest='force_current_version',
109 p.add_option ("-s", '--show-rules',
110 help=_ ("show rules [default: -f 0, -t %s]") % program_version,
112 action='store_true', default=False)
114 p.add_option ('-t', '--to',
115 help=_ ("convert to VERSION [default: %s]") % program_version,
116 metavar=_ ('VERSION'),
121 p.add_option_group ('',
123 _ ("Report bugs via %s")
124 % 'http://post.gmane.org/post.php'
125 '?group=gmane.comp.gnu.lilypond.bugs') + '\n')
131 def str_to_tuple (s):
132 return tuple ([int(n) for n in s.split ('.')])
135 return '.'.join (['%s' % x for x in t])
137 def version_cmp (t1, t2):
143 def get_conversions (from_version, to_version):
144 def is_applicable (v, f = from_version, t = to_version):
145 return version_cmp (v[0], f) > 0 and version_cmp (v[0], t) <= 0
146 return filter (is_applicable, convertrules.conversions)
148 def latest_version ():
149 return convertrules.conversions[-1][0]
151 def show_rules (file, from_version, to_version):
152 for x in convertrules.conversions:
153 if (not from_version or x[0] > from_version) \
154 and (not to_version or x[0] <= to_version):
155 ly.encoded_write (file, '%s: %s\n' % (tup_to_str (x[0]), x[2]))
157 def do_conversion (str, from_version, to_version):
158 """Apply conversions from FROM_VERSION to TO_VERSION. Return
159 tuple (LAST,STR), with the last succesful conversion and the resulting
161 conv_list = get_conversions (from_version, to_version)
163 error_file_write (_ ("Applying conversion: "))
168 error_file_write (tup_to_str (x[0]))
169 if x != conv_list[-1]:
170 error_file_write (', ')
172 last_conversion = x[0]
174 except convertrules.FatalConversionError:
175 error_file_write ('\n'
176 + _ ("Error while converting")
178 + _ ("Stopping at last successful rule")
181 return (last_conversion, str)
185 def guess_lilypond_version (input):
186 m = lilypond_version_re.search (input)
192 class FatalConversionError:
195 class UnknownVersion:
198 def do_one_file (infile_name):
199 ly.stderr_write (_ ("Processing `%s\'... ") % infile_name)
200 sys.stderr.write ('\n')
203 infile = open (infile_name, 'r')
204 input = infile.read ()
207 input = sys.stdin.read ()
211 if global_options.from_version:
212 from_version = global_options.from_version
214 guess = guess_lilypond_version (input)
216 raise UnknownVersion ()
217 from_version = str_to_tuple (guess)
219 if global_options.to_version:
220 to_version = global_options.to_version
222 to_version = latest_version ()
225 (last, result) = do_conversion (input, from_version, to_version)
228 if global_options.force_current_version and last == to_version:
229 last = str_to_tuple (program_version)
231 newversion = r'\version "%s"' % tup_to_str (last)
232 if lilypond_version_re.search (result):
233 result = re.sub (lilypond_version_re_str,
234 '\\' + newversion, result)
235 elif not global_options.skip_version_add:
236 result = newversion + '\n' + result
238 error_file_write ('\n')
240 if global_options.edit:
242 os.remove(infile_name + '~')
245 os.rename (infile_name, infile_name + '~')
246 outfile = open (infile_name, 'w')
251 outfile.write (result)
256 opt_parser = get_option_parser()
257 (options, args) = opt_parser.parse_args ()
260 if options.from_version:
261 options.from_version = str_to_tuple (options.from_version)
262 if options.to_version:
263 options.to_version = str_to_tuple (options.to_version)
265 options.outfile_name = ''
266 global global_options
267 global_options = options
269 if not args and not options.show_rules:
270 opt_parser.print_help ()
276 files = do_options ()
278 # should parse files[] to read \version?
279 if global_options.show_rules:
280 show_rules (sys.stdout, global_options.from_version, global_options.to_version)
283 identify (sys.stderr)
288 elif not os.path.isfile (f):
289 error (_ ("cannot open file: `%s'") % f)
295 except UnknownVersion:
296 error (_ ("cannot determine version for `%s'. Skipping") % f)
298 sys.stderr.write ('\n')