]> git.donarmstrong.com Git - lilypond.git/blob - scripts/convert-ly.py
Merge branch 'master' of ssh://jomand@git.sv.gnu.org/srv/git/lilypond
[lilypond.git] / scripts / convert-ly.py
1 #!@TARGET_PYTHON@
2 #
3 # convert-ly.py -- Update old LilyPond input files (fix name?)
4 #
5 # source file of the GNU LilyPond music typesetter
6 #
7 # (c) 1998--2007  Han-Wen Nienhuys <hanwen@xs4all.nl>
8 #                 Jan Nieuwenhuizen <janneke@gnu.org>
9 #
10 # converting rules are found in python/convertrules.py
11 #
12
13 import os
14 import sys
15 import string
16 import re
17
18 """
19 @relocate-preamble@
20 """
21
22 import lilylib as ly
23 global _;_=ly._
24
25 import convertrules
26
27 lilypond_version_re_str = '\\\\version *\"([0-9.]+)"'
28 lilypond_version_re = re.compile (lilypond_version_re_str)
29
30
31 help_summary = (
32 _ ('''Update LilyPond input to newer version.  By default, update from the
33 version taken from the \\version command, to the current LilyPond version.''')
34 + _ ("Examples:")
35 + '''
36   convert-ly -e old.ly
37   convert-ly --from=2.3.28 --to 2.5.21 foobar.ly
38 ''')
39
40 copyright = ('Jan Nieuwenhuizen <janneke@gnu.org>',
41              'Han-Wen Nienhuys <hanwen@xs4all.nl>')
42
43 program_name = os.path.basename (sys.argv[0])
44 program_version = '@TOPLEVEL_VERSION@'
45
46 def warning (s):
47     ly.stderr_write (program_name + ": " + _ ("warning: %s") % s + '\n')
48
49 def error (s):
50     ly.stderr_write (program_name + ": " + _ ("error: %s") % s + '\n')
51
52 def identify (port=sys.stderr):
53     ly.encoded_write (port, '%s (GNU LilyPond) %s\n' % (program_name, program_version))
54
55 def warranty ():
56     identify ()
57     ly.encoded_write (sys.stdout, '''
58 Copyright (c) %s by
59
60   Han-Wen Nienhuys
61   Jan Nieuwenhuizen
62
63 %s
64 %s
65 '''  ( '2001--2006',
66        _ ("Distributed under terms of the GNU General Public License."),
67        _ ('It comes with NO WARRANTY.')))
68
69
70 def get_option_parser ():
71     p = ly.get_option_parser (usage=_ ("%s [OPTION]... FILE") % 'convert-ly',
72                   description=help_summary,
73                   add_help_option=False)
74
75     p.version="@TOPLEVEL_VERSION@"
76     p.add_option("--version",
77                  action="version",
78                  help=_ ("show version number and exit"))
79
80     p.add_option("-h", "--help",
81                  action="help",
82                  help=_ ("show this help and exit"))
83
84     p.add_option ('-f', '--from', 
85               action="store",
86               metavar=_ ("VERSION"),
87               dest="from_version",
88               help=_ ("start from VERSION [default: \\version found in file]"),
89               default='')
90     
91     p.add_option ('-e', '--edit', help=_ ("edit in place"),
92               action='store_true')
93     p.add_option ('-n', '--no-version',
94               help=_ ("do not add \\version command if missing"),
95               action='store_true',
96               dest='skip_version_add',
97               default=False)
98     
99     p.add_option ("-s", '--show-rules',
100               help=_ ("show rules [default: --from=0, --to=@TOPLEVEL_VERSION@]"),
101               dest='show_rules',
102               action='store_true', default=False)
103     
104     p.add_option ('-t', '--to',
105               help=_ ("convert to VERSION [default: @TOPLEVEL_VERSION@]"),
106               metavar=_ ('VERSION'),
107               action='store',
108               dest="to_version",
109               default='')
110
111     p.add_option_group (ly.display_encode (_ ('Bugs')),
112                         description=(_ ("Report bugs via")
113                                      + ''' http://post.gmane.org/post.php'''
114                                      '''?group=gmane.comp.gnu.lilypond.bugs\n'''))
115     
116     return p
117
118
119
120 def str_to_tuple (s):
121     return tuple (map (int, string.split (s, '.')))
122
123 def tup_to_str (t):
124     return string.join (map (lambda x: '%s' % x, list (t)), '.')
125
126 def version_cmp (t1, t2):
127     for x in [0, 1, 2]:
128         if t1[x] - t2[x]:
129             return t1[x] - t2[x]
130     return 0
131
132 def get_conversions (from_version, to_version):
133     def is_applicable (v, f = from_version, t = to_version):
134         return version_cmp (v[0], f) > 0 and version_cmp (v[0], t) <= 0
135     return filter (is_applicable, convertrules.conversions)
136
137 def latest_version ():
138     return convertrules.conversions[-1][0]
139
140 def show_rules (file, from_version, to_version):
141     for x in convertrules.conversions:
142         if (not from_version or x[0] > from_version) \
143            and (not to_version or x[0] <= to_version):
144             file.write  ('%s: %s\n' % (tup_to_str (x[0]), x[2]))
145
146 def do_conversion (str, from_version, to_version):
147     """Apply conversions from FROM_VERSION to TO_VERSION.  Return
148 tuple (LAST,STR), with the last succesful conversion and the resulting
149 string."""
150     conv_list = get_conversions (from_version, to_version)
151
152     if convertrules.error_file:
153         convertrules.error_file.write (_ ("Applying conversion: "))
154         
155     last_conversion = ()
156     try:
157         for x in conv_list:
158             convertrules.error_file.write (tup_to_str (x[0]))
159             if x != conv_list[-1]:
160                 convertrules.error_file.write (', ')
161             str = x[1] (str)
162             last_conversion = x[0]
163
164     except convertrules.FatalConversionError:
165         convertrules.error_file.write ('\n'
166                                        + _ ("Error while converting")
167                                        + '\n'
168                                        + _ ("Stopping at last succesful rule")
169                                        + '\n')
170
171     return (last_conversion, str)
172
173
174
175 def guess_lilypond_version (filename):
176     s = open (filename).read ()
177     m = lilypond_version_re.search (s)
178     if m:
179         return m.group (1)
180     else:
181         return ''
182
183 class FatalConversionError:
184     pass
185
186 class UnknownVersion:
187     pass
188
189 def do_one_file (infile_name):
190     ly.stderr_write (_ ("Processing `%s\'... ") % infile_name)
191     sys.stderr.write ('\n')
192
193     from_version = None
194     to_version = None
195     if global_options.from_version:
196         from_version = global_options.from_version
197     else:
198         guess = guess_lilypond_version (infile_name)
199         if not guess:
200             raise UnknownVersion ()
201         from_version = str_to_tuple (guess)
202
203     if global_options.to_version:
204         to_version = global_options.to_version
205     else:
206         to_version = latest_version ()
207
208
209     if infile_name:
210         infile = open (infile_name, 'r')
211     else:
212         infile = sys.stdin
213
214
215     (last, result) = do_conversion (infile.read (), from_version, to_version)
216     infile.close ()
217
218     if last:
219         newversion = r'\version "%s"' % tup_to_str (last)
220         if lilypond_version_re.search (result):
221             result = re.sub (lilypond_version_re_str,
222                      '\\' + newversion, result)
223         elif not global_options.skip_version_add:
224             result = newversion + '\n' + result
225             
226         convertrules.error_file.write ('\n')            
227     
228         if global_options.edit:
229             try:
230                 os.remove(infile_name + '~')
231             except:
232                 pass
233             os.rename (infile_name, infile_name + '~')
234             outfile = open (infile_name, 'w')
235         else:
236             outfile = sys.stdout
237
238
239         outfile.write (result)
240
241     sys.stderr.flush ()
242
243 def do_options ():
244     opt_parser = get_option_parser()
245     (options, args) = opt_parser.parse_args ()
246
247
248     if options.from_version:
249         options.from_version = str_to_tuple (options.from_version)
250     if options.to_version:
251         options.to_version = str_to_tuple (options.to_version)
252
253     options.outfile_name = ''
254     global global_options
255     global_options = options
256
257     if not args and not options.show_rules:
258         opt_parser.print_help ()
259         sys.exit (2)
260
261     return args
262
263 def main ():
264     files = do_options ()
265
266     # should parse files[] to read \version?
267     if global_options.show_rules:
268         show_rules (sys.stdout, global_options.from_version, global_options.to_version)
269         sys.exit (0)
270
271     identify (sys.stderr)
272
273     for f in files:
274         if f == '-':
275             f = ''
276         elif not os.path.isfile (f):
277             error (_ ("cannot open file: `%s'") % f)
278             if len (files) == 1:
279                 sys.exit (1)
280             continue
281         try:
282             do_one_file (f)
283         except UnknownVersion:
284             error (_ ("cannot determine version for `%s'.  Skipping") % f)
285
286     sys.stderr.write ('\n')
287
288 main ()