PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/unladen_swallow/lib/spitfire/scripts/spitfire-compile

https://bitbucket.org/hexdump42/pypy-benchmarks
Python | 84 lines | 67 code | 15 blank | 2 comment | 18 complexity | 8c184fca20771ef6f8815280698208f9 MD5 | raw file
Possible License(s): Apache-2.0, GPL-2.0, BSD-3-Clause
  1. #!/usr/bin/env python
  2. import logging
  3. import os
  4. import os.path
  5. import sys
  6. import spitfire
  7. import spitfire.compiler.util
  8. from spitfire.compiler import analyzer
  9. from spitfire.compiler.util import Compiler
  10. def process_file(compiler, filename, options):
  11. def print_output(*args):
  12. if options.verbose:
  13. print >> sys.stderr, ' '.join(args)
  14. try:
  15. if options.output_file:
  16. compiler.write_file = False
  17. if options.output_file == '-':
  18. f = sys.stdout
  19. else:
  20. f = open(options.output_file, 'w')
  21. else:
  22. compiler.write_file = True
  23. src_code = compiler.compile_file(filename)
  24. if options.output_file:
  25. f.write(src_code)
  26. f.close()
  27. except Exception, e:
  28. error_msg = 'Failed processing file: %s' % filename
  29. if options.verbose:
  30. logging.exception(error_msg)
  31. else:
  32. print >> sys.stderr, error_msg
  33. print >> sys.stderr, e
  34. sys.exit(1)
  35. # selectively enable psyco on import methods
  36. def init_psyco(options):
  37. if options.x_psyco:
  38. try:
  39. import psyco
  40. except ImportError:
  41. print >> sys.stderr, 'WARNING: unable to import psyco'
  42. return
  43. import re
  44. psyco.cannotcompile(re.compile)
  45. if options.x_psyco_profile:
  46. psyco.log()
  47. psyco.profile()
  48. else:
  49. import spitfire.compiler.scanner
  50. psyco.bind(spitfire.compiler.scanner.SpitfireScanner.scan)
  51. import copy
  52. psyco.bind(copy.deepcopy)
  53. import yappsrt
  54. psyco.bind(yappsrt.Scanner.token)
  55. import spitfire.compiler.ast
  56. psyco.bind(spitfire.compiler.ast.NodeList.__iter__)
  57. if __name__ == '__main__':
  58. from optparse import OptionParser
  59. op = OptionParser()
  60. spitfire.compiler.util.add_common_options(op)
  61. (options, args) = op.parse_args()
  62. if options.version:
  63. print >> sys.stderr, 'spitfire %s' % spitfire.__version__
  64. sys.exit(0)
  65. init_psyco(options)
  66. compiler_args = Compiler.args_from_optparse(options)
  67. compiler = Compiler(**compiler_args)
  68. for filename in args:
  69. process_file(compiler, filename, options)