PageRenderTime 23ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/optimizations/run_all.py

https://bitbucket.org/pombredanne/nuitka
Python | 180 lines | 116 code | 39 blank | 25 comment | 26 complexity | 091f98bd0cb85660991a43e586a1a676 MD5 | raw file
  1. #!/usr/bin/env python
  2. # Copyright 2013, Kay Hayen, mailto:kay.hayen@gmail.com
  3. #
  4. # Python test originally created or extracted from other peoples work. The
  5. # parts from me are licensed as below. It is at least Free Softwar where
  6. # it's copied from other people. In these cases, that will normally be
  7. # indicated.
  8. #
  9. # Licensed under the Apache License, Version 2.0 (the "License");
  10. # you may not use this file except in compliance with the License.
  11. # You may obtain a copy of the License at
  12. #
  13. # http://www.apache.org/licenses/LICENSE-2.0
  14. #
  15. # Unless required by applicable law or agreed to in writing, software
  16. # distributed under the License is distributed on an "AS IS" BASIS,
  17. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18. # See the License for the specific language governing permissions and
  19. # limitations under the License.
  20. #
  21. from __future__ import print_function
  22. import os, sys, subprocess, tempfile, shutil
  23. try:
  24. import lxml.etree
  25. except ImportError:
  26. sys.exit( "Error, no 'lxml' module installed, cannot run XML based tests." )
  27. # Go its own directory, to have it easy with path knowledge.
  28. os.chdir( os.path.dirname( os.path.abspath( __file__ ) ) )
  29. search_mode = len( sys.argv ) > 1 and sys.argv[1] == "search"
  30. start_at = sys.argv[2] if len( sys.argv ) > 2 else None
  31. if start_at:
  32. active = False
  33. else:
  34. active = True
  35. if "PYTHON" not in os.environ:
  36. os.environ[ "PYTHON" ] = "python"
  37. def check_output(*popenargs, **kwargs):
  38. from subprocess import Popen, PIPE, CalledProcessError
  39. if 'stdout' in kwargs:
  40. raise ValueError('stdout argument not allowed, it will be overridden.')
  41. process = Popen(stdout=PIPE, *popenargs, **kwargs)
  42. output, unused_err = process.communicate()
  43. retcode = process.poll()
  44. if retcode:
  45. cmd = kwargs.get("args")
  46. if cmd is None:
  47. cmd = popenargs[0]
  48. raise CalledProcessError(retcode, cmd, output=output)
  49. return output
  50. version_output = check_output(
  51. [ os.environ[ "PYTHON" ], "--version" ],
  52. stderr = subprocess.STDOUT
  53. )
  54. python_version = version_output.split()[1]
  55. os.environ[ "PYTHONPATH" ] = os.getcwd()
  56. print( "Using concrete python", python_version )
  57. def checkSequence( statements ):
  58. for statement in module_statements:
  59. kind = getKind( statement )
  60. if kind == "Print":
  61. print_args = getRole( statement, "values" )
  62. if len( print_args ) != 1:
  63. sys.exit( "Error, print with more than one argument." )
  64. print_arg = print_args[0]
  65. if getKind( print_arg ) != "ConstantRef":
  66. sys.exit( "Error, print of non-constant %s." % getKind( print_arg ) )
  67. else:
  68. sys.exit( "Error, non-print statement of unknown kind '%s'." % kind )
  69. for filename in sorted( os.listdir( "." ) ):
  70. if not filename.endswith( ".py" ) or filename.startswith( "run_" ):
  71. continue
  72. # Skip tests that require Python 2.7 at least.
  73. if filename.endswith( "27.py" ) and python_version.startswith( b"2.6" ):
  74. continue
  75. path = filename
  76. if not active and start_at in ( filename, path ):
  77. active = True
  78. extra_flags = [ "expect_success" ]
  79. if active:
  80. # Apply 2to3 conversion if necessary.
  81. assert type( python_version ) is bytes
  82. if python_version.startswith( b"3" ):
  83. new_path = os.path.join( tempfile.gettempdir(), filename )
  84. shutil.copy( path, new_path )
  85. path = new_path
  86. # No idea how to make this portable to Windows, but we can delay it until
  87. # Python3 is fully functional under Linux first.
  88. result = subprocess.call(
  89. "2to3 -w -n --no-diffs " + path,
  90. stderr = open( "/dev/null", "w" ),
  91. shell = True
  92. )
  93. print( "Consider", path, end = " " )
  94. command = "%s %s --dump-xml %s" % (
  95. os.environ[ "PYTHON" ],
  96. os.path.join( "..", "..", "bin", "nuitka" ),
  97. path
  98. )
  99. result = subprocess.check_output(
  100. command,
  101. shell = True
  102. )
  103. root = lxml.etree.fromstring( result )
  104. module_body = root[0]
  105. module_statements_sequence = module_body[ 0 ]
  106. assert len( module_statements_sequence ) == 1
  107. module_statements = iter( module_statements_sequence ).next()
  108. def getKind( node ):
  109. result = node.attrib[ "kind" ]
  110. result = result.replace( "Statements", "" )
  111. result = result.replace( "Statement", "" )
  112. result = result.replace( "Expression", "" )
  113. return result
  114. def getRole( node, role ):
  115. for child in node:
  116. if child.tag == "role" and child.attrib[ "name" ] == role:
  117. return child
  118. else:
  119. return None
  120. checkSequence( module_statements )
  121. # TODO: Detect the exception from above
  122. if False and result == 2:
  123. sys.stderr.write( "Interruped, with CTRL-C\n" )
  124. sys.exit( 2 )
  125. # TODO: Detect error case
  126. if False and result != 0 and search_mode:
  127. print( "Error exit!", result )
  128. sys.exit( result )
  129. print( "OK." )
  130. else:
  131. print("Skipping", filename)