PageRenderTime 49ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/lib-python/2.7/test/script_helper.py

https://bitbucket.org/yrttyr/pypy
Python | 164 lines | 129 code | 14 blank | 21 comment | 15 complexity | b70de1b206ddf562ed6d781116dc7d0c MD5 | raw file
  1. # Common utility functions used by various script execution tests
  2. # e.g. test_cmd_line, test_cmd_line_script and test_runpy
  3. import sys
  4. import os
  5. import re
  6. import os.path
  7. import tempfile
  8. import subprocess
  9. import py_compile
  10. import contextlib
  11. import shutil
  12. import zipfile
  13. from test.test_support import strip_python_stderr
  14. # Executing the interpreter in a subprocess
  15. def _assert_python(expected_success, *args, **env_vars):
  16. cmd_line = [sys.executable]
  17. if not env_vars:
  18. cmd_line.append('-E')
  19. cmd_line.extend(args)
  20. # Need to preserve the original environment, for in-place testing of
  21. # shared library builds.
  22. env = os.environ.copy()
  23. env.update(env_vars)
  24. p = subprocess.Popen(cmd_line, stdin=subprocess.PIPE,
  25. stdout=subprocess.PIPE, stderr=subprocess.PIPE,
  26. env=env)
  27. try:
  28. out, err = p.communicate()
  29. finally:
  30. subprocess._cleanup()
  31. p.stdout.close()
  32. p.stderr.close()
  33. rc = p.returncode
  34. err = strip_python_stderr(err)
  35. if (rc and expected_success) or (not rc and not expected_success):
  36. raise AssertionError(
  37. "Process return code is %d, "
  38. "stderr follows:\n%s" % (rc, err.decode('ascii', 'ignore')))
  39. return rc, out, err
  40. def assert_python_ok(*args, **env_vars):
  41. """
  42. Assert that running the interpreter with `args` and optional environment
  43. variables `env_vars` is ok and return a (return code, stdout, stderr) tuple.
  44. """
  45. return _assert_python(True, *args, **env_vars)
  46. def assert_python_failure(*args, **env_vars):
  47. """
  48. Assert that running the interpreter with `args` and optional environment
  49. variables `env_vars` fails and return a (return code, stdout, stderr) tuple.
  50. """
  51. return _assert_python(False, *args, **env_vars)
  52. def python_exit_code(*args):
  53. cmd_line = [sys.executable, '-E']
  54. cmd_line.extend(args)
  55. with open(os.devnull, 'w') as devnull:
  56. return subprocess.call(cmd_line, stdout=devnull,
  57. stderr=subprocess.STDOUT)
  58. def spawn_python(*args, **kwargs):
  59. cmd_line = [sys.executable, '-E']
  60. cmd_line.extend(args)
  61. return subprocess.Popen(cmd_line, stdin=subprocess.PIPE,
  62. stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
  63. **kwargs)
  64. def kill_python(p):
  65. p.stdin.close()
  66. data = p.stdout.read()
  67. p.stdout.close()
  68. # try to cleanup the child so we don't appear to leak when running
  69. # with regrtest -R.
  70. p.wait()
  71. subprocess._cleanup()
  72. return data
  73. def run_python(*args, **kwargs):
  74. if __debug__:
  75. p = spawn_python(*args, **kwargs)
  76. else:
  77. p = spawn_python('-O', *args, **kwargs)
  78. stdout_data = kill_python(p)
  79. return p.wait(), stdout_data
  80. # Script creation utilities
  81. @contextlib.contextmanager
  82. def temp_dir():
  83. dirname = tempfile.mkdtemp()
  84. dirname = os.path.realpath(dirname)
  85. try:
  86. yield dirname
  87. finally:
  88. shutil.rmtree(dirname)
  89. def make_script(script_dir, script_basename, source):
  90. script_filename = script_basename+os.extsep+'py'
  91. script_name = os.path.join(script_dir, script_filename)
  92. script_file = open(script_name, 'w')
  93. script_file.write(source)
  94. script_file.close()
  95. return script_name
  96. def compile_script(script_name):
  97. py_compile.compile(script_name, doraise=True)
  98. if __debug__:
  99. compiled_name = script_name + 'c'
  100. else:
  101. compiled_name = script_name + 'o'
  102. return compiled_name
  103. def make_zip_script(zip_dir, zip_basename, script_name, name_in_zip=None):
  104. zip_filename = zip_basename+os.extsep+'zip'
  105. zip_name = os.path.join(zip_dir, zip_filename)
  106. zip_file = zipfile.ZipFile(zip_name, 'w')
  107. if name_in_zip is None:
  108. name_in_zip = os.path.basename(script_name)
  109. zip_file.write(script_name, name_in_zip)
  110. zip_file.close()
  111. #if test.test_support.verbose:
  112. # zip_file = zipfile.ZipFile(zip_name, 'r')
  113. # print 'Contents of %r:' % zip_name
  114. # zip_file.printdir()
  115. # zip_file.close()
  116. return zip_name, os.path.join(zip_name, name_in_zip)
  117. def make_pkg(pkg_dir):
  118. os.mkdir(pkg_dir)
  119. make_script(pkg_dir, '__init__', '')
  120. def make_zip_pkg(zip_dir, zip_basename, pkg_name, script_basename,
  121. source, depth=1, compiled=False):
  122. unlink = []
  123. init_name = make_script(zip_dir, '__init__', '')
  124. unlink.append(init_name)
  125. init_basename = os.path.basename(init_name)
  126. script_name = make_script(zip_dir, script_basename, source)
  127. unlink.append(script_name)
  128. if compiled:
  129. init_name = compile_script(init_name)
  130. script_name = compile_script(script_name)
  131. unlink.extend((init_name, script_name))
  132. pkg_names = [os.sep.join([pkg_name]*i) for i in range(1, depth+1)]
  133. script_name_in_zip = os.path.join(pkg_names[-1], os.path.basename(script_name))
  134. zip_filename = zip_basename+os.extsep+'zip'
  135. zip_name = os.path.join(zip_dir, zip_filename)
  136. zip_file = zipfile.ZipFile(zip_name, 'w')
  137. for name in pkg_names:
  138. init_name_in_zip = os.path.join(name, init_basename)
  139. zip_file.write(init_name, init_name_in_zip)
  140. zip_file.write(script_name, script_name_in_zip)
  141. zip_file.close()
  142. for name in unlink:
  143. os.unlink(name)
  144. #if test.test_support.verbose:
  145. # zip_file = zipfile.ZipFile(zip_name, 'r')
  146. # print 'Contents of %r:' % zip_name
  147. # zip_file.printdir()
  148. # zip_file.close()
  149. return zip_name, os.path.join(zip_name, script_name_in_zip)