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
26 lilypond_version_re_str
= '\\\\version *\"([0-9.]+)"'
27 lilypond_version_re
= re
.compile (lilypond_version_re_str
)
31 _ ('''Update LilyPond input to newer version. By default, update from the
32 version taken from the \\version command, to the current LilyPond version.''')
36 convert-ly --from=2.3.28 --to 2.5.21 foobar.ly
39 copyright
= ('Jan Nieuwenhuizen <janneke@gnu.org>',
40 'Han-Wen Nienhuys <hanwen@xs4all.nl>')
42 program_name
= os
.path
.basename (sys
.argv
[0])
43 program_version
= '@TOPLEVEL_VERSION@'
45 error_file_write
= ly
.stderr_write
48 ly
.stderr_write (program_name
+ ": " + _ ("warning: %s") % s
+ '\n')
51 ly
.stderr_write (program_name
+ ": " + _ ("error: %s") % s
+ '\n')
53 def identify (port
=sys
.stderr
):
54 ly
.encoded_write (port
, '%s (GNU LilyPond) %s\n' % (program_name
, program_version
))
58 ly
.encoded_write (sys
.stdout
, '''
67 _ ("Distributed under terms of the GNU General Public License."),
68 _ ('It comes with NO WARRANTY.')))
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)
76 p
.version
="@TOPLEVEL_VERSION@"
77 p
.add_option("--version",
79 help=_ ("show version number and exit"))
81 p
.add_option("-h", "--help",
83 help=_ ("show this help and exit"))
85 p
.add_option ('-f', '--from',
87 metavar
=_ ("VERSION"),
89 help=_ ("start from VERSION [default: \\version found in file]"),
92 p
.add_option ('-e', '--edit', help=_ ("edit in place"),
95 p
.add_option ('-n', '--no-version',
96 help=_ ("do not add \\version command if missing"),
98 dest
='skip_version_add',
101 p
.add_option ('-c', '--current-version',
102 help=_ ("force updating \\version number to %s") % program_version
,
104 dest
='force_current_version',
107 p
.add_option ("-s", '--show-rules',
108 help=_ ("show rules [default: --from=0, --to=%s]") % program_version
,
110 action
='store_true', default
=False)
112 p
.add_option ('-t', '--to',
113 help=_ ("convert to VERSION [default: %s]") % program_version
,
114 metavar
=_ ('VERSION'),
119 p
.add_option_group (ly
.display_encode (_ ('Bugs')),
120 description
=(_ ("Report bugs via")
121 + ''' http://post.gmane.org/post.php'''
122 '''?group=gmane.comp.gnu.lilypond.bugs\n'''))
128 def str_to_tuple (s
):
129 return tuple ([int(n
) for n
in s
.split ('.')])
132 return '.'.join (['%s' % x
for x
in t
])
134 def version_cmp (t1
, t2
):
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
)
145 def latest_version ():
146 return convertrules
.conversions
[-1][0]
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]))
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
158 conv_list
= get_conversions (from_version
, to_version
)
160 error_file_write (_ ("Applying conversion: "))
165 error_file_write (tup_to_str (x
[0]))
166 if x
!= conv_list
[-1]:
167 error_file_write (', ')
169 last_conversion
= x
[0]
171 except convertrules
.FatalConversionError
:
172 error_file_write ('\n'
173 + _ ("Error while converting")
175 + _ ("Stopping at last succesful rule")
178 return (last_conversion
, str)
182 def guess_lilypond_version (input):
183 m
= lilypond_version_re
.search (input)
189 class FatalConversionError
:
192 class UnknownVersion
:
195 def do_one_file (infile_name
):
196 ly
.stderr_write (_ ("Processing `%s\'... ") % infile_name
)
197 sys
.stderr
.write ('\n')
200 infile
= open (infile_name
, 'r')
201 input = infile
.read ()
204 input = sys
.stdin
.read ()
208 if global_options
.from_version
:
209 from_version
= global_options
.from_version
211 guess
= guess_lilypond_version (input)
213 raise UnknownVersion ()
214 from_version
= str_to_tuple (guess
)
216 if global_options
.to_version
:
217 to_version
= global_options
.to_version
219 to_version
= latest_version ()
222 (last
, result
) = do_conversion (input, from_version
, to_version
)
225 if global_options
.force_current_version
and last
== to_version
:
226 last
= str_to_tuple (program_version
)
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
235 error_file_write ('\n')
237 if global_options
.edit
:
239 os
.remove(infile_name
+ '~')
242 os
.rename (infile_name
, infile_name
+ '~')
243 outfile
= open (infile_name
, 'w')
248 outfile
.write (result
)
253 opt_parser
= get_option_parser()
254 (options
, args
) = opt_parser
.parse_args ()
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
)
262 options
.outfile_name
= ''
263 global global_options
264 global_options
= options
266 if not args
and not options
.show_rules
:
267 opt_parser
.print_help ()
273 files
= do_options ()
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
)
280 identify (sys
.stderr
)
285 elif not os
.path
.isfile (f
):
286 error (_ ("cannot open file: `%s'") % f
)
292 except UnknownVersion
:
293 error (_ ("cannot determine version for `%s'. Skipping") % f
)
295 sys
.stderr
.write ('\n')