3 # convert-ly.py -- Update old LilyPond input files (fix name?)
5 # source file of the GNU LilyPond music typesetter
7 # (c) 1998--2006 Han-Wen Nienhuys <hanwen@cs.uu.nl>
8 # Jan Nieuwenhuizen <janneke@gnu.org>
10 # converting rules are found in python/convertrules.py
19 datadir = '@local_lilypond_datadir@'
20 if not os.path.isdir (datadir):
21 datadir = '@lilypond_datadir@'
22 if os.environ.has_key ('LILYPONDPREFIX'):
23 datadir = os.environ['LILYPONDPREFIX']
24 while datadir[-1] == os.sep:
25 datadir = datadir[:-1]
27 if os.path.exists (os.path.join (datadir, 'share/lilypond/@TOPLEVEL_VERSION@/')):
28 datadir = os.path.join (datadir, 'share/lilypond/@TOPLEVEL_VERSION@/')
30 sys.path.insert (0, os.path.join (datadir, 'python'))
37 from convertrules import *
39 lilypond_version_re_str = '\\\\version *\"([0-9.]+)"'
40 lilypond_version_re = re.compile (lilypond_version_re_str)
44 '''Update LilyPond input to newer version. By default, update from the
45 version taken from the \\version command, to the current LilyPond version.
50 convert-ly --from=2.3.28 --to 2.5.21 foobar.ly
53 copyright = ('Jan Nieuwenhuizen <janneke@gnu.org>',
54 'Han-Wen Nienhuys <hanwen@cs.uu.nl>')
56 option_definitions = [
57 ('', 'e', 'edit',_('edit in place')),
58 (_('VERSION'), 'f', 'from',
59 _('start from VERSION [default: \\version found in file]')),
60 ('', 'h', 'help',_('print this help')),
61 ('', 'n', 'no-version',_ ('do not add \\version command if missing')),
62 ('','s', 'show-rules', _('print rules [default: --from=0, --to=@TOPLEVEL_VERSION@]')),
63 (_('VERSION'), 't', 'to',_('convert to VERSION [default: @TOPLEVEL_VERSION@]')),
64 ('','v','version',_('print program version"'))
67 program_name = os.path.basename (sys.argv[0])
68 program_version = '@TOPLEVEL_VERSION@'
73 return tuple (map (string.atoi, string.split (s, '.')))
76 return string.join (map (lambda x: '%s' % x, list (t)), '.')
78 def version_cmp (t1, t2):
84 def get_conversions (from_version, to_version):
85 def is_applicable (v, f = from_version, t = to_version):
86 return version_cmp (v[0], f) > 0 and version_cmp (v[0], t) <= 0
87 return filter (is_applicable, conversions)
89 def latest_version ():
90 return conversions[-1][0]
92 def show_rules (file, from_version, to_version):
94 if (not from_version or x[0] > from_version) \
95 and (not to_version or x[0] <= to_version):
96 file.write ('%s: %s\n' % (tup_to_str (x[0]), x[2]))
98 def do_conversion (str, from_version, to_version):
99 """Apply conversions from FROM_VERSION to TO_VERSION. Return
100 tuple (LAST,STR), with the last succesful conversion and the resulting
102 conv_list = get_conversions (from_version, to_version)
105 error_file.write (_ ("Applying conversion: "))
110 error_file.write (tup_to_str (x[0]))
111 if x != conv_list[-1]:
112 error_file.write (', ')
114 last_conversion = x[0]
116 except FatalConversionError:
117 error_file.write (_ ("error while converting"))
118 error_file.write ('\n')
119 error_file.write (_ ("Aborting"))
120 error_file.write ('\n')
124 return (last_conversion, str)
128 def guess_lilypond_version (filename):
129 s = open (filename).read ()
130 m = lilypond_version_re.search (s)
136 class FatalConversionError:
139 class UnknownVersion:
142 def do_one_file (infile_name):
143 sys.stderr.write (_ ("Processing `%s\'... ") % infile_name)
144 sys.stderr.write ('\n')
147 outfile_name = infile_name + '.NEW'
148 elif __main__.outfile_name:
149 outfile_name = __main__.outfile_name
151 if __main__.from_version:
152 from_version = __main__.from_version
154 guess = guess_lilypond_version (infile_name)
156 raise UnknownVersion ()
157 from_version = str_to_tuple (guess)
159 if __main__.to_version:
160 to_version = __main__.to_version
162 to_version = latest_version ()
166 infile = open (infile_name, 'r')
171 (last, result) = do_conversion (infile.read (), from_version, to_version)
175 newversion = r'\version "%s"' % tup_to_str (last)
176 if lilypond_version_re.search (result):
177 result = re.sub (lilypond_version_re_str,
178 '\\' + newversion, result)
180 result = newversion + '\n' + result
182 error_file.write ('\n')
186 os.remove(infile_name + '~')
189 os.rename (infile_name, infile_name + '~')
190 outfile = open (infile_name, 'w')
195 outfile.write (result)
206 global from_version, to_version, edit, show_rules_p, add_version
208 (sh, long) = ly.getopt_args (option_definitions)
210 (options, files) = getopt.getopt (sys.argv[1:], sh, long)
211 except getopt.error, s:
212 sys.stderr.write ('\n')
213 ly.error (_ ("getopt says: `%s'" % s))
214 sys.stderr.write ('\n')
222 if o == '--help' or o == '-h':
225 elif o == '--version' or o == '-v':
226 ly.identify (sys.stdout)
228 elif o== '--from' or o=='-f':
229 from_version = str_to_tuple (a)
230 elif o== '--to' or o=='-t':
231 to_version = str_to_tuple (a)
232 elif o== '--edit' or o == '-e':
234 elif o== '--show-rules' or o == '-s':
236 elif o == '--no-version' or o == '-n':
246 files = do_options ()
248 # should parse files[] to read \version?
250 show_rules (sys.stdout, from_version, to_version)
253 ly.identify (sys.stderr)
262 elif not os.path.isfile (f):
263 ly.error (_ ("can't open file: `%s'") % f)
269 except UnknownVersion:
270 ly.error (_ ("can't determine version for `%s'. Skipping") % f)
272 sys.stderr.write ('\n')