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