3 # convert-ly.py -- Update old LilyPond input files (fix name?)
5 # source file of the GNU LilyPond music typesetter
7 # (c) 1998--2007 Han-Wen Nienhuys <hanwen@xs4all.nl>
8 # Jan Nieuwenhuizen <janneke@gnu.org>
10 # converting rules are found in python/convertrules.py
27 lilypond_version_re_str = '\\\\version *\"([0-9.]+)"'
28 lilypond_version_re = re.compile (lilypond_version_re_str)
32 _ ('''Update LilyPond input to newer version. By default, update from the
33 version taken from the \\version command, to the current LilyPond version.''')
37 convert-ly --from=2.3.28 --to 2.5.21 foobar.ly
40 copyright = ('Jan Nieuwenhuizen <janneke@gnu.org>',
41 'Han-Wen Nienhuys <hanwen@xs4all.nl>')
43 program_name = os.path.basename (sys.argv[0])
44 program_version = '@TOPLEVEL_VERSION@'
47 sys.stderr.write (program_name + ": " + _ ("warning: %s") % s + '\n')
50 sys.stderr.write (program_name + ": " + _ ("error: %s") % s + '\n')
52 def identify (port=sys.stderr):
53 port.write ('%s (GNU LilyPond) %s\n' % (program_name, program_version))
66 _ ("Distributed under terms of the GNU General Public License."),
67 _ ('It comes with NO WARRANTY.')))
70 def get_option_parser ():
71 p = ly.get_option_parser (usage=_ ("%s [OPTION]... FILE") % 'convert-ly',
72 version="@TOPLEVEL_VERSION@",
73 description=help_summary)
75 p.add_option ('-f', '--from',
77 metavar=_ ("VERSION"),
79 help=_ ("start from VERSION [default: \\version found in file]"),
82 p.add_option ('-e', '--edit', help=_ ("edit in place"),
84 p.add_option ('-n', '--no-version',
85 help=_ ("do not add \\version command if missing"),
87 dest='skip_version_add',
90 p.add_option ("-s", '--show-rules',
91 help=_ ("show rules [default: --from=0, --to=@TOPLEVEL_VERSION@]"),
93 action='store_true', default=False)
95 p.add_option ('-t', '--to',
96 help=_ ("convert to VERSION [default: @TOPLEVEL_VERSION@]"),
97 metavar=_ ('VERSION'),
102 p.add_option_group ('bugs',
103 description=(_ ("Report bugs via")
104 + ''' http://post.gmane.org/post.php'''
105 '''?group=gmane.comp.gnu.lilypond.bugs\n'''))
111 def str_to_tuple (s):
112 return tuple (map (int, string.split (s, '.')))
115 return string.join (map (lambda x: '%s' % x, list (t)), '.')
117 def version_cmp (t1, t2):
123 def get_conversions (from_version, to_version):
124 def is_applicable (v, f = from_version, t = to_version):
125 return version_cmp (v[0], f) > 0 and version_cmp (v[0], t) <= 0
126 return filter (is_applicable, convertrules.conversions)
128 def latest_version ():
129 return convertrules.conversions[-1][0]
131 def show_rules (file, from_version, to_version):
132 for x in convertrules.conversions:
133 if (not from_version or x[0] > from_version) \
134 and (not to_version or x[0] <= to_version):
135 file.write ('%s: %s\n' % (tup_to_str (x[0]), x[2]))
137 def do_conversion (str, from_version, to_version):
138 """Apply conversions from FROM_VERSION to TO_VERSION. Return
139 tuple (LAST,STR), with the last succesful conversion and the resulting
141 conv_list = get_conversions (from_version, to_version)
143 if convertrules.error_file:
144 convertrules.error_file.write (_ ("Applying conversion: "))
149 convertrules.error_file.write (tup_to_str (x[0]))
150 if x != conv_list[-1]:
151 convertrules.error_file.write (', ')
153 last_conversion = x[0]
155 except convertrules.FatalConversionError:
156 convertrules.error_file.write ('\n'
157 + _ ("Error while converting")
159 + _ ("Stopping at last succesful rule")
162 return (last_conversion, str)
166 def guess_lilypond_version (filename):
167 s = open (filename).read ()
168 m = lilypond_version_re.search (s)
174 class FatalConversionError:
177 class UnknownVersion:
180 def do_one_file (infile_name):
181 sys.stderr.write (_ ("Processing `%s\'... ") % infile_name)
182 sys.stderr.write ('\n')
186 if global_options.from_version:
187 from_version = global_options.from_version
189 guess = guess_lilypond_version (infile_name)
191 raise UnknownVersion ()
192 from_version = str_to_tuple (guess)
194 if global_options.to_version:
195 to_version = global_options.to_version
197 to_version = latest_version ()
201 infile = open (infile_name, 'r')
206 (last, result) = do_conversion (infile.read (), from_version, to_version)
210 newversion = r'\version "%s"' % tup_to_str (last)
211 if lilypond_version_re.search (result):
212 result = re.sub (lilypond_version_re_str,
213 '\\' + newversion, result)
214 elif not global_options.skip_version_add:
215 result = newversion + '\n' + result
217 convertrules.error_file.write ('\n')
219 if global_options.edit:
221 os.remove(infile_name + '~')
224 os.rename (infile_name, infile_name + '~')
225 outfile = open (infile_name, 'w')
230 outfile.write (result)
235 opt_parser = get_option_parser()
236 (options, args) = opt_parser.parse_args ()
239 if options.from_version:
240 options.from_version = str_to_tuple (options.from_version)
241 if options.to_version:
242 options.to_version = str_to_tuple (options.to_version)
244 options.outfile_name = ''
245 global global_options
246 global_options = options
248 if not args and not options.show_rules:
249 opt_parser.print_help ()
255 files = do_options ()
257 # should parse files[] to read \version?
258 if global_options.show_rules:
259 show_rules (sys.stdout, global_options.from_version, global_options.to_version)
262 identify (sys.stderr)
267 elif not os.path.isfile (f):
268 error (_ ("cannot open file: `%s'") % f)
274 except UnknownVersion:
275 error (_ ("cannot determine version for `%s'. Skipping") % f)
277 sys.stderr.write ('\n')