PageRenderTime 66ms CodeModel.GetById 37ms RepoModel.GetById 0ms app.codeStats 0ms

/scripts/convert-ly.py

https://gitlab.com/aguai/lilypond
Python | 367 lines | 334 code | 10 blank | 23 comment | 6 complexity | b0a90c77225c374f23ee4dda2db359f8 MD5 | raw file
  1. #!@TARGET_PYTHON@
  2. # convert-ly.py -- Update old LilyPond input files (fix name?)
  3. # converting rules are found in python/convertrules.py
  4. # This file is part of LilyPond, the GNU music typesetter.
  5. #
  6. # Copyright (C) 1998--2012 Han-Wen Nienhuys <hanwen@xs4all.nl>
  7. # Jan Nieuwenhuizen <janneke@gnu.org>
  8. #
  9. # LilyPond is free software: you can redistribute it and/or modify
  10. # it under the terms of the GNU General Public License as published by
  11. # the Free Software Foundation, either version 3 of the License, or
  12. # (at your option) any later version.
  13. #
  14. # LilyPond is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU General Public License
  20. # along with LilyPond. If not, see <http://www.gnu.org/licenses/>.
  21. import os
  22. import sys
  23. import re
  24. """
  25. @relocate-preamble@
  26. """
  27. import lilylib as ly
  28. global _;_=ly._
  29. ly.require_python_version ()
  30. import convertrules
  31. lilypond_version_re_str = '\\\\version *\"([0-9.]+)"'
  32. lilypond_version_re = re.compile (lilypond_version_re_str)
  33. lilypond_version_strict_re_str = '\\\\version *\"([0-9]+[.][0-9]+[.][0-9]+)"'
  34. lilypond_version_strict_re = re.compile (lilypond_version_strict_re_str)
  35. help_summary = (
  36. _ ('''Update LilyPond input to newer version. By default, update from the
  37. version taken from the \\version command, to the current LilyPond version.''')
  38. + "\n"
  39. + _ ("If FILE is `-', read from standard input.")
  40. + "\n\n"
  41. + _ ("Examples:")
  42. + '''
  43. $ convert-ly -e old.ly
  44. $ convert-ly --from=2.3.28 --to=2.5.21 foobar.ly > foobar-new.ly
  45. ''')
  46. copyright = ('Jan Nieuwenhuizen <janneke@gnu.org>',
  47. 'Han-Wen Nienhuys <hanwen@xs4all.nl>')
  48. program_name = os.path.basename (sys.argv[0])
  49. program_version = '@TOPLEVEL_VERSION@'
  50. authors = ('Jan Nieuwenhuizen <janneke@gnu.org>',
  51. 'Han-Wen Nienhuys <hanwen@xs4all.nl>')
  52. def identify ():
  53. ly.progress ('%s (GNU LilyPond) %s\n' % (program_name, program_version))
  54. def warranty ():
  55. identify ()
  56. ly.encoded_write (sys.stdout, '''
  57. %s
  58. %s
  59. %s
  60. %s
  61. ''' % ( _ ('Copyright (c) %s by') % '2001--2012',
  62. ' '.join (authors),
  63. _ ('Distributed under terms of the GNU General Public License.'),
  64. _ ('It comes with NO WARRANTY.')))
  65. def get_option_parser ():
  66. p = ly.get_option_parser (usage=_ ("%s [OPTION]... FILE") % 'convert-ly',
  67. description=help_summary,
  68. add_help_option=False)
  69. p.version="@TOPLEVEL_VERSION@"
  70. p.add_option("--version",
  71. action="version",
  72. help=_ ("show version number and exit"))
  73. p.add_option("-h", "--help",
  74. action="help",
  75. help=_ ("show this help and exit"))
  76. p.add_option ('-f', '--from',
  77. action="store",
  78. metavar=_ ("VERSION"),
  79. dest="from_version",
  80. help=_ ("start from VERSION [default: \\version found in file]"),
  81. default='')
  82. p.add_option ('-e', '--edit', help=_ ("edit in place"),
  83. action='store_true')
  84. p.add_option ("-l", "--loglevel",
  85. help=_ ("Print log messages according to LOGLEVEL "
  86. "(NONE, ERROR, WARNING, PROGRESS (default), DEBUG)"),
  87. metavar=_ ("LOGLEVEL"),
  88. action='callback',
  89. callback=ly.handle_loglevel_option,
  90. type='string')
  91. p.add_option ('-n', '--no-version',
  92. help=_ ("do not add \\version command if missing"),
  93. action='store_true',
  94. dest='skip_version_add',
  95. default=False)
  96. p.add_option ('-c', '--current-version',
  97. help=_ ("force updating \\version number to %s") % program_version,
  98. action='store_true',
  99. dest='force_current_version',
  100. default=False)
  101. p.add_option ('-d', '--diff-version-update',
  102. help=_ ("only update \\version number if file is modified"),
  103. action='store_true',
  104. dest='diff_version_update',
  105. default=False)
  106. p.add_option ("-s", '--show-rules',
  107. help=_ ("show rules [default: -f 0, -t %s]") % program_version,
  108. dest='show_rules',
  109. action='store_true', default=False)
  110. p.add_option ('-t', '--to',
  111. help=_ ("convert to VERSION [default: %s]") % program_version,
  112. metavar=_ ('VERSION'),
  113. action='store',
  114. dest="to_version",
  115. default='')
  116. p.add_option ('-w', '--warranty', help=_ ("show warranty and copyright"),
  117. action='store_true',
  118. ),
  119. p.add_option_group ('',
  120. description=(
  121. _ ("Report bugs via %s")
  122. % 'http://post.gmane.org/post.php'
  123. '?group=gmane.comp.gnu.lilypond.bugs') + '\n')
  124. return p
  125. def str_to_tuple (s):
  126. return tuple ([int(n) for n in s.split ('.')])
  127. def tup_to_str (t):
  128. return '.'.join (['%s' % x for x in t])
  129. def version_cmp (t1, t2):
  130. for x in [0, 1, 2]:
  131. if t1[x] - t2[x]:
  132. return t1[x] - t2[x]
  133. return 0
  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. def latest_version ():
  139. return convertrules.conversions[-1][0]
  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. ly.encoded_write (file, '%s: %s\n' % (tup_to_str (x[0]), x[2]))
  145. def do_conversion (str, from_version, to_version):
  146. """Apply conversions from FROM_VERSION to TO_VERSION. Return
  147. tuple (LAST,STR), with the last successful conversion and the resulting
  148. string."""
  149. conv_list = get_conversions (from_version, to_version)
  150. ly.progress (_ ("Applying conversion: "), newline = False)
  151. last_conversion = None
  152. errors = 0
  153. try:
  154. for x in conv_list:
  155. if x != conv_list[-1]:
  156. ly.progress (tup_to_str (x[0]), newline = False)
  157. ly.progress (', ', newline = False)
  158. else:
  159. ly.progress (tup_to_str (x[0]))
  160. str = x[1] (str)
  161. last_conversion = x[0]
  162. except convertrules.FatalConversionError:
  163. ly.error (_ ("Error while converting")
  164. + '\n'
  165. + _ ("Stopping at last successful rule"))
  166. errors += 1
  167. return (last_conversion, str, errors)
  168. def guess_lilypond_version (input):
  169. m = lilypond_version_strict_re.search (input)
  170. if m:
  171. return m.group (1)
  172. m = lilypond_version_re.search (input)
  173. if m:
  174. raise InvalidVersion (m.group (1))
  175. else:
  176. return ''
  177. class FatalConversionError (Exception):
  178. pass
  179. class UnknownVersion (Exception):
  180. pass
  181. class InvalidVersion (Exception):
  182. def __init__ (self, version):
  183. self.version = version
  184. def do_one_file (infile_name):
  185. ly.progress (_ (u"Processing `%s\'... ") % infile_name, True)
  186. if infile_name:
  187. infile = open (infile_name, 'r')
  188. input = infile.read ()
  189. infile.close ()
  190. else:
  191. input = sys.stdin.read ()
  192. from_version = None
  193. to_version = None
  194. if global_options.from_version:
  195. from_version = global_options.from_version
  196. else:
  197. guess = guess_lilypond_version (input)
  198. if not guess:
  199. raise UnknownVersion ()
  200. from_version = str_to_tuple (guess)
  201. if global_options.to_version:
  202. to_version = global_options.to_version
  203. else:
  204. to_version = latest_version ()
  205. if len (from_version) != 3:
  206. raise InvalidVersion (".".join ([str(n) for n in from_version]))
  207. (last, result, errors) = do_conversion (input, from_version, to_version)
  208. if global_options.force_current_version and \
  209. (last is None or last == to_version):
  210. last = str_to_tuple (program_version)
  211. if last:
  212. if global_options.diff_version_update:
  213. if result == input:
  214. # check the y in x.y.z (minor version number)
  215. previous_stable = (last[0], 2*(last[1]/2), 0)
  216. if ((last[0:2] != from_version[0:2]) and
  217. (previous_stable > from_version)):
  218. # previous stable version
  219. last = previous_stable
  220. else:
  221. # make no (actual) change to the version number
  222. last = from_version
  223. newversion = r'\version "%s"' % tup_to_str (last)
  224. if lilypond_version_re.search (result):
  225. result = re.sub (lilypond_version_re_str,
  226. '\\' + newversion, result)
  227. elif not global_options.skip_version_add:
  228. result = newversion + '\n' + result
  229. ly.progress ('\n')
  230. if global_options.edit:
  231. try:
  232. os.remove (infile_name + '~')
  233. except:
  234. pass
  235. os.rename (infile_name, infile_name + '~')
  236. outfile = open (infile_name, 'w')
  237. else:
  238. outfile = sys.stdout
  239. outfile.write (result)
  240. sys.stderr.flush ()
  241. return errors
  242. def do_options ():
  243. opt_parser = get_option_parser()
  244. (options, args) = opt_parser.parse_args ()
  245. if options.warranty:
  246. warranty ()
  247. sys.exit (0)
  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. options.outfile_name = ''
  253. global global_options
  254. global_options = options
  255. if not args and not options.show_rules:
  256. opt_parser.print_help ()
  257. sys.exit (2)
  258. return args
  259. def main ():
  260. files = do_options ()
  261. # should parse files[] to read \version?
  262. if global_options.show_rules:
  263. show_rules (sys.stdout, global_options.from_version, global_options.to_version)
  264. sys.exit (0)
  265. identify ()
  266. errors = 0
  267. for f in files:
  268. f = f.decode (sys.stdin.encoding or "utf-8")
  269. if f == '-':
  270. f = ''
  271. elif not os.path.isfile (f):
  272. ly.error (_ (u"%s: Unable to open file") % f)
  273. errors += 1
  274. continue
  275. try:
  276. errors += do_one_file (f)
  277. except UnknownVersion:
  278. ly.error (_ (u"%s: Unable to determine version. Skipping") % f)
  279. errors += 1
  280. except InvalidVersion:
  281. # Compat code for 2.x and 3.0 syntax ("except .. as v" doesn't
  282. # work in python 2.4!):
  283. t, v, b = sys.exc_info ()
  284. ly.error (_ (u"%s: Invalid version string `%s' \n"
  285. "Valid version strings consist of three numbers, "
  286. "separated by dots, e.g. `2.8.12'") % (f, v.version) )
  287. errors += 1
  288. if errors:
  289. ly.warning (ly.ungettext ("There was %d error.",
  290. "There were %d errors.", errors) % errors)
  291. sys.exit (1)
  292. main ()